Skip to content

Fix convert calendar on non-temporal data in datasets #10268

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 1 commit into from
Apr 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: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/mathause>`_.
- Variables with no temporal dimension are left untouched by :py:meth:`~xarray.Dataset.convert_calendar`. (:issue:`10266`, :pull:`10268`)
By `Pascal Bourgault <https://github.com/aulemahal>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/coding/calendar_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion xarray/tests/test_calendar_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
[
Expand Down
Loading