Skip to content

Commit f957eb8

Browse files
committed
bin coordinate name changed
1 parent d614246 commit f957eb8

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

xarray/core/groupby.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
189189
raise TypeError("Can't specify both `grouper` and `bins`.")
190190
if bins is not None:
191191
group = DataArray(pd.cut(group.values, bins, **cut_kwargs),
192-
group.coords, name=group.name)
192+
group.coords, name=group.name + '_bins')
193+
# RPA: we want to restore the original coordinates at some point!
193194
if grouper is not None:
194195
index = safe_cast_to_index(group)
195196
if not index.is_monotonic:
@@ -209,7 +210,6 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
209210
# assume that group already has sorted, unique values
210211
# (if using bins, the group will have the same name as a dimension
211212
# but different values)
212-
print('assume that group already has sorted, unique values')
213213
if group.dims != (group.name,):
214214
raise ValueError('`group` is required to be a coordinate if '
215215
'`group.name` is a dimension in `obj`')
@@ -429,7 +429,8 @@ def lookup_order(dimension):
429429

430430
def _restore_multiindex(self, combined):
431431
if self._stacked_dim is not None and self._stacked_dim in combined.dims:
432-
combined[self._stacked_dim] = self.group[self._stacked_dim]
432+
stacked_dim = self.group[self._stacked_dim]
433+
combined[self._stacked_dim] = stacked_dim
433434
return combined
434435

435436
def apply(self, func, shortcut=False, **kwargs):

xarray/test/test_dataarray.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,8 @@ def test_groupby_bins(self):
13351335
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html
13361336
bins = [0,1.5,5]
13371337
bin_coords = ['(0, 1.5]', '(1.5, 5]']
1338-
expected = DataArray([1,5], dims='dim_0', coords={'dim_0': bin_coords})
1338+
expected = DataArray([1,5], dims='dim_0_bins',
1339+
coords={'dim_0_bins': bin_coords})
13391340
# the problem with this is that it overwrites the dimensions of array!
13401341
#actual = array.groupby('dim_0', bins=bins).sum()
13411342
actual = array.groupby_bins('dim_0', bins).apply(
@@ -1349,13 +1350,15 @@ def test_groupby_bins_multidim(self):
13491350
array = self.make_groupby_multidim_example_array()
13501351
bins = [0,15,20]
13511352
bin_coords = ['(0, 15]', '(15, 20]']
1352-
expected = DataArray([16, 40], dims='lat', coords={'lat': bin_coords})
1353+
expected = DataArray([16, 40], dims='lat_bins',
1354+
coords={'lat_bins': bin_coords})
13531355
actual = array.groupby_bins('lat', bins).apply(
13541356
lambda x : x.sum(), shortcut=False)
13551357
self.assertDataArrayIdentical(expected, actual)
13561358
# modify the array coordinates to be non-monotonic after unstacking
13571359
array['lat'].data = np.array([[10., 20.], [20., 10.]])
1358-
expected = DataArray([28, 28], dims='lat', coords={'lat': bin_coords})
1360+
expected = DataArray([28, 28], dims='lat_bins',
1361+
coords={'lat_bins': bin_coords})
13591362
actual = array.groupby_bins('lat', bins).apply(
13601363
lambda x : x.sum(), shortcut=False)
13611364
self.assertDataArrayIdentical(expected, actual)

0 commit comments

Comments
 (0)