Skip to content

Fix infer_freq, check for subdtype "datetime64"/"timedelta64" #9999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading