-
Notifications
You must be signed in to change notification settings - Fork 7k
[Data] Compute Expressions-datetime #58740
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
alexeykudinkin
merged 11 commits into
ray-project:master
from
400Ping:data/compute-expressions-dt
Nov 24, 2025
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a3c1f01
[data]Compute Expressions-datetime
400Ping 9d6cf9d
fix prpperty
400Ping 502650c
add type-checking
400Ping 5a04f43
[Data] Fix datetime namespace review comments
400Ping b2ec0da
fix linter
400Ping d01c544
fix return_dtype
400Ping 7888ddc
fix comments ine test_namespace
400Ping e493098
Merge branch 'master' into data/compute-expressions-dt
400Ping 3fb454c
fix
400Ping 1d6053b
fix
400Ping 7152feb
fix
400Ping 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
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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Callable, Literal | ||
|
|
||
| import pyarrow | ||
| import pyarrow.compute as pc | ||
|
|
||
| from ray.data.datatype import DataType | ||
| from ray.data.expressions import pyarrow_udf | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ray.data.expressions import Expr, UDFExpr | ||
|
|
||
| TemporalUnit = Literal[ | ||
| "year", | ||
| "quarter", | ||
| "month", | ||
| "week", | ||
| "day", | ||
| "hour", | ||
| "minute", | ||
| "second", | ||
| "millisecond", | ||
| "microsecond", | ||
| "nanosecond", | ||
| ] | ||
|
|
||
|
|
||
| @dataclass | ||
| class _DatetimeNamespace: | ||
| """Datetime namespace for operations on datetime-typed expression columns.""" | ||
|
|
||
| _expr: "Expr" | ||
|
|
||
| def _unary_temporal_int( | ||
| self, func: Callable[[pyarrow.Array], pyarrow.Array] | ||
| ) -> "UDFExpr": | ||
| """Helper for year/month/… that return int32.""" | ||
|
|
||
| @pyarrow_udf(return_dtype=DataType.int32()) | ||
| def _udf(arr: pyarrow.Array) -> pyarrow.Array: | ||
| return func(arr) | ||
|
|
||
| return _udf(self._expr) | ||
|
|
||
| # extractors | ||
|
|
||
| def year(self) -> "UDFExpr": | ||
| """Extract year component.""" | ||
| return self._unary_temporal_int(pc.year) | ||
|
|
||
| def month(self) -> "UDFExpr": | ||
| """Extract month component.""" | ||
| return self._unary_temporal_int(pc.month) | ||
|
|
||
| def day(self) -> "UDFExpr": | ||
| """Extract day component.""" | ||
| return self._unary_temporal_int(pc.day) | ||
|
|
||
| def hour(self) -> "UDFExpr": | ||
| """Extract hour component.""" | ||
| return self._unary_temporal_int(pc.hour) | ||
|
|
||
| def minute(self) -> "UDFExpr": | ||
| """Extract minute component.""" | ||
| return self._unary_temporal_int(pc.minute) | ||
|
|
||
| def second(self) -> "UDFExpr": | ||
| """Extract second component.""" | ||
| return self._unary_temporal_int(pc.second) | ||
|
|
||
| # formatting | ||
|
|
||
| def strftime(self, fmt: str) -> "UDFExpr": | ||
| """Format timestamps with a strftime pattern.""" | ||
|
|
||
| @pyarrow_udf(return_dtype=DataType.string()) | ||
| def _format(arr: pyarrow.Array) -> pyarrow.Array: | ||
| return pc.strftime(arr, format=fmt) | ||
|
|
||
| return _format(self._expr) | ||
|
|
||
| # rounding | ||
|
|
||
| def ceil(self, unit: TemporalUnit) -> "UDFExpr": | ||
| """Ceil timestamps to the next multiple of the given unit.""" | ||
| return_dtype = DataType.temporal() | ||
400Ping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @pyarrow_udf(return_dtype=return_dtype) | ||
| def _ceil(arr: pyarrow.Array) -> pyarrow.Array: | ||
| return pc.ceil_temporal(arr, multiple=1, unit=unit) | ||
|
|
||
| return _ceil(self._expr) | ||
|
|
||
| def floor(self, unit: TemporalUnit) -> "UDFExpr": | ||
| """Floor timestamps to the previous multiple of the given unit.""" | ||
| return_dtype = DataType.temporal() | ||
|
|
||
| @pyarrow_udf(return_dtype=return_dtype) | ||
| def _floor(arr: pyarrow.Array) -> pyarrow.Array: | ||
| return pc.floor_temporal(arr, multiple=1, unit=unit) | ||
|
|
||
| return _floor(self._expr) | ||
|
|
||
| def round(self, unit: TemporalUnit) -> "UDFExpr": | ||
| """Round timestamps to the nearest multiple of the given unit.""" | ||
| return_dtype = DataType.temporal() | ||
|
|
||
| @pyarrow_udf(return_dtype=return_dtype) | ||
| def _round(arr: pyarrow.Array) -> pyarrow.Array: | ||
|
|
||
| return pc.round_temporal(arr, multiple=1, unit=unit) | ||
|
|
||
| return _round(self._expr) | ||
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
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.