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 quantile method to groupby object #3018

Closed
huard opened this issue Jun 12, 2019 · 4 comments · Fixed by #2828
Closed

Add quantile method to groupby object #3018

huard opened this issue Jun 12, 2019 · 4 comments · Fixed by #2828

Comments

@huard
Copy link
Contributor

huard commented Jun 12, 2019

Dataset and DataArray objects have a quantile method, but not GroupBy. This would be useful for climatological analyses.

@tommylees112
Copy link

This is an awesome addition thankyou! I updated my xarray to V:0.12.2 but I am still getting an error with dataset objects

a = np.ones((400)) * 10
a[3:7] = 0.2
a[10:13] = 0.2
p = np.repeat(a, 25).reshape(400, 5, 5)
lat = np.arange(0, 5)
lon = np.arange(0, 5)
time = pd.date_range('2000-01-01', freq='M', periods=p.shape[0])
d = xr.Dataset(
    {'precip': (['time', 'lat', 'lon'], p)},
    coords={
        'lon': lon,
        'lat': lat,
        'time': time
    }
)

d.groupby('time.month').quantile(q=0.1)

gives the error message

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-38-276cfe319286> in <module>
----> 1 d.groupby('time.month').quantile(q=0.1)

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

Whereas for the DataArray it works fine.

d.precip.groupby('time.month').quantile(q=0.1)

Out[39]:
<xarray.DataArray 'precip' (month: 12)>
array([10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.])
Coordinates:
  * month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12

Is this the expected behaviour?

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.0 | packaged by conda-forge | (default, Nov 12 2018, 12:34:36)
[Clang 4.0.1 (tags/RELEASE_401/final)]
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.6.2

xarray: 0.12.2
pandas: 0.24.2
numpy: 1.16.4
scipy: 1.3.0
netCDF4: 1.5.1.2
pydap: None
h5netcdf: None
h5py: 2.9.0
Nio: None
zarr: None
cftime: 1.0.3.4
nc_time_axis: None
PseudonetCDF: None
rasterio: 1.0.17
cfgrib: 0.9.7
iris: None
bottleneck: 1.2.1
dask: 1.2.2
distributed: 1.28.1
matplotlib: 3.1.0
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 41.0.1
pip: 19.1
conda: None
pytest: 4.5.0
IPython: 7.1.1
sphinx: 2.0.1

@huard
Copy link
Contributor Author

huard commented Jul 2, 2019

The PR only covered DataArrays, not Datasets, so yes, this is expected.

@dcherian
Copy link
Contributor

dcherian commented Jul 2, 2019

Well that was an oversight. Any interest in opening a PR @tommylees112 or @huard

@dcherian
Copy link
Contributor

dcherian commented Jul 2, 2019

OK looks like the method was added to the DataArrayGroupBy class not GroupBy. I should've caught this at review. Just moving the function definition to GroupBy seems to make things work

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

Successfully merging a pull request may close this issue.

3 participants