From 34fd2b6cb94dfb824c5371c37b6eb5e70a88260f Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Dec 2016 18:11:57 +0100 Subject: [PATCH] use safe_cast_to_index to sanitize DataArrays for groupby (#1133) * groupby should pass ndarray to unique_value_groups #1132 * use safe_cast_to_index to sanitize DataArrays for groupby #1133 * add test for #1133 * correct issue number * minor cleanup * add note to whats new * fix formatting * repair Marco's umlaut * More descriptive what's new note --- doc/whats-new.rst | 5 ++++- xarray/core/groupby.py | 2 +- xarray/test/test_groupby.py | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6b7bf3238c3..350b472af56 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -199,7 +199,10 @@ Bug fixes - Fixed sub-optimal performance in certain operations with object arrays (:issue:`1121`). By `Yves Delley `_. -- 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 `_. + +- Fixed a bug with facetgrid (the ``norm`` keyword was ignored, :issue:`1159`). By `Fabien Maussion `_. .. _whats-new.0.8.2: diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 48ebbab2a30..3fe91c27108 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -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 diff --git a/xarray/test/test_groupby.py b/xarray/test/test_groupby.py index f4b96af74ef..9a153e45da0 100644 --- a/xarray/test/test_groupby.py +++ b/xarray/test/test_groupby.py @@ -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 @@ -46,6 +47,22 @@ 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])]) @@ -53,4 +70,5 @@ def test_groupby_duplicate_coordinate_labels(): actual = array.groupby('x').sum() assert expected.equals(actual) + # TODO: move other groupby tests from test_dataset and test_dataarray over here