Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
405b61f
tests
brandon-b-miller Mar 1, 2025
36eb333
initial
brandon-b-miller Mar 1, 2025
7eaaab2
merge/resolve
brandon-b-miller Mar 10, 2025
a60d8e1
merge/resolve
brandon-b-miller Mar 13, 2025
d4d8748
fix up
brandon-b-miller Mar 19, 2025
547ba6c
slightly adjust logic
brandon-b-miller Mar 19, 2025
327e427
merge/resolve
brandon-b-miller Mar 21, 2025
b994c6b
Merge branch 'branch-25.06' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Apr 1, 2025
5065648
clean
brandon-b-miller Apr 1, 2025
bd55421
remove unused mapping
brandon-b-miller Apr 1, 2025
3985014
something partial
brandon-b-miller Apr 1, 2025
a8c13b0
Merge branch 'branch-25.06' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Apr 2, 2025
5a22a24
significantly reduce logic
brandon-b-miller Apr 2, 2025
4b1ee24
clean
brandon-b-miller Apr 2, 2025
1711b61
merge/resolve
brandon-b-miller Apr 3, 2025
fcfd75e
failing tests
brandon-b-miller Apr 3, 2025
bd573b7
merge/resolve/pass
brandon-b-miller Jun 22, 2026
0ce3e8e
clean
brandon-b-miller Jun 22, 2026
65e527a
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Jun 22, 2026
1fb02e7
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
vyasr Jun 22, 2026
0f8d495
address reviews
brandon-b-miller Jun 22, 2026
3a8a4e2
shortcut in astype
brandon-b-miller Jun 23, 2026
5912548
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Jun 23, 2026
7a25dbb
coverage
brandon-b-miller Jun 23, 2026
3d55b64
merge/resolve
brandon-b-miller Jun 24, 2026
55995e7
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Jun 24, 2026
bfbfbe3
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Jun 25, 2026
a3e4b41
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller Jun 25, 2026
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
61 changes: 58 additions & 3 deletions python/cudf_polars/cudf_polars/dsl/expressions/datetime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# TODO: Document TemporalFunction to remove noqa
# ruff: noqa: D101
Expand Down Expand Up @@ -26,6 +26,15 @@
__all__ = ["TemporalFunction"]


_unit_to_nanoseconds_conversion = {
plc.TypeId.DURATION_NANOSECONDS: 1,
plc.TypeId.DURATION_MICROSECONDS: 1_000,
plc.TypeId.DURATION_MILLISECONDS: 1_000_000,
plc.TypeId.DURATION_SECONDS: 1_000_000_000,
plc.TypeId.DURATION_DAYS: 86_400_000_000_000,
}
Comment thread
Matt711 marked this conversation as resolved.
Resolved


class TemporalFunction(Expr):
class Name(IntEnum):
"""Internal and picklable representation of polars' `TemporalFunction`."""
Expand Down Expand Up @@ -112,6 +121,26 @@ def from_polars(cls, obj: polars._expr_nodes.TemporalFunction) -> Self:
"ns": plc.datetime.RoundingFrequency.NANOSECOND,
}

# Number of nanoseconds represented by one unit of each ``total_*`` component.
_TOTAL_COMPONENT_NANOSECONDS: ClassVar[dict[Name, int]] = {
Name.TotalDays: 86_400_000_000_000,
Name.TotalHours: 3_600_000_000_000,
Name.TotalMinutes: 60_000_000_000,
Name.TotalSeconds: 1_000_000_000,
Name.TotalMilliseconds: 1_000_000,
Name.TotalMicroseconds: 1_000,
Name.TotalNanoseconds: 1,
}
_ADDITIONAL_COMPONENTS: ClassVar[list[Name]] = [
Name.TotalDays,
Name.TotalSeconds,
Name.TotalMicroseconds,
Name.TotalMilliseconds,
Name.TotalNanoseconds,
Name.TotalHours,
Name.TotalMinutes,
]
Comment thread
brandon-b-miller marked this conversation as resolved.
Resolved

