Skip to content

Commit fa03b5b

Browse files
ej81spencerkclark
andauthored
Fix convert calendar with different dimension name (pydata#10544)
* Fix convert calendar with different dimension name * Update whats-new.rst --------- Co-authored-by: Spencer Clark <[email protected]>
1 parent 57358ae commit fa03b5b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Bug fixes
2727

2828
- Fix Pydap Datatree backend testing. Testing now compares elements of (unordered) two sets (before, lists) (:pull:`10525`).
2929
By `Miguel Jimenez-Urias <https://github.com/Mikejmnez>`_.
30+
- Fix ``KeyError`` when passing a ``dim`` argument different from the default to ``convert_calendar`` (:pull:`10544`).
31+
By `Eric Jansen <https://github.com/ej81>`_.
3032

3133

3234
Documentation

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.sel(time=out[dim].notnull())
216+
out = out.sel({dim: 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ def test_convert_calendar_errors():
239239
convert_calendar(da, "standard", dim="x")
240240

241241

242+
def test_convert_calendar_dimension_name():
243+
src = DataArray(
244+
date_range("2004-01-01", "2004-01-31", freq="D", calendar="noleap"),
245+
dims=("date",),
246+
name="date",
247+
)
248+
249+
out = convert_calendar(src, "proleptic_gregorian", dim="date")
250+
251+
np.testing.assert_array_equal(src, out)
252+
253+
242254
def test_convert_calendar_same_calendar():
243255
src = DataArray(
244256
date_range("2000-01-01", periods=12, freq="6h", use_cftime=False),

0 commit comments

Comments
 (0)