diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 605175e32d2..add40bb6b81 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -27,6 +27,8 @@ Bug fixes - Fix Pydap Datatree backend testing. Testing now compares elements of (unordered) two sets (before, lists) (:pull:`10525`). By `Miguel Jimenez-Urias `_. +- Fix ``KeyError`` when passing a ``dim`` argument different from the default to ``convert_calendar`` (:pull:`10544`). + By `Eric Jansen `_. Documentation diff --git a/xarray/coding/calendar_ops.py b/xarray/coding/calendar_ops.py index 5fdd106e179..a6f0254a42d 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.sel(time=out[dim].notnull()) + out = out.sel({dim: 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 8dc1c2a503b..4ec45e4113b 100644 --- a/xarray/tests/test_calendar_ops.py +++ b/xarray/tests/test_calendar_ops.py @@ -239,6 +239,18 @@ def test_convert_calendar_errors(): convert_calendar(da, "standard", dim="x") +def test_convert_calendar_dimension_name(): + src = DataArray( + date_range("2004-01-01", "2004-01-31", freq="D", calendar="noleap"), + dims=("date",), + name="date", + ) + + out = convert_calendar(src, "proleptic_gregorian", dim="date") + + np.testing.assert_array_equal(src, out) + + def test_convert_calendar_same_calendar(): src = DataArray( date_range("2000-01-01", periods=12, freq="6h", use_cftime=False),