Skip to content

feat: provide day_of_year and day_of_week for dt accessor #1911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bigframes/operations/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ def day(self) -> series.Series:
def dayofweek(self) -> series.Series:
return self._apply_unary_op(ops.dayofweek_op)

@property
def day_of_week(self) -> series.Series:
return self.dayofweek

@property
def dayofyear(self) -> series.Series:
return self._apply_unary_op(ops.dayofyear_op)

@property
def day_of_year(self) -> series.Series:
return self.dayofyear

@property
def date(self) -> series.Series:
return self._apply_unary_op(ops.date_op)
Expand Down
32 changes: 32 additions & 0 deletions tests/system/small/operations/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,28 @@ def test_dt_dayofweek(scalars_dfs, col_name):
pytest.importorskip("pandas", minversion="2.0.0")
scalars_df, scalars_pandas_df = scalars_dfs
bf_series: bigframes.series.Series = scalars_df[col_name]

bf_result = bf_series.dt.dayofweek.to_pandas()
pd_result = scalars_pandas_df[col_name].dt.dayofweek

assert_series_equal(pd_result, bf_result, check_dtype=False)


@pytest.mark.parametrize(
("col_name",),
DATE_COLUMNS,
)
def test_dt_day_of_week(scalars_dfs, col_name):
pytest.importorskip("pandas", minversion="2.0.0")
scalars_df, scalars_pandas_df = scalars_dfs
bf_series: bigframes.series.Series = scalars_df[col_name]

bf_result = bf_series.dt.day_of_week.to_pandas()
pd_result = scalars_pandas_df[col_name].dt.day_of_week

assert_series_equal(pd_result, bf_result, check_dtype=False)


@pytest.mark.parametrize(
("col_name",),
DATE_COLUMNS,
Expand All @@ -100,12 +116,28 @@ def test_dt_dayofyear(scalars_dfs, col_name):
pytest.importorskip("pandas", minversion="2.0.0")
scalars_df, scalars_pandas_df = scalars_dfs
bf_series: bigframes.series.Series = scalars_df[col_name]

bf_result = bf_series.dt.dayofyear.to_pandas()
pd_result = scalars_pandas_df[col_name].dt.dayofyear

assert_series_equal(pd_result, bf_result, check_dtype=False)


@pytest.mark.parametrize(
("col_name",),
DATE_COLUMNS,
)
def test_dt_day_of_year(scalars_dfs, col_name):
pytest.importorskip("pandas", minversion="2.0.0")
scalars_df, scalars_pandas_df = scalars_dfs
bf_series: bigframes.series.Series = scalars_df[col_name]

bf_result = bf_series.dt.day_of_year.to_pandas()
pd_result = scalars_pandas_df[col_name].dt.day_of_year

assert_series_equal(pd_result, bf_result, check_dtype=False)


@pytest.mark.parametrize(
("col_name",),
DATETIME_COL_NAMES,
Expand Down
62 changes: 62 additions & 0 deletions third_party/bigframes_vendored/pandas/core/indexes/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@ def dayofweek(self):

raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
def day_of_week(self):
"""The day of the week with Monday=0, Sunday=6.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to set __doc__ to avoid duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like Chelsea just pointed out, there are some minor differences between docs so I think this is no longer feasible.


Return the day of the week. It is assumed the week starts on
Monday, which is denoted by 0 and ends on Sunday, which is denoted
by 6.

**Examples:**

>>> import pandas as pd
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
>>> s = bpd.Series(
... pd.date_range('2016-12-31', '2017-01-08', freq='D').to_series()
... )
>>> s.dt.day_of_week
2016-12-31 00:00:00 5
2017-01-01 00:00:00 6
2017-01-02 00:00:00 0
2017-01-03 00:00:00 1
2017-01-04 00:00:00 2
2017-01-05 00:00:00 3
2017-01-06 00:00:00 4
2017-01-07 00:00:00 5
2017-01-08 00:00:00 6
dtype: Int64

Returns:
Series: Containing integers indicating the day number.
"""

raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
def dayofyear(self):
"""The ordinal day of the year.
Expand Down Expand Up @@ -94,6 +128,34 @@ def dayofyear(self):

raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
def day_of_year(self):
"""The ordinal day of the year.

**Examples:**

>>> import pandas as pd
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
>>> s = bpd.Series(
... pd.date_range('2016-12-28', '2017-01-03', freq='D').to_series()
... )
>>> s.dt.day_of_year
2016-12-28 00:00:00 363
2016-12-29 00:00:00 364
2016-12-30 00:00:00 365
2016-12-31 00:00:00 366
2017-01-01 00:00:00 1
2017-01-02 00:00:00 2
2017-01-03 00:00:00 3
dtype: Int64

Returns:
Series: Containing integers indicating the day number.
"""

raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
def date(self):
"""Returns a Series with the date part of Timestamps without time and
Expand Down