Skip to content

Commit c187db1

Browse files
committed
.
1 parent 1e0fc8d commit c187db1

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

xarray/core/dataset.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10342,15 +10342,34 @@ def resample(
1034210342
def drop_attrs(self) -> Self:
1034310343
"""
1034410344
Removes all attributes from the Dataset and its variables.
10345+
10346+
Returns
10347+
-------
10348+
Dataset
1034510349
"""
1034610350
# Remove attributes from the dataset
1034710351
self = self._replace(attrs={})
1034810352

1034910353
# Remove attributes from each variable in the dataset
1035010354
for var in self.variables:
10351-
# variables don't have a `._replace` method, so we copy and then remove. If
10352-
# we added a `._replace` method, we could use that instead.
10353-
self[var] = self[var].copy()
10354-
self[var].attrs = {}
10355+
# variables don't have a `._replace` method, so we copy and then remove
10356+
# attrs. If we added a `._replace` method, we could use that instead.
10357+
if var not in self.indexes:
10358+
self[var] = self[var].copy()
10359+
self[var].attrs = {}
10360+
10361+
new_idx_variables = {}
10362+
10363+
# Not sure this is the most elegant way of doing this, but it works.
10364+
# (Contributions welcome for a more general "map over all variables, including
10365+
# indexes" approach.)
10366+
for idx, idx_vars in self.xindexes.group_by_index():
10367+
# copy each coordinate variable of an index and drop their attrs
10368+
temp_idx_variables = {k: v.copy() for k, v in idx_vars.items()}
10369+
for v in temp_idx_variables.values():
10370+
v.attrs = {}
10371+
# maybe re-wrap the index object in new coordinate variables
10372+
new_idx_variables.update(idx.create_variables(temp_idx_variables))
10373+
self = self.assign(**new_idx_variables)
1035510374

1035610375
return self

xarray/tests/test_dataset.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from xarray.core import dtypes, indexing, utils
3939
from xarray.core.common import duck_array_ops, full_like
4040
from xarray.core.coordinates import Coordinates, DatasetCoordinates
41-
from xarray.core.indexes import Index, PandasIndex
41+
from xarray.core.indexes import Index, PandasIndex, PandasMultiIndex
4242
from xarray.core.pycompat import array_type, integer_types
4343
from xarray.core.utils import is_scalar
4444
from xarray.testing import _assert_internal_invariants
@@ -4366,10 +4366,15 @@ def test_drop_attrs(self) -> None:
43664366
# Doesn't change original
43674367
assert_identical(ds, original)
43684368

4369-
# Example with variables and coords with attrs, check they're dropped too
4369+
# Example with variables and coords with attrs, and a multiindex. (arguably
4370+
# should have used a canonical dataset with all the features we're should
4371+
# support...)
43704372
var = Variable("x", [1, 2, 3], attrs=dict(x=1, y=2))
43714373
idx = IndexVariable("y", [1, 2, 3], attrs=dict(c=1, d=2))
4372-
ds = Dataset(dict(var1=var), coords=dict(y=idx)).assign_attrs(a=1, b=2)
4374+
mx = xr.Coordinates.from_pandas_multiindex(
4375+
pd.MultiIndex.from_tuples([(1, 2), (3, 4)], names=["d", "e"]), "z"
4376+
)
4377+
ds = Dataset(dict(var1=var), coords=dict(y=idx, z=mx)).assign_attrs(a=1, b=2)
43734378
assert ds.coords["y"].attrs != {}
43744379

43754380
original = ds.copy(deep=True)

0 commit comments

Comments
 (0)