Skip to content

Commit

Permalink
bin coordinate name changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rabernat committed Jul 6, 2016
1 parent d614246 commit f957eb8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
raise TypeError("Can't specify both `grouper` and `bins`.")
if bins is not None:
group = DataArray(pd.cut(group.values, bins, **cut_kwargs),
group.coords, name=group.name)
group.coords, name=group.name + '_bins')
# RPA: we want to restore the original coordinates at some point!
if grouper is not None:
index = safe_cast_to_index(group)
if not index.is_monotonic:
Expand All @@ -209,7 +210,6 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
# assume that group already has sorted, unique values
# (if using bins, the group will have the same name as a dimension
# but different values)
print('assume that group already has sorted, unique values')
if group.dims != (group.name,):
raise ValueError('`group` is required to be a coordinate if '
'`group.name` is a dimension in `obj`')
Expand Down Expand Up @@ -429,7 +429,8 @@ def lookup_order(dimension):

def _restore_multiindex(self, combined):
if self._stacked_dim is not None and self._stacked_dim in combined.dims:
combined[self._stacked_dim] = self.group[self._stacked_dim]
stacked_dim = self.group[self._stacked_dim]
combined[self._stacked_dim] = stacked_dim
return combined

def apply(self, func, shortcut=False, **kwargs):
Expand Down
9 changes: 6 additions & 3 deletions xarray/test/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,8 @@ def test_groupby_bins(self):
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html
bins = [0,1.5,5]
bin_coords = ['(0, 1.5]', '(1.5, 5]']
expected = DataArray([1,5], dims='dim_0', coords={'dim_0': bin_coords})
expected = DataArray([1,5], dims='dim_0_bins',
coords={'dim_0_bins': bin_coords})
# the problem with this is that it overwrites the dimensions of array!
#actual = array.groupby('dim_0', bins=bins).sum()
actual = array.groupby_bins('dim_0', bins).apply(
Expand All @@ -1349,13 +1350,15 @@ def test_groupby_bins_multidim(self):
array = self.make_groupby_multidim_example_array()
bins = [0,15,20]
bin_coords = ['(0, 15]', '(15, 20]']
expected = DataArray([16, 40], dims='lat', coords={'lat': bin_coords})
expected = DataArray([16, 40], dims='lat_bins',
coords={'lat_bins': bin_coords})
actual = array.groupby_bins('lat', bins).apply(
lambda x : x.sum(), shortcut=False)
self.assertDataArrayIdentical(expected, actual)
# modify the array coordinates to be non-monotonic after unstacking
array['lat'].data = np.array([[10., 20.], [20., 10.]])
expected = DataArray([28, 28], dims='lat', coords={'lat': bin_coords})
expected = DataArray([28, 28], dims='lat_bins',
coords={'lat_bins': bin_coords})
actual = array.groupby_bins('lat', bins).apply(
lambda x : x.sum(), shortcut=False)
self.assertDataArrayIdentical(expected, actual)
Expand Down

0 comments on commit f957eb8

Please sign in to comment.