Skip to content
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

Fix time indexing regression in convert_calendar #9192

Merged
merged 8 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 2 additions & 16 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,19 +580,10 @@ def __init__(
array: Any,
dim: Hashable,
coord_dtype: Any = None,
*,
fastpath: bool = False,
):
if fastpath:
index = array
else:
index = safe_cast_to_index(array)
index = safe_cast_to_index(array).copy()

if index.name is None:
# make a shallow copy: cheap and because the index name may be updated
# here or in other constructors (cannot use pd.Index.rename as this
# constructor is also called from PandasMultiIndex)
index = index.copy()
index.name = dim

self.index = index
Expand All @@ -607,7 +598,7 @@ def _replace(self, index, dim=None, coord_dtype=None):
dim = self.dim
if coord_dtype is None:
coord_dtype = self.coord_dtype
return type(self)(index, dim, coord_dtype, fastpath=True)
return type(self)(index, dim, coord_dtype)

@classmethod
def from_variables(
Expand Down Expand Up @@ -653,11 +644,6 @@ def from_variables(

obj = cls(data, dim, coord_dtype=var.dtype)
assert not isinstance(obj.index, pd.MultiIndex)
# Rename safely
# make a shallow copy: cheap and because the index name may be updated
# here or in other constructors (cannot use pd.Index.rename as this
# constructor is also called from PandasMultiIndex)
obj.index = obj.index.copy()
obj.index.name = name

return obj
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,22 @@ def test_cftimeindex_calendar_property(calendar, expected):
assert index.calendar == expected


@requires_cftime
def test_cftime_noleap_with_str():
# https://github.com/pydata/xarray/issues/9138
time = pd.date_range("2000-01-01", "2006-01-01", freq="D")
temperature = np.ones(len(time))
da = xr.DataArray(
data=temperature,
dims=["time"],
coords=dict(
time=time,
),
)
da_noleap = da.convert_calendar("noleap")
da_noleap.sel(time=slice("2001", "2002"))
spencerkclark marked this conversation as resolved.
Show resolved Hide resolved


@requires_cftime
def test_empty_cftimeindex_calendar_property():
index = CFTimeIndex([])
Expand Down
Loading