-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
[data] Add better support for udf returns from list of datetime objects #46762
Merged
bveeramani
merged 3 commits into
ray-project:master
from
omatthew98:mowen/transform-date-list
Jul 26, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,5 @@ | ||
import collections | ||
from datetime import datetime | ||
from typing import Any, Dict, List, Union | ||
|
||
import numpy as np | ||
|
@@ -38,6 +39,28 @@ def validate_numpy_batch(batch: Union[Dict[str, np.ndarray], Dict[str, list]]) - | |
f"a numpy batch to a block, got: {type(batch)} " | ||
f"({_truncated_repr(batch)})" | ||
) | ||
|
||
def _detect_highest_datetime_precision_dtype(datetime_list: List[datetime]) -> str: | ||
highest_precision = 'datetime64[D]' # Start with day precision | ||
|
||
for dt in datetime_list: | ||
if dt.microsecond != 0: | ||
highest_precision = 'datetime64[ns]' | ||
break | ||
elif dt.second != 0: | ||
highest_precision = 'datetime64[s]' | ||
elif dt.minute != 0: | ||
highest_precision = 'datetime64[m]' | ||
elif dt.hour != 0: | ||
highest_precision = 'datetime64[h]' | ||
|
||
return highest_precision | ||
|
||
def _convert_datetime_list_to_array(datetime_list: List[datetime]) -> np.ndarray: | ||
# Detect highest precision | ||
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. Nit: This comment seems superfluous since the function is already name |
||
dtype_with_precision = _detect_highest_datetime_precision_dtype(datetime_list) | ||
|
||
return np.array([np.datetime64(dt) for dt in datetime_list], dtype=dtype_with_precision) | ||
|
||
|
||
def convert_udf_returns_to_numpy(udf_return_col: Any) -> Any: | ||
|
@@ -64,6 +87,9 @@ def convert_udf_returns_to_numpy(udf_return_col: Any) -> Any: | |
udf_return_col = np.expand_dims(udf_return_col[0], axis=0) | ||
return udf_return_col | ||
|
||
if all(isinstance(elem, datetime) for elem in udf_return_col): | ||
return _convert_datetime_list_to_array(udf_return_col) | ||
|
||
# Try to convert list values into an numpy array via | ||
# np.array(), so users don't need to manually cast. | ||
# NOTE: we don't cast generic iterables, since types like | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own understanding, is this list of precisions exhaustive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The full list is here: https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-units.
Not including lower precision units (W, M, Y) is not a problem unless people need to represent times so distance that the extra range is needed (for context day precision allows for linux epoch +/- 2.5e16 years).
For higher precision units (ns, ps, fs, as), the precision of python datetime is microseconds so I don't think we need to support those in this function.