Repository navigation
Support cudf-polars total_xxx datetime extraction methods
#18171
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
rapids-bot
merged 28 commits into
NVIDIA:main
from
brandon-b-miller:fea-cudf-polars-more-dt-extracts
Jun 26, 2026
Merged
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
405b61f
tests
brandon-b-miller 36eb333
initial
brandon-b-miller 7eaaab2
merge/resolve
brandon-b-miller a60d8e1
merge/resolve
brandon-b-miller d4d8748
fix up
brandon-b-miller 547ba6c
slightly adjust logic
brandon-b-miller 327e427
merge/resolve
brandon-b-miller b994c6b
Merge branch 'branch-25.06' into fea-cudf-polars-more-dt-extracts
brandon-b-miller 5065648
clean
brandon-b-miller bd55421
remove unused mapping
brandon-b-miller 3985014
something partial
brandon-b-miller a8c13b0
Merge branch 'branch-25.06' into fea-cudf-polars-more-dt-extracts
brandon-b-miller 5a22a24
significantly reduce logic
brandon-b-miller 4b1ee24
clean
brandon-b-miller 1711b61
merge/resolve
brandon-b-miller fcfd75e
failing tests
brandon-b-miller bd573b7
merge/resolve/pass
brandon-b-miller 0ce3e8e
clean
brandon-b-miller 65e527a
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller 1fb02e7
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
vyasr 0f8d495
address reviews
brandon-b-miller 3a8a4e2
shortcut in astype
brandon-b-miller 5912548
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller 7a25dbb
coverage
brandon-b-miller 3d55b64
merge/resolve
brandon-b-miller 55995e7
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller bfbfbe3
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller a3e4b41
Merge branch 'main' into fea-cudf-polars-more-dt-extracts
brandon-b-miller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
@@ -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, | ||
| } | ||
|
|
||
|
|
||
| class TemporalFunction(Expr): | ||
| class Name(IntEnum): | ||
| """Internal and picklable representation of polars' `TemporalFunction`.""" | ||
|
|
@@ -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, | ||
| ] | ||
|
brandon-b-miller marked this conversation as resolved.
Resolved
|
||
|
|
||
| _valid_ops: ClassVar[set[Name]] = { | ||
| *_COMPONENT_MAP.keys(), | ||
| Name.IsLeapYear, | ||
|
|
@@ -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, | ||
| } | ||
|
|
@@ -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( | ||
|
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( | ||
|
brandon-b-miller marked this conversation as resolved.
Resolved
|
||
| casted, | ||
| plc.Scalar.from_py( | ||
| factor, plc.DataType(plc.TypeId.INT64), stream=df.stream | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: if |
||
| ), | ||
| 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( | ||
|
|
@@ -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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.