From 662bf8b4bb83120ef2d03c93aebee123afbb7681 Mon Sep 17 00:00:00 2001 From: Kai Muehlbauer Date: Wed, 29 Jan 2025 09:03:12 +0100 Subject: [PATCH 1/5] Fix infer_freq, check for subdtype "datetime64"/"timedelta64" --- xarray/coding/frequencies.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/coding/frequencies.py b/xarray/coding/frequencies.py index 8629c491cfb..cf137839f03 100644 --- a/xarray/coding/frequencies.py +++ b/xarray/coding/frequencies.py @@ -48,6 +48,7 @@ from xarray.coding.cftime_offsets import _MONTH_ABBREVIATIONS, _legacy_to_new_freq from xarray.coding.cftimeindex import CFTimeIndex from xarray.core.common import _contains_datetime_like_objects +from xarray.core.dtypes import _is_numpy_subdtype _ONE_MICRO = 1 _ONE_MILLI = _ONE_MICRO * 1000 @@ -88,9 +89,10 @@ def infer_freq(index): elif not _contains_datetime_like_objects(Variable("dim", index)): raise ValueError("'index' must contain datetime-like objects") dtype = np.asarray(index).dtype - if dtype == "datetime64[ns]": + + if _is_numpy_subdtype(dtype, "datetime64"): index = pd.DatetimeIndex(index.values) - elif dtype == "timedelta64[ns]": + elif _is_numpy_subdtype(dtype, "timedelta64"): index = pd.TimedeltaIndex(index.values) else: index = CFTimeIndex(index.values) From 34b17e4743ec6799fe97ed08acfd557de39b4d45 Mon Sep 17 00:00:00 2001 From: Kai Muehlbauer Date: Wed, 29 Jan 2025 09:25:30 +0100 Subject: [PATCH 2/5] update infer_freq test --- xarray/tests/test_cftimeindex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 8fc79a0cc53..aeaae73f2ea 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -1377,16 +1377,16 @@ def test_asi8_empty_cftimeindex(): @requires_cftime -def test_infer_freq_valid_types(): +def test_infer_freq_valid_types(time_unit): cf_indx = xr.cftime_range("2000-01-01", periods=3, freq="D") assert xr.infer_freq(cf_indx) == "D" assert xr.infer_freq(xr.DataArray(cf_indx)) == "D" - pd_indx = pd.date_range("2000-01-01", periods=3, freq="D") + pd_indx = pd.date_range("2000-01-01", periods=3, freq="D").as_unit(time_unit) assert xr.infer_freq(pd_indx) == "D" assert xr.infer_freq(xr.DataArray(pd_indx)) == "D" - pd_td_indx = pd.timedelta_range(start="1D", periods=3, freq="D") + pd_td_indx = pd.timedelta_range(start="1D", periods=3, freq="D").as_unit(time_unit) assert xr.infer_freq(pd_td_indx) == "D" assert xr.infer_freq(xr.DataArray(pd_td_indx)) == "D" From 754cab58b9e81ca8ecbdc42321dbbb45fc0043f3 Mon Sep 17 00:00:00 2001 From: Kai Muehlbauer Date: Wed, 29 Jan 2025 09:28:46 +0100 Subject: [PATCH 3/5] add whats-new.rst entry --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 54f9fec28d9..86c329e12a2 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -50,7 +50,7 @@ eventually be deprecated. New Features ~~~~~~~~~~~~ -- Relax nanosecond datetime restriction in CF time decoding (:issue:`7493`, :pull:`9618`). +- Relax nanosecond datetime restriction in CF time decoding (:issue:`7493`, :pull:`9618`, :pull:`9999`). By `Kai Mühlbauer `_ and `Spencer Clark `_. - Enable the ``compute=False`` option in :py:meth:`DataTree.to_zarr`. (:pull:`9958`). By `Sam Levang `_. From cb137716b27f988ed157ef12538a91409811b47d Mon Sep 17 00:00:00 2001 From: Kai Muehlbauer Date: Wed, 29 Jan 2025 09:52:13 +0100 Subject: [PATCH 4/5] add typing to test function --- xarray/tests/test_cftimeindex.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 92abcec9404..0e809a5dec6 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -31,6 +31,8 @@ _all_cftime_date_types, ) +from xarray.core.types import PDDatetimeUnitOptions + # cftime 1.5.2 renames "gregorian" to "standard" standard_or_gregorian = "" if has_cftime: @@ -1393,7 +1395,7 @@ def test_asi8_empty_cftimeindex(): @requires_cftime -def test_infer_freq_valid_types(time_unit): +def test_infer_freq_valid_types(time_unit: PDDatetimeUnitOptions) -> None: cf_indx = xr.cftime_range("2000-01-01", periods=3, freq="D") assert xr.infer_freq(cf_indx) == "D" assert xr.infer_freq(xr.DataArray(cf_indx)) == "D" From 2fc9f51bea157370e149c0d907e507f0cc73e80d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Jan 2025 09:02:08 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_cftimeindex.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 0e809a5dec6..9531f3fbf0b 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -19,6 +19,7 @@ _parse_iso8601, parse_iso8601_like, ) +from xarray.core.types import PDDatetimeUnitOptions from xarray.tests import ( assert_array_equal, assert_identical, @@ -31,8 +32,6 @@ _all_cftime_date_types, ) -from xarray.core.types import PDDatetimeUnitOptions - # cftime 1.5.2 renames "gregorian" to "standard" standard_or_gregorian = "" if has_cftime: