diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4995b2953f9..5e0c6825a15 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -71,6 +71,8 @@ Bug fixes had no effect. (Mentioned in :issue:`9921`) - Enable ``keep_attrs`` in ``DatasetView.map`` relevant for :py:func:`map_over_datasets` (:pull:`10219`) By `Mathias Hauser `_. +- Variables with no temporal dimension are left untouched by :py:meth:`~xarray.Dataset.convert_calendar`. (:issue:`10266`, :pull:`10268`) + By `Pascal Bourgault `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/coding/calendar_ops.py b/xarray/coding/calendar_ops.py index ec5a7efba4b..5fdd106e179 100644 --- a/xarray/coding/calendar_ops.py +++ b/xarray/coding/calendar_ops.py @@ -213,7 +213,7 @@ def convert_calendar( out[dim] = new_times # Remove NaN that where put on invalid dates in target calendar - out = out.where(out[dim].notnull(), drop=True) + out = out.sel(time=out[dim].notnull()) if use_cftime: # Reassign times to ensure time index of output is a CFTimeIndex diff --git a/xarray/tests/test_calendar_ops.py b/xarray/tests/test_calendar_ops.py index 13e9f7a1030..8dc1c2a503b 100644 --- a/xarray/tests/test_calendar_ops.py +++ b/xarray/tests/test_calendar_ops.py @@ -4,7 +4,7 @@ import pandas as pd import pytest -from xarray import CFTimeIndex, DataArray, infer_freq +from xarray import CFTimeIndex, DataArray, Dataset, infer_freq from xarray.coding.calendar_ops import convert_calendar, interp_calendar from xarray.coding.cftime_offsets import date_range from xarray.testing import assert_identical @@ -63,6 +63,24 @@ def test_convert_calendar(source, target, use_cftime, freq): np.testing.assert_array_equal(conv.time, expected_times) +def test_convert_calendar_dataset(): + # Check that variables without a time dimension are not modified + src = DataArray( + date_range("2004-01-01", "2004-12-31", freq="D", calendar="standard"), + dims=("time",), + name="time", + ) + da_src = DataArray( + np.linspace(0, 1, src.size), dims=("time",), coords={"time": src} + ).expand_dims(lat=[0, 1]) + ds_src = Dataset({"hastime": da_src, "notime": (("lat",), [0, 1])}) + + conv = convert_calendar(ds_src, "360_day", align_on="date") + + assert conv.time.dt.calendar == "360_day" + assert_identical(ds_src.notime, conv.notime) + + @pytest.mark.parametrize( "source,target,freq", [