From 86a408d20a946efbd14c3f1beb72bd13ebd6a481 Mon Sep 17 00:00:00 2001 From: Max Jones <14077947+maxrjones@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:21:52 -0700 Subject: [PATCH 1/2] Add missing DataTree attributes to docs (#9876) --- doc/api.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/api.rst b/doc/api.rst index 85ef46ca6ba..342ae08e1a4 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -626,12 +626,14 @@ Attributes relating to the recursive tree-like structure of a ``DataTree``. DataTree.depth DataTree.width DataTree.subtree + DataTree.subtree_with_keys DataTree.descendants DataTree.siblings DataTree.lineage DataTree.parents DataTree.ancestors DataTree.groups + DataTree.xindexes Data Contents ------------- @@ -645,6 +647,7 @@ This interface echoes that of ``xarray.Dataset``. DataTree.dims DataTree.sizes DataTree.data_vars + DataTree.ds DataTree.coords DataTree.attrs DataTree.encoding From 76120781f2b2205cc37bd97d0b4ed13d78521d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Wed, 18 Dec 2024 07:50:31 +0100 Subject: [PATCH 2/2] move scalar-handling logic into `possibly_convert_objects` (#9900) * move scalar-handling logic into `possibly_convert_objects` * add whats-new.rst entry --- doc/whats-new.rst | 4 ++++ xarray/core/variable.py | 22 ++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ecf1702c356..dec80590c11 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -61,6 +61,10 @@ Internal Changes ~~~~~~~~~~~~~~~~ - Move non-CF related ``ensure_dtype_not_object`` from conventions to backends (:pull:`9828`). By `Kai Mühlbauer `_. +- 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 `_. .. _whats-new.2024.11.0: diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 07113d66b5b..9d56555f31b 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -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 @@ -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 @@ -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