diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 70b5bf2ccaa..3810c806fef 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -63,7 +63,7 @@ eventually be deprecated. New Features ~~~~~~~~~~~~ -- Relax nanosecond datetime / timedelta restriction in CF time decoding (:issue:`7493`, :pull:`9618`, :pull:`9966`, :pull:`9977`). +- Relax nanosecond datetime restriction in CF time decoding (:issue:`7493`, :pull:`9618`, :pull:`9977`, :pull:`9966`, :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 `_. 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) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 85622d54973..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, @@ -1393,16 +1394,16 @@ def test_asi8_empty_cftimeindex(): @requires_cftime -def test_infer_freq_valid_types(): +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" - 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" pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy