question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

xr.Dataset.map drops attrs of DataArray

See original GitHub issue

MCVE Code Sample

import xarray as xr
import numpy as np

ds = xr.DataArray([1, 2], attrs=dict(tst="DataArray")).to_dataset(name="data")
ds.attrs["tst"] = "Dataset"
ds.map(np.mean, keep_attrs=True).data

returns

<xarray.DataArray 'data' ()>
array(1.5)

Expected Output

<xarray.DataArray 'data' ()>
array(1.5)
Attributes:
    tst:      DataArray

Problem Description

Applying xr.Dataset.map(..., keep_attrs=True) does not retain the attributes of the DataArrays. Should it?

In constrast ds.mean(keep_attrs=True) retains DataArray-level attrs.

EDIT: corrected example

Output of xr.show_versions()

INSTALLED VERSIONS

commit: bd4f048bdb5a5a356a5603904d96a676037d1b6e python: 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 4.12.14-lp151.28.32-default machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_US.UTF-8 libhdf5: 1.10.5 libnetcdf: 4.6.2

xarray: 0.14.0+117.gbd4f048b.dirty pandas: 0.25.2 numpy: 1.17.3 scipy: 1.3.1 netCDF4: 1.5.1.2 pydap: installed h5netcdf: 0.7.4 h5py: 2.10.0 Nio: 1.5.5 zarr: 2.3.2 cftime: 1.0.4.2 nc_time_axis: 1.2.0 PseudoNetCDF: installed rasterio: 1.1.0 cfgrib: 0.9.7.2 iris: 2.2.0 bottleneck: 1.2.1 dask: 2.6.0 distributed: 2.6.0 matplotlib: 3.1.1 cartopy: 0.17.0 seaborn: 0.9.0 numbagg: installed setuptools: 41.6.0.post20191029 pip: 19.3.1 conda: None pytest: 5.2.2 IPython: 7.9.0 sphinx: None

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
max-sixtycommented, Dec 6, 2019

(forgive me, am on vacation)

I actually didn’t know about the multiple levels of attrs. Given that does exist, I agree with @dcherian re keep_attrs impacting individual variable attrs too.

While it is a behavior change, I think it’s fine: the current behavior is somewhere between a bug and an undocumented artefact. Unless others see a more material change here?

0reactions
TomNicholascommented, Dec 13, 2019

When I wrote the (set_options) keep_attrs switch in then I intended it to function as a global switch, so ensuring that individual DataArray attrs are also propagated fits with that intention. I just apparently never thought of this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

xarray.Dataset.map
keep_attrs ( bool or None , optional) – If True, both the dataset's and variables' attributes ( attrs ) will be copied from...
Read more >
API reference — xarray 0.10.6 documentation
Given any number of Dataset and/or DataArray objects, returns new objects with aligned indexes and dimension sizes. broadcast (*args, **kwargs), Explicitly ...
Read more >
DataArray deletes Attributes in simple computation
You can set global options in xarray with xr.set_options : ... Note that since Dataset and DataArray attributes are simply dictionaries, ...
Read more >
Basic data structures of xarray - Towards Data Science
In many fields of science, there's a need to map a data point to various ... We'll create a pandas Series and then...
Read more >
Source code for cf_xarray.accessor - CF Xarray
... Union, cast, ) import xarray as xr from xarray import DataArray, Dataset from ... Dataset, attr: str, accessor: CFAccessor, key_mappers: Mapping[str, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found