Skip to content
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 @@ -41,6 +41,8 @@ Bug Fixes
By `Ian Hunt-Isaak <https://github.com/ianhi>`_.
- Always normalize slices when indexing ``LazilyIndexedArray`` instances (:issue:`10941`, :pull:`10948`).
By `Justus Magin <https://github.com/keewis>`_.
- Avoid casting custom indexes in ``Dataset.drop_attrs`` (:pull:`10961`)
By `Justus Magin <https://github.com/keewis>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10475,7 +10475,7 @@ def drop_attrs(self, *, deep: bool = True) -> Self:
for var in self.variables:
# variables don't have a `._replace` method, so we copy and then remove
# attrs. If we added a `._replace` method, we could use that instead.
if var not in self.indexes:
if var not in self.xindexes:
self[var] = self[var].copy()
self[var].attrs = {}

Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4868,6 +4868,19 @@ def test_drop_attrs(self) -> None:
assert list(result.data_vars) == list(ds.data_vars)
assert list(result.coords) == list(ds.coords)

def test_drop_attrs_custom_index(self):
class CustomIndex(Index):
@classmethod
def from_variables(cls, variables, *, options=None):
return cls()

ds = xr.Dataset(coords={"y": ("x", [1, 2])}).set_xindex("y", CustomIndex)
# should not raise a TypeError
ds.drop_attrs()

# make sure the index didn't disappear
assert "y" in ds.xindexes

def test_assign_multiindex_level(self) -> None:
data = create_test_multiindex()
with pytest.raises(ValueError, match=r"cannot drop or update.*corrupt.*index "):
Expand Down
Loading