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

Add groupby.pipe? #3782

Closed
max-sixty opened this issue Feb 20, 2020 · 4 comments
Closed

Add groupby.pipe? #3782

max-sixty opened this issue Feb 20, 2020 · 4 comments

Comments

@max-sixty
Copy link
Collaborator

MCVE Code Sample

In [1]: import xarray as xr                                                                                                                                         

In [3]: import numpy as np                                                                                                                                          

In [4]: ds = xr.Dataset( 
   ...:       {"foo": (("x", "y"), np.random.rand(4, 3))}, 
   ...:       coords={"x": [10, 20, 30, 40], "letters": ("x", list("abba"))}, 
   ...: )                                                                                                                                                      

In [5]: ds.groupby('letters')                                                                                                                                       
Out[5]: 
DatasetGroupBy, grouped over 'letters' 
2 groups with labels 'a', 'b'.

In [8]: ds.groupby('letters').sum(...) / ds.groupby('letters').count(...)                                                                                           
Out[8]: 
<xarray.Dataset>
Dimensions:  (letters: 2)
Coordinates:
  * letters  (letters) object 'a' 'b'
Data variables:
    foo      (letters) float64 0.4182 0.4995

In [9]: ds.groupby('letters').pipe(lambda x: x.sum() / x.count())                                                                                                   
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c9b142ea051b> in <module>
----> 1 ds.groupby('letters').pipe(lambda x: x.sum() / x.count())

AttributeError: 'DatasetGroupBy' object has no attribute 'pipe'

Expected Output

I think we could add groupby.pipe, as a convenience?

Output of xr.show_versions()

In [12]: xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8 (default, Aug 7 2019, 17:28:10)
[...]
python-bits: 64
OS: Linux
OS-release: [...]
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.utf8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: None

xarray: 0.14.1
pandas: 0.25.3
numpy: 1.18.1
scipy: 1.4.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.1.2
cartopy: None
seaborn: 0.10.0
numbagg: None
setuptools: 45.0.0
pip: 20.0.2
conda: None
pytest: 5.3.2
IPython: 7.11.1
sphinx: 2.3.1

@dcherian
Copy link
Contributor

How is this different from map or apply

@max-sixty
Copy link
Collaborator Author

Pipe operates on the whole object, not on each group; consistent with the other objects like Dataset

@max-sixty
Copy link
Collaborator Author

I think I see your point @dcherian: given that groupby.map also reduces, and there isn't much else you can do with a groupby, this probably isn't needed...

@max-sixty
Copy link
Collaborator Author

Closing for now. Given almost any useful groupby method maps-and-reduces the groups, map covers almost all use cases.

(ref #1251).

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

2 participants