Skip to content

Commit 40e7638

Browse files
authored
feat: provide day_of_year and day_of_week for dt accessor (#1911)
* feat: provide day_of_year and day_of_week for dt accessor * fix mypy * fix lint * fix format * fix doc
1 parent 2baa0e0 commit 40e7638

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

bigframes/operations/datetimes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ def day(self) -> series.Series:
4949
def dayofweek(self) -> series.Series:
5050
return self._apply_unary_op(ops.dayofweek_op)
5151

52+
@property
53+
def day_of_week(self) -> series.Series:
54+
return self.dayofweek
55+
5256
@property
5357
def dayofyear(self) -> series.Series:
5458
return self._apply_unary_op(ops.dayofyear_op)
5559

60+
@property
61+
def day_of_year(self) -> series.Series:
62+
return self.dayofyear
63+
5664
@property
5765
def date(self) -> series.Series:
5866
return self._apply_unary_op(ops.date_op)

tests/system/small/operations/test_datetimes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,28 @@ def test_dt_dayofweek(scalars_dfs, col_name):
8686
pytest.importorskip("pandas", minversion="2.0.0")
8787
scalars_df, scalars_pandas_df = scalars_dfs
8888
bf_series: bigframes.series.Series = scalars_df[col_name]
89+
8990
bf_result = bf_series.dt.dayofweek.to_pandas()
9091
pd_result = scalars_pandas_df[col_name].dt.dayofweek
9192

9293
assert_series_equal(pd_result, bf_result, check_dtype=False)
9394

9495

96+
@pytest.mark.parametrize(
97+
("col_name",),
98+
DATE_COLUMNS,
99+
)
100+
def test_dt_day_of_week(scalars_dfs, col_name):
101+
pytest.importorskip("pandas", minversion="2.0.0")
102+
scalars_df, scalars_pandas_df = scalars_dfs
103+
bf_series: bigframes.series.Series = scalars_df[col_name]
104+
105+
bf_result = bf_series.dt.day_of_week.to_pandas()
106+
pd_result = scalars_pandas_df[col_name].dt.day_of_week
107+
108+
assert_series_equal(pd_result, bf_result, check_dtype=False)
109+
110+
95111
@pytest.mark.parametrize(
96112
("col_name",),
97113
DATE_COLUMNS,
@@ -100,12 +116,28 @@ def test_dt_dayofyear(scalars_dfs, col_name):
100116
pytest.importorskip("pandas", minversion="2.0.0")
101117
scalars_df, scalars_pandas_df = scalars_dfs
102118
bf_series: bigframes.series.Series = scalars_df[col_name]
119+
103120
bf_result = bf_series.dt.dayofyear.to_pandas()
104121
pd_result = scalars_pandas_df[col_name].dt.dayofyear
105122

106123
assert_series_equal(pd_result, bf_result, check_dtype=False)
107124

108125

126+
@pytest.mark.parametrize(
127+
("col_name",),
128+
DATE_COLUMNS,
129+
)
130+
def test_dt_day_of_year(scalars_dfs, col_name):
131+
pytest.importorskip("pandas", minversion="2.0.0")
132+
scalars_df, scalars_pandas_df = scalars_dfs
133+
bf_series: bigframes.series.Series = scalars_df[col_name]
134+
135+
bf_result = bf_series.dt.day_of_year.to_pandas()
136+
pd_result = scalars_pandas_df[col_name].dt.day_of_year
137+
138+
assert_series_equal(pd_result, bf_result, check_dtype=False)
139+
140+
109141
@pytest.mark.parametrize(
110142
("col_name",),
111143
DATETIME_COL_NAMES,

third_party/bigframes_vendored/pandas/core/indexes/accessor.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,40 @@ def dayofweek(self):
6666

6767
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
6868

69+
@property
70+
def day_of_week(self):
71+
"""The day of the week with Monday=0, Sunday=6.
72+
73+
Return the day of the week. It is assumed the week starts on
74+
Monday, which is denoted by 0 and ends on Sunday, which is denoted
75+
by 6.
76+
77+
**Examples:**
78+
79+
>>> import pandas as pd
80+
>>> import bigframes.pandas as bpd
81+
>>> bpd.options.display.progress_bar = None
82+
>>> s = bpd.Series(
83+
... pd.date_range('2016-12-31', '2017-01-08', freq='D').to_series()
84+
... )
85+
>>> s.dt.day_of_week
86+
2016-12-31 00:00:00 5
87+
2017-01-01 00:00:00 6
88+
2017-01-02 00:00:00 0
89+
2017-01-03 00:00:00 1
90+
2017-01-04 00:00:00 2
91+
2017-01-05 00:00:00 3
92+
2017-01-06 00:00:00 4
93+
2017-01-07 00:00:00 5
94+
2017-01-08 00:00:00 6
95+
dtype: Int64
96+
97+
Returns:
98+
Series: Containing integers indicating the day number.
99+
"""
100+
101+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
102+
69103
@property
70104
def dayofyear(self):
71105
"""The ordinal day of the year.
@@ -94,6 +128,34 @@ def dayofyear(self):
94128

95129
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
96130

131+
@property
132+
def day_of_year(self):
133+
"""The ordinal day of the year.
134+
135+
**Examples:**
136+
137+
>>> import pandas as pd
138+
>>> import bigframes.pandas as bpd
139+
>>> bpd.options.display.progress_bar = None
140+
>>> s = bpd.Series(
141+
... pd.date_range('2016-12-28', '2017-01-03', freq='D').to_series()
142+
... )
143+
>>> s.dt.day_of_year
144+
2016-12-28 00:00:00 363
145+
2016-12-29 00:00:00 364
146+
2016-12-30 00:00:00 365
147+
2016-12-31 00:00:00 366
148+
2017-01-01 00:00:00 1
149+
2017-01-02 00:00:00 2
150+
2017-01-03 00:00:00 3
151+
dtype: Int64
152+
153+
Returns:
154+
Series: Containing integers indicating the day number.
155+
"""
156+
157+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
158+
97159
@property
98160
def date(self):
99161
"""Returns a Series with the date part of Timestamps without time and

0 commit comments

Comments
 (0)