Skip to content

Commit

Permalink
Fix infer_freq, check for subdtype "datetime64"/"timedelta64" (#9999)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kmuehlbauer and pre-commit-ci[bot] authored Jan 29, 2025
1 parent 8ccf1cb commit 018b241
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/kmuehlbauer>`_ and `Spencer Clark <https://github.com/spencerkclark>`_.
- Enable the ``compute=False`` option in :py:meth:`DataTree.to_zarr`. (:pull:`9958`).
By `Sam Levang <https://github.com/slevang>`_.
Expand Down
6 changes: 4 additions & 2 deletions xarray/coding/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
_parse_iso8601,
parse_iso8601_like,
)
from xarray.core.types import PDDatetimeUnitOptions
from xarray.tests import (
assert_array_equal,
assert_identical,
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 018b241

Please sign in to comment.