Skip to content
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
merged 3 commits into from
Jul 26, 2024
Merged
Changes from 1 commit
Commits
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
26 changes: 26 additions & 0 deletions python/ray/data/_internal/numpy_support.py
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
Expand Down Expand Up @@ -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]'
Copy link
Member

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?

Copy link
Contributor Author

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.


return highest_precision

def _convert_datetime_list_to_array(datetime_list: List[datetime]) -> np.ndarray:
# Detect highest precision
Copy link
Member

Choose a reason for hiding this comment

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

Nit: This comment seems superfluous since the function is already name detect_highest_dataetime_precision

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:
Expand All @@ -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
Expand Down