Skip to content

Commit cf03f4b

Browse files
committed
Add a deep kwarg (default True?)
1 parent 8baff44 commit cf03f4b

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

xarray/core/dataarray.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7147,16 +7147,17 @@ def to_dask_dataframe(
71477147
# https://mypy.readthedocs.io/en/latest/common_issues.html#dealing-with-conflicting-names
71487148
str = utils.UncachedAccessor(StringAccessor["DataArray"])
71497149

7150-
def drop_attrs(self) -> Self:
7150+
def drop_attrs(self, deep: bool = True) -> Self:
71517151
"""
71527152
Removes all attributes from the DataArray.
71537153
7154+
Parameters
7155+
----------
7156+
deep : bool, default True
7157+
Removes attributes from coordinates.
7158+
71547159
Returns
71557160
-------
71567161
DataArray
71577162
"""
7158-
self = self.copy()
7159-
7160-
self.attrs = {}
7161-
7162-
return self
7163+
return self._to_temp_dataset().drop_attrs(deep=deep).to_array()

xarray/core/dataset.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10339,17 +10339,25 @@ def resample(
1033910339
**indexer_kwargs,
1034010340
)
1034110341

10342-
def drop_attrs(self) -> Self:
10342+
def drop_attrs(self, deep: bool = True) -> Self:
1034310343
"""
1034410344
Removes all attributes from the Dataset and its variables.
1034510345
10346+
Parameters
10347+
----------
10348+
deep : bool, default True
10349+
Removes attributes from all variables.
10350+
1034610351
Returns
1034710352
-------
1034810353
Dataset
1034910354
"""
1035010355
# Remove attributes from the dataset
1035110356
self = self._replace(attrs={})
1035210357

10358+
if not deep:
10359+
return self
10360+
1035310361
# Remove attributes from each variable in the dataset
1035410362
for var in self.variables:
1035510363
# variables don't have a `._replace` method, so we copy and then remove
@@ -10359,7 +10367,6 @@ def drop_attrs(self) -> Self:
1035910367
self[var].attrs = {}
1036010368

1036110369
new_idx_variables = {}
10362-
1036310370
# Not sure this is the most elegant way of doing this, but it works.
1036410371
# (Should we have a more general "map over all variables, including
1036510372
# indexes" approach?)

xarray/tests/test_dataset.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4375,13 +4375,16 @@ def test_drop_attrs(self) -> None:
43754375
pd.MultiIndex.from_tuples([(1, 2), (3, 4)], names=["d", "e"]), "z"
43764376
)
43774377
ds = Dataset(dict(var1=var), coords=dict(y=idx, z=mx)).assign_attrs(a=1, b=2)
4378+
assert ds.attrs != {}
4379+
assert ds["var1"].attrs != {}
4380+
assert ds["y"].attrs != {}
43784381
assert ds.coords["y"].attrs != {}
43794382

43804383
original = ds.copy(deep=True)
43814384
result = ds.drop_attrs()
43824385

43834386
assert result.attrs == {}
4384-
assert result["x"].attrs == {}
4387+
assert result["var1"].attrs == {}
43854388
assert result["y"].attrs == {}
43864389
assert list(result.data_vars) == list(ds.data_vars)
43874390
assert list(result.coords) == list(ds.coords)
@@ -4392,6 +4395,14 @@ def test_drop_attrs(self) -> None:
43924395
# can't currently contain `attrs`, so we can't test those.)
43934396
assert ds.coords["y"].attrs != {}
43944397

4398+
# Test for deep=False
4399+
result_shallow = ds.drop_attrs(deep=False)
4400+
assert result_shallow.attrs == {}
4401+
assert result_shallow["var1"].attrs != {}
4402+
assert result_shallow["y"].attrs != {}
4403+
assert list(result.data_vars) == list(ds.data_vars)
4404+
assert list(result.coords) == list(ds.coords)
4405+
43954406
def test_assign_multiindex_level(self) -> None:
43964407
data = create_test_multiindex()
43974408
with pytest.raises(ValueError, match=r"cannot drop or update.*corrupt.*index "):

0 commit comments

Comments
 (0)