Skip to content

Commit

Permalink
use safe_cast_to_index to sanitize DataArrays for groupby (pydata#1133)
Browse files Browse the repository at this point in the history
* groupby should pass ndarray to unique_value_groups pydata#1132

* use safe_cast_to_index to sanitize DataArrays for groupby pydata#1133

* add test for pydata#1133

* correct issue number

* minor cleanup

* add note to whats new

* fix formatting

* repair Marco's umlaut

* More descriptive what's new note
  • Loading branch information
j08lue authored and shoyer committed Dec 19, 2016
1 parent d31cb80 commit 34fd2b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ Bug fixes
- Fixed sub-optimal performance in certain operations with object arrays (:issue:`1121`).
By `Yves Delley <https://github.com/burnpanck>`_.

- Fixed a bug whith facetgrid (the ``norm`` keyword was ignored, :issue:`1159`).
- Fix ``.groupby(group)`` when ``group`` has datetime dtype (:issue:`1132`).
By `Jonas Sølvsteen <https://github.com/j08lue>`_.

- Fixed a bug with facetgrid (the ``norm`` keyword was ignored, :issue:`1159`).
By `Fabien Maussion <https://github.com/fmaussion>`_.

.. _whats-new.0.8.2:
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
else:
# look through group to find the unique values
unique_values, group_indices = unique_value_groups(
group, sort=(bins is None))
safe_cast_to_index(group), sort=(bins is None))
unique_coord = IndexVariable(group.name, unique_values)

# specification for the groupby operation
Expand Down
18 changes: 18 additions & 0 deletions xarray/test/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import division
from __future__ import print_function
import numpy as np
import pandas as pd
import xarray as xr
from xarray.core.groupby import _consolidate_slices

Expand Down Expand Up @@ -46,11 +47,28 @@ def test_multi_index_groupby_sum():
assert expected.equals(actual)


def test_groupby_da_datetime():
# test groupby with a DataArray of dtype datetime for GH1132
# create test data
times = pd.date_range('2000-01-01', periods=4)
foo = xr.DataArray([1,2,3,4], coords=dict(time=times), dims='time')
# create test index
dd = times.to_datetime()
reference_dates = [dd[0], dd[2]]
labels = reference_dates[0:1]*2 + reference_dates[1:2]*2
ind = xr.DataArray(labels, coords=dict(time=times), dims='time', name='reference_date')
g = foo.groupby(ind)
actual = g.sum(dim='time')
expected = xr.DataArray([3,7], coords=dict(reference_date=reference_dates), dims='reference_date')
assert actual.equals(expected)


def test_groupby_duplicate_coordinate_labels():
# fix for http://stackoverflow.com/questions/38065129
array = xr.DataArray([1, 2, 3], [('x', [1, 1, 2])])
expected = xr.DataArray([3, 3], [('x', [1, 2])])
actual = array.groupby('x').sum()
assert expected.equals(actual)


# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 comments on commit 34fd2b6

Please sign in to comment.