_valid_ops: ClassVar[set[Name]] = {
*_COMPONENT_MAP.keys(),
Name.IsLeapYear,
Expand All @@ -121,6 +150,7 @@ def from_polars(cls, obj: polars._expr_nodes.TemporalFunction) -> Self:
Name.IsoYear,
Name.MonthStart,
Name.MonthEnd,
*_ADDITIONAL_COMPONENTS,
Name.CastTimeUnit,
Name.Truncate,
}
Expand Down Expand Up @@ -156,7 +186,33 @@ def do_evaluate(
) -> Column:
"""Evaluate this expression given a dataframe for context."""
columns = [child.evaluate(df, context=context) for child in self.children]
if self.name is TemporalFunction.Name.Truncate:
if self.name in self._ADDITIONAL_COMPONENTS:
(column,) = columns
source_ns = _unit_to_nanoseconds_conversion[column.obj.type().id()]
target_ns = self._TOTAL_COMPONENT_NANOSECONDS[self.name]
casted = plc.unary.cast(
Comment thread
brandon-b-miller marked this conversation as resolved.
Resolved
column.obj, plc.DataType(plc.TypeId.INT64), stream=df.stream
)
if source_ns >= target_ns:
# Coarser (or equal) storage unit: exact integer multiply.
op = plc.binaryop.BinaryOperator.MUL
factor = source_ns // target_ns
else:
# Finer storage unit: integer divide. libcudf (like polars)
# truncates toward zero for signed integer division.
op = plc.binaryop.BinaryOperator.DIV
factor = target_ns // source_ns
result = plc.binaryop.binary_operation(
Comment thread
brandon-b-miller marked this conversation as resolved.
Resolved
casted,
plc.Scalar.from_py(
factor, plc.DataType(plc.TypeId.INT64), stream=df.stream

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: if factor is ever 1, we could avoid this plc.binaryop.binary_operation call all together and return Column(casted, dtype=self.dtype)

),
op,
self.dtype.plc_type,
stream=df.stream,
)
return Column(result, dtype=self.dtype)
elif self.name is TemporalFunction.Name.Truncate:
(column, _) = columns
return Column(
plc.datetime.floor_datetimes(
Expand Down Expand Up @@ -246,7 +302,6 @@ def do_evaluate(
self.dtype.plc_type,
stream=df.stream,
)

return Column(result, dtype=self.dtype)
elif self.name is TemporalFunction.Name.MonthEnd:
(column,) = columns
Expand Down
44 changes: 43 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-2026, NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

Expand Down Expand Up @@ -57,6 +57,16 @@ def test_datetime_dataframe_scan(engine: pl.GPUEngine, dtype):
"nanosecond",
]

duration_extract_fields = [
"total_seconds",
"total_milliseconds",
"total_microseconds",
"total_nanoseconds",
"total_days",
"total_hours",
"total_minutes",
]


@pytest.fixture(
ids=datetime_extract_fields,
Expand Down Expand Up @@ -171,6 +181,38 @@ def test_strftime_duration(engine: pl.GPUEngine, format):
assert_ir_translation_raises(q, engine, NotImplementedError)


@pytest.mark.parametrize("field", duration_extract_fields)
@pytest.mark.parametrize(
"dtype", [pl.Duration("ms"), pl.Duration("us"), pl.Duration("ns")]
)
def test_duration_total_component_extract(engine: pl.GPUEngine, field, dtype):
ldf = pl.LazyFrame(
{
"durations": pl.Series(
[
0,
1,
15,
-1500,
1000,
1111,
1500,
11111,
-134234534,
134234534,
# values beyond float64's exact-integer range to guard
# against precision loss in the unit conversion
5857593848682946,
-5857593848682946,
],
dtype=dtype,
),
}
)
q = ldf.select(getattr(pl.col("durations").dt, field)())
assert_gpu_result_equal(q, engine=engine)


@pytest.mark.parametrize(
"dtype", [pl.Date(), pl.Datetime("ms"), pl.Datetime("us"), pl.Datetime("ns")]
)
Expand Down
Loading