Skip to content

feat: Add "dayofyear" property for dt accessors #1692

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
May 6, 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
7 changes: 7 additions & 0 deletions bigframes/core/compile/scalar_op_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,13 @@ def dayofweek_op_impl(x: ibis_types.Value):
)


@scalar_op_compiler.register_unary_op(ops.dayofyear_op)
def dayofyear_op_impl(x: ibis_types.Value):
return (
typing.cast(ibis_types.TimestampValue, x).day_of_year().cast(ibis_dtypes.int64)
)


@scalar_op_compiler.register_unary_op(ops.hour_op)
def hour_op_impl(x: ibis_types.Value):
return typing.cast(ibis_types.TimestampValue, x).hour().cast(ibis_dtypes.int64)
Expand Down
2 changes: 2 additions & 0 deletions bigframes/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
date_diff_op,
day_op,
dayofweek_op,
dayofyear_op,
month_op,
quarter_op,
year_op,
Expand Down Expand Up @@ -261,6 +262,7 @@
"month_op",
"year_op",
"dayofweek_op",
"dayofyear_op",
"quarter_op",
# Time ops
"hour_op",
Expand Down
5 changes: 5 additions & 0 deletions bigframes/operations/date_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
type_signature=op_typing.DATELIKE_ACCESSOR,
)

dayofyear_op = base_ops.create_unary_op(
name="dayofyear",
type_signature=op_typing.DATELIKE_ACCESSOR,
)

quarter_op = base_ops.create_unary_op(
name="quarter",
type_signature=op_typing.DATELIKE_ACCESSOR,
Expand Down
4 changes: 4 additions & 0 deletions bigframes/operations/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def day(self) -> series.Series:
def dayofweek(self) -> series.Series:
return self._apply_unary_op(ops.dayofweek_op)

@property
def dayofyear(self) -> series.Series:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a docstring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Doc updated.

return self._apply_unary_op(ops.dayofyear_op)

@property
def date(self) -> series.Series:
return self._apply_unary_op(ops.date_op)
Expand Down
14 changes: 14 additions & 0 deletions tests/system/small/operations/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ def test_dt_dayofweek(scalars_dfs, col_name):
assert_series_equal(pd_result, bf_result, check_dtype=False)


@pytest.mark.parametrize(
("col_name",),
DATE_COLUMNS,
)
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",),
DATETIME_COL_NAMES,
Expand Down
29 changes: 29 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,35 @@ def dayofweek(self):

raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
def dayofyear(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.dayofyear
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
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