Skip to content

Commit d7a6f2b

Browse files
authored
Fix convert calendar on non-temporal data in datasets (pydata#10268)
1 parent 2c6a776 commit d7a6f2b

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Bug fixes
7171
had no effect. (Mentioned in :issue:`9921`)
7272
- Enable ``keep_attrs`` in ``DatasetView.map`` relevant for :py:func:`map_over_datasets` (:pull:`10219`)
7373
By `Mathias Hauser <https://github.com/mathause>`_.
74+
- Variables with no temporal dimension are left untouched by :py:meth:`~xarray.Dataset.convert_calendar`. (:issue:`10266`, :pull:`10268`)
75+
By `Pascal Bourgault <https://github.com/aulemahal>`_.
7476

7577
Documentation
7678
~~~~~~~~~~~~~

xarray/coding/calendar_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def convert_calendar(
213213
out[dim] = new_times
214214

215215
# Remove NaN that where put on invalid dates in target calendar
216-
out = out.where(out[dim].notnull(), drop=True)
216+
out = out.sel(time=out[dim].notnull())
217217

218218
if use_cftime:
219219
# Reassign times to ensure time index of output is a CFTimeIndex

xarray/tests/test_calendar_ops.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pandas as pd
55
import pytest
66

7-
from xarray import CFTimeIndex, DataArray, infer_freq
7+
from xarray import CFTimeIndex, DataArray, Dataset, infer_freq
88
from xarray.coding.calendar_ops import convert_calendar, interp_calendar
99
from xarray.coding.cftime_offsets import date_range
1010
from xarray.testing import assert_identical
@@ -63,6 +63,24 @@ def test_convert_calendar(source, target, use_cftime, freq):
6363
np.testing.assert_array_equal(conv.time, expected_times)
6464

6565

66+
def test_convert_calendar_dataset():
67+
# Check that variables without a time dimension are not modified
68+
src = DataArray(
69+
date_range("2004-01-01", "2004-12-31", freq="D", calendar="standard"),
70+
dims=("time",),
71+
name="time",
72+
)
73+
da_src = DataArray(
74+
np.linspace(0, 1, src.size), dims=("time",), coords={"time": src}
75+
).expand_dims(lat=[0, 1])
76+
ds_src = Dataset({"hastime": da_src, "notime": (("lat",), [0, 1])})
77+
78+
conv = convert_calendar(ds_src, "360_day", align_on="date")
79+
80+
assert conv.time.dt.calendar == "360_day"
81+
assert_identical(ds_src.notime, conv.notime)
82+
83+
6684
@pytest.mark.parametrize(
6785
"source,target,freq",
6886
[

0 commit comments

Comments
 (0)