diff --git a/doc/data-structures.rst b/doc/data-structures.rst index 74617a43340..947c09db7d6 100644 --- a/doc/data-structures.rst +++ b/doc/data-structures.rst @@ -368,9 +368,10 @@ Transforming datasets In addition to dictionary-like methods (described above), xarray has additional methods (like pandas) for transforming datasets into new objects. -For removing variables, you can select and drop an explicit list of variables -by indexing with a list of names or using the :py:meth:`~xarray.Dataset.drop` -methods to return a new ``Dataset``. These operations keep around coordinates: +For removing variables, you can select and drop an explicit list of +variables by indexing with a list of names or using the +:py:meth:`~xray.Dataset.drop` methods to return a new ``Dataset``. These +operations keep around coordinates: .. ipython:: python diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a6567193951..9c6c6a6089b 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -75,6 +75,8 @@ By `Robin Wilson `_. Bug fixes ~~~~~~~~~ +- ``groupby_bins`` now restores empty bins by default (:issue:`1019`). + By `Ryan Abernathey `_. - Fix issues for dates outside the valid range of pandas timestamps (:issue:`975`). By `Mathias Hauser `_. diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 7b925af2105..2c03cf5421f 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -154,6 +154,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None, specified bins by `pandas.cut`. cut_kwargs : dict, optional Extra keyword arguments to pass to `pandas.cut` + """ from .dataset import as_dataset from .dataarray import DataArray @@ -193,6 +194,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None, binned = pd.cut(group.values, bins, **cut_kwargs) new_dim_name = group.name + '_bins' group = DataArray(binned, group.coords, name=new_dim_name) + full_index = binned.categories if grouper is not None: index = safe_cast_to_index(group) if not index.is_monotonic: diff --git a/xarray/test/test_dataarray.py b/xarray/test/test_dataarray.py index f613259f128..79b5c893039 100644 --- a/xarray/test/test_dataarray.py +++ b/xarray/test/test_dataarray.py @@ -1416,6 +1416,18 @@ def test_groupby_bins(self): # (would fail with shortcut=True above) self.assertEqual(len(array.dim_0), 4) + def test_groupby_bins_empty(self): + array = DataArray(np.arange(4), dims='dim_0') + # one of these bins will be empty + bins = [0,4,5] + actual = array.groupby_bins('dim_0', bins).sum() + expected = DataArray([6, np.nan], dims='dim_0_bins', + coords={'dim_0_bins': ['(0, 4]','(4, 5]']}) + self.assertDataArrayIdentical(expected, actual) + # make sure original array is unchanged + # (was a problem in earlier versions) + self.assertEqual(len(array.dim_0), 4) + def test_groupby_bins_multidim(self): array = self.make_groupby_multidim_example_array() bins = [0,15,20]