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

DataArray().drop_attrs(deep=False) doesn't do anything #10027

Open
j-haacker opened this issue Feb 5, 2025 · 4 comments
Open

DataArray().drop_attrs(deep=False) doesn't do anything #10027

j-haacker opened this issue Feb 5, 2025 · 4 comments

Comments

@j-haacker
Copy link

j-haacker commented Feb 5, 2025

[Edit] I didn't see any issue templates because I took the route via opening an issue linked to a line of code, which seems to have failed anyway.

I tried to drop DataArray attributes without dropping the coordinate attributes. For this, I set deep=False in the dataarray's method .drop_attrs(). This did not work.

Environment

INSTALLED VERSIONS
------------------
commit: None
python: 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.8.0-52-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.4-development

xarray: 2025.1.1
pandas: 2.2.3
numpy: 2.2.2
scipy: 1.15.1
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: 3.0.1
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.1.0
distributed: None
matplotlib: 3.10.0
cartopy: 0.24.1
seaborn: None
numbagg: None
fsspec: 2024.12.0
cupy: None
pint: 0.24.4
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.8.0
pip: 25.0
conda: None
pytest: 8.3.4
mypy: None
IPython: 8.31.0
sphinx: None

Minimal example to reproduce

import xarray as xr
a = xr.DataArray(0).assign_attrs({"b": "c"})
a = a.drop_attrs(deep=False)
print(a)

results in

<xarray.DataArray ()> Size: 8B
array(0)
Attributes:
    b:        c

The attributes should have been dropped but they are still present.


my initial guess

[Edit] removed, was wrong, see below

Copy link

welcome bot commented Feb 5, 2025

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@j-haacker
Copy link
Author

I see now that should have opened the issue from the issues tab. If required, I can close this and open a new issue using the template.

@j-haacker
Copy link
Author

My initial guess, that the dataarray attributes survived in a copy of itself, was wrong. I believe the answer is much simpler:

I believe that in

self._to_temp_dataset().drop_attrs(deep=deep).pipe(self._from_temp_dataset)
_to_temp_dataset() wraps the variable in a Dataset, consequently .drop_attrs() only drops the dataset, but not the dataarray attributes if deep=False - see
if not deep:

@j-haacker
Copy link
Author

I will leave it here because I can't think of a satisfying solution.

Allowing the argument deep to be a Literal meant to specify a depth level seems odd to me.

Changing Dataset().drop_attrs() to drop variable attributes, even with deep=False, would change the behavior in general and should not be done.

Using a new _to_temp_dataarray() method seems like an overkill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant