Skip to content

Commit 7612078

Browse files
authored
move scalar-handling logic into possibly_convert_objects (#9900)
* move scalar-handling logic into `possibly_convert_objects` * add whats-new.rst entry
1 parent 86a408d commit 7612078

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Internal Changes
6161
~~~~~~~~~~~~~~~~
6262
- Move non-CF related ``ensure_dtype_not_object`` from conventions to backends (:pull:`9828`).
6363
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
64+
- Move handling of scalar datetimes into ``_possibly_convert_objects``
65+
within ``as_compatible_data``. This is consistent with how lists of these objects
66+
will be converted (:pull:`9900`).
67+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
6468

6569
.. _whats-new.2024.11.0:
6670

xarray/core/variable.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numbers
77
import warnings
88
from collections.abc import Callable, Hashable, Mapping, Sequence
9-
from datetime import timedelta
109
from functools import partial
1110
from types import EllipsisType
1211
from typing import TYPE_CHECKING, Any, NoReturn, cast
@@ -232,10 +231,16 @@ def _as_nanosecond_precision(data):
232231

233232
def _possibly_convert_objects(values):
234233
"""Convert arrays of datetime.datetime and datetime.timedelta objects into
235-
datetime64 and timedelta64, according to the pandas convention. For the time
236-
being, convert any non-nanosecond precision DatetimeIndex or TimedeltaIndex
237-
objects to nanosecond precision. While pandas is relaxing this in version
238-
2.0.0, in xarray we will need to make sure we are ready to handle
234+
datetime64 and timedelta64, according to the pandas convention.
235+
236+
* datetime.datetime
237+
* datetime.timedelta
238+
* pd.Timestamp
239+
* pd.Timedelta
240+
241+
For the time being, convert any non-nanosecond precision DatetimeIndex or
242+
TimedeltaIndex objects to nanosecond precision. While pandas is relaxing this
243+
in version 2.0.0, in xarray we will need to make sure we are ready to handle
239244
non-nanosecond precision datetimes or timedeltas in our code before allowing
240245
such values to pass through unchanged. Converting to nanosecond precision
241246
through pandas.Series objects ensures that datetimes and timedeltas are
@@ -305,13 +310,6 @@ def convert_non_numpy_type(data):
305310
if isinstance(data, tuple):
306311
data = utils.to_0d_object_array(data)
307312

308-
if isinstance(data, pd.Timestamp):
309-
# TODO: convert, handle datetime objects, too
310-
data = np.datetime64(data.value, "ns")
311-
312-
if isinstance(data, timedelta):
313-
data = np.timedelta64(getattr(data, "value", data), "ns")
314-
315313
# we don't want nested self-described arrays
316314
if isinstance(data, pd.Series | pd.DataFrame):
317315
pandas_data = data.values

0 commit comments

Comments
 (0)