Skip to content

Commit

Permalink
map ordinal day to day of year
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-b-miller committed Mar 2, 2025
1 parent 9d6953a commit e0bfe19
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion python/cudf_polars/cudf_polars/dsl/expressions/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ def __init__(
self.name = name
self.children = children
self.is_pointwise = True
if self.name not in self._COMPONENT_MAP:
if (
self.name not in self._COMPONENT_MAP
and self.name is not self.Name.OrdinalDay
):
raise NotImplementedError(f"Temporal function {self.name}")

def do_evaluate(
Expand All @@ -132,6 +135,8 @@ def do_evaluate(
for child in self.children
]
(column,) = columns
if self.name is TemporalFunction.Name.OrdinalDay:
return Column(plc.datetime.day_of_year(column.obj))
if self.name is TemporalFunction.Name.Microsecond:
millis = plc.datetime.extract_datetime_component(
column.obj, plc.datetime.DatetimeComponent.MILLISECOND
Expand Down
22 changes: 21 additions & 1 deletion python/cudf_polars/tests/expressions/test_datetime_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

Expand Down Expand Up @@ -135,3 +135,23 @@ def test_date_extract(field):
q = ldf.select(field(pl.col("dates").dt))

assert_gpu_result_equal(q)


@pytest.mark.parametrize(
"start_date, end_date",
[
(datetime.date(2001, 12, 22), datetime.date(2001, 12, 25)),
(datetime.date(2000, 2, 27), datetime.date(2000, 3, 1)), # Leap year transition
(datetime.date(1999, 12, 31), datetime.date(2000, 1, 2)),
(datetime.date(2020, 2, 28), datetime.date(2020, 3, 1)),
(datetime.date(2021, 1, 1), datetime.date(2021, 1, 2)),
],
)
def test_ordinal_day(start_date, end_date):
df = pl.DataFrame({"date": pl.date_range(start_date, end_date, eager=True)}).lazy()

q = df.with_columns(
pl.col("date").dt.ordinal_day().alias("day_of_year"),
)

assert_gpu_result_equal(q)

0 comments on commit e0bfe19

Please sign in to comment.