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

Keep attributes across operations - "drop_conflicts" option? #8105

Open
Sevans711 opened this issue Aug 23, 2023 · 1 comment
Open

Keep attributes across operations - "drop_conflicts" option? #8105

Sevans711 opened this issue Aug 23, 2023 · 1 comment
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@Sevans711
Copy link

What is your issue?

The Problem
When I perform simple operations (e.g., +, -, *, /) on DataArray objects, I expected compatible attributes to be maintained, and incompatible attributes to be removed. Even when playing with the available options for keep_attrs, this behavior does not occur. See also #4763 and #2582.

Here is an example showing what happens in each case:

da1 = xr.DataArray(1, attrs=dict(unit_system='SI', a=7))
da2 = xr.DataArray(2, attrs=dict(unit_system='SI', a=8))
with xr.set_options(keep_attrs='default'):
    result = da1 + da2
    result.attrs    # {}
with xr.set_options(keep_attrs=True):
    result = da1 + da2
    result.attrs    # {'unit_system': 'SI', 'a': 7}

Desired behavior:

    result.attrs    # {'unit_system': 'SI'}

That is to say, I want attributes with conflicting values to be discarded, and all other attributes to be maintained.

Suggested Fix
This is similar to the "drop_conflicts" option for merging attrs, from xarray.merge. I suggest that "drop_conflicts" be added as an option for keep_attrs. For example, I would like to be able to do:

with xr.set_options(keep_attrs="drop_conflicts"):
    result = da1 + da2
    result.attrs     # {'unit_system': 'SI'}

If anyone else is able to add this option into the code, that would be amazing!

Or, if there is already a way to accomplish this behavior, I would appreciate any advice on how to do it.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.9.13 (main, Aug 25 2022, 18:29:29) [Clang 12.0.0 ] python-bits: 64 OS: Darwin OS-release: 21.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.10.6 libnetcdf: None

xarray: 2023.8.0
pandas: 1.4.4
numpy: 1.21.5
scipy: 1.9.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: 3.7.0
Nio: None
zarr: 2.13.6
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: 1.3.5
dask: 2022.7.0
distributed: 2022.7.0
matplotlib: 3.5.2
cartopy: None
seaborn: 0.11.2
numbagg: None
fsspec: 2022.7.1
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 63.4.1
pip: 22.2.2
conda: 22.9.0
pytest: 7.1.2
mypy: None
IPython: 7.31.1
sphinx: 5.0.2

@Sevans711 Sevans711 added the needs triage Issue that has not been reviewed by xarray team member label Aug 23, 2023
@welcome
Copy link

welcome bot commented Aug 23, 2023

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!

@dcherian dcherian added topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) and removed needs triage Issue that has not been reviewed by xarray team member labels Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)
Projects
None yet
Development

No branches or pull requests

2 participants