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

move scalar-handling logic into possibly_convert_objects #9900

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Internal Changes
~~~~~~~~~~~~~~~~
- Move non-CF related ``ensure_dtype_not_object`` from conventions to backends (:pull:`9828`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
- Move handling of scalar datetimes into ``_possibly_convert_objects``
within ``as_compatible_data``. This is consistent with how lists of these objects
will be converted (:pull:`9900`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

.. _whats-new.2024.11.0:

Expand Down
22 changes: 10 additions & 12 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numbers
import warnings
from collections.abc import Callable, Hashable, Mapping, Sequence
from datetime import timedelta
from functools import partial
from types import EllipsisType
from typing import TYPE_CHECKING, Any, NoReturn, cast
Expand Down Expand Up @@ -232,10 +231,16 @@ def _as_nanosecond_precision(data):

def _possibly_convert_objects(values):
"""Convert arrays of datetime.datetime and datetime.timedelta objects into
datetime64 and timedelta64, according to the pandas convention. For the time
being, convert any non-nanosecond precision DatetimeIndex or TimedeltaIndex
objects to nanosecond precision. While pandas is relaxing this in version
2.0.0, in xarray we will need to make sure we are ready to handle
datetime64 and timedelta64, according to the pandas convention.

* datetime.datetime
* datetime.timedelta
* pd.Timestamp
* pd.Timedelta

For the time being, convert any non-nanosecond precision DatetimeIndex or
TimedeltaIndex objects to nanosecond precision. While pandas is relaxing this
in version 2.0.0, in xarray we will need to make sure we are ready to handle
non-nanosecond precision datetimes or timedeltas in our code before allowing
such values to pass through unchanged. Converting to nanosecond precision
through pandas.Series objects ensures that datetimes and timedeltas are
Expand Down Expand Up @@ -305,13 +310,6 @@ def convert_non_numpy_type(data):
if isinstance(data, tuple):
data = utils.to_0d_object_array(data)

if isinstance(data, pd.Timestamp):
# TODO: convert, handle datetime objects, too
data = np.datetime64(data.value, "ns")

if isinstance(data, timedelta):
data = np.timedelta64(getattr(data, "value", data), "ns")

# we don't want nested self-described arrays
if isinstance(data, pd.Series | pd.DataFrame):
pandas_data = data.values
Expand Down
Loading