pydata / pydata/xarray

GroupBy.map with keep_attrs=True gives error

Open
#4,450 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-groupby topic-metadata
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

I wanted to apply a function (not as simple as np.mean in the example below) using xarray.map and keep the coordinate (lat) attributes. I got an error with keep_attrs=True that I didn't understand.

import numpy as np
import xarray as xr
ds =  xr.tutorial.open_dataset("air_temperature")
test_map = ds.groupby('lat').map(np.mean, keep_attrs=True)
test_map.lat
Error
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-e572326fea03> in <module>
      2 import xarray as xr
      3 ds =  xr.tutorial.open_dataset("air_temperature")
----> 4 test_map = ds.groupby('lat').map(np.mean, keep_attrs=True)
      5 test_map.lat

/opt/conda/lib/python3.8/site-packages/xarray/core/groupby.py in map(self, func, args, shortcut, **kwargs)
    921         # ignore shortcut if set (for now)
    922         applied = (func(ds, *args, **kwargs) for ds in self._iter_grouped())
--> 923         return self._combine(applied)
    924 
    925     def apply(self, func, args=(), shortcut=None, **kwargs):

/opt/conda/lib/python3.8/site-packages/xarray/core/groupby.py in _combine(self, applied)
    941     def _combine(self, applied):
    942         """Recombine the applied objects like the original."""
--> 943         applied_example, applied = peek_at(applied)
    944         coord, dim, positions = self._infer_concat_args(applied_example)
    945         combined = concat(applied, dim)

/opt/conda/lib/python3.8/site-packages/xarray/core/utils.py in peek_at(iterable)
    181     """
    182     gen = iter(iterable)
--> 183     peek = next(gen)
    184     return peek, itertools.chain([peek], gen)
    185 

/opt/conda/lib/python3.8/site-packages/xarray/core/groupby.py in <genexpr>(.0)
    920         """
    921         # ignore shortcut if set (for now)
--> 922         applied = (func(ds, *args, **kwargs) for ds in self._iter_grouped())
    923         return self._combine(applied)
    924 

<__array_function__ internals> in mean(*args, **kwargs)

TypeError: _mean_dispatcher() got an unexpected keyword argument 'keep_attrs'
Output of xr.show_versions() ``` INSTALLED VERSIONS ------------------ commit: None python: 3.8.5 | packaged by conda-forge | (default, Aug 21 2020, 18:21:27) [GCC 7.5.0] python-bits: 64 OS: Linux OS-release: 4.19.112+ machine: x86_64 processor: x86_64 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 libhdf5: 1.10.6 libnetcdf: 4.7.4

xarray: 0.16.0
pandas: 1.1.1
numpy: 1.19.1
scipy: 1.5.2
netCDF4: 1.5.4
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: 2.4.0
cftime: 1.2.1
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.20.0
distributed: 2.24.0
matplotlib: 3.2.2
cartopy: 0.18.0
seaborn: 0.10.1
numbagg: None
pint: None
setuptools: 49.6.0.post20200814
pip: 20.2.2
conda: 4.8.3
pytest: None
IPython: 7.17.0
sphinx: None

</details>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example at the GroupBy.map entry point shown in the traceback with the xarray tutorial air_temperature dataset and np.mean. Confirm the behavior of keep_attrs=True and define done as GroupBy.map accepting the option without passing it incompatibly to np.mean while retaining the lat coordinate attributes.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.