Skip to content

Commit 018b241

Browse files
Fix infer_freq, check for subdtype "datetime64"/"timedelta64" (#9999)
* Fix infer_freq, check for subdtype "datetime64"/"timedelta64" * update infer_freq test * add whats-new.rst entry * add typing to test function * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8ccf1cb commit 018b241

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

doc/whats-new.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ eventually be deprecated.
6363

6464
New Features
6565
~~~~~~~~~~~~
66-
- Relax nanosecond datetime / timedelta restriction in CF time decoding (:issue:`7493`, :pull:`9618`, :pull:`9966`, :pull:`9977`).
66+
- Relax nanosecond datetime restriction in CF time decoding (:issue:`7493`, :pull:`9618`, :pull:`9977`, :pull:`9966`, :pull:`9999`).
6767
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Spencer Clark <https://github.com/spencerkclark>`_.
6868
- Enable the ``compute=False`` option in :py:meth:`DataTree.to_zarr`. (:pull:`9958`).
6969
By `Sam Levang <https://github.com/slevang>`_.

xarray/coding/frequencies.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from xarray.coding.cftime_offsets import _MONTH_ABBREVIATIONS, _legacy_to_new_freq
4949
from xarray.coding.cftimeindex import CFTimeIndex
5050
from xarray.core.common import _contains_datetime_like_objects
51+
from xarray.core.dtypes import _is_numpy_subdtype
5152

5253
_ONE_MICRO = 1
5354
_ONE_MILLI = _ONE_MICRO * 1000
@@ -88,9 +89,10 @@ def infer_freq(index):
8889
elif not _contains_datetime_like_objects(Variable("dim", index)):
8990
raise ValueError("'index' must contain datetime-like objects")
9091
dtype = np.asarray(index).dtype
91-
if dtype == "datetime64[ns]":
92+
93+
if _is_numpy_subdtype(dtype, "datetime64"):
9294
index = pd.DatetimeIndex(index.values)
93-
elif dtype == "timedelta64[ns]":
95+
elif _is_numpy_subdtype(dtype, "timedelta64"):
9496
index = pd.TimedeltaIndex(index.values)
9597
else:
9698
index = CFTimeIndex(index.values)

xarray/tests/test_cftimeindex.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
_parse_iso8601,
2020
parse_iso8601_like,
2121
)
22+
from xarray.core.types import PDDatetimeUnitOptions
2223
from xarray.tests import (
2324
assert_array_equal,
2425
assert_identical,
@@ -1393,16 +1394,16 @@ def test_asi8_empty_cftimeindex():
13931394

13941395

13951396
@requires_cftime
1396-
def test_infer_freq_valid_types():
1397+
def test_infer_freq_valid_types(time_unit: PDDatetimeUnitOptions) -> None:
13971398
cf_indx = xr.cftime_range("2000-01-01", periods=3, freq="D")
13981399
assert xr.infer_freq(cf_indx) == "D"
13991400
assert xr.infer_freq(xr.DataArray(cf_indx)) == "D"
14001401

1401-
pd_indx = pd.date_range("2000-01-01", periods=3, freq="D")
1402+
pd_indx = pd.date_range("2000-01-01", periods=3, freq="D").as_unit(time_unit)
14021403
assert xr.infer_freq(pd_indx) == "D"
14031404
assert xr.infer_freq(xr.DataArray(pd_indx)) == "D"
14041405

1405-
pd_td_indx = pd.timedelta_range(start="1D", periods=3, freq="D")
1406+
pd_td_indx = pd.timedelta_range(start="1D", periods=3, freq="D").as_unit(time_unit)
14061407
assert xr.infer_freq(pd_td_indx) == "D"
14071408
assert xr.infer_freq(xr.DataArray(pd_td_indx)) == "D"
14081409

0 commit comments

Comments
 (0)