From f5db2f03125e17a5e4cb38a10e9dc7318e6b4602 Mon Sep 17 00:00:00 2001 From: Dan Nowacki Date: Tue, 15 Oct 2019 16:57:41 -0700 Subject: [PATCH 1/3] Fix and add test for groupby_bins() isnan TypeError. --- xarray/core/groupby.py | 2 +- xarray/tests/test_groupby.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index c52b0dc97e6..43724e3c644 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -321,7 +321,7 @@ def __init__( full_index = None if bins is not None: - if np.isnan(bins).all(): + if duck_array_ops.isnull(bins).all(): raise ValueError("All bin edges are NaN.") binned = pd.cut(group.values, bins, **cut_kwargs) new_dim_name = group.name + "_bins" diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 957d4800c40..053138b7950 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -276,4 +276,15 @@ def test_groupby_grouping_errors(): dataset.to_array().groupby(dataset.foo * np.nan) +def test_groupby_bins_timeseries(): + ds = xr.Dataset() + ds["time"] = xr.DataArray( + pd.date_range("2010-08-01", "2010-08-15", freq="15min"), dims="time" + ) + ds["val"] = xr.DataArray(np.random.rand(*ds["time"].shape), dims="time") + ds.groupby_bins( + "time", pd.date_range(start="2010-08-01", end="2010-08-15", freq="24.8H") + ) + + # TODO: move other groupby tests from test_dataset and test_dataarray over here From 8eec204f8b288ad93a69f0485bec1f06292d5b02 Mon Sep 17 00:00:00 2001 From: Dan Nowacki Date: Wed, 16 Oct 2019 09:43:16 -0700 Subject: [PATCH 2/3] Better testing --- xarray/tests/test_groupby.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 053138b7950..2342b35983d 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -281,10 +281,14 @@ def test_groupby_bins_timeseries(): ds["time"] = xr.DataArray( pd.date_range("2010-08-01", "2010-08-15", freq="15min"), dims="time" ) - ds["val"] = xr.DataArray(np.random.rand(*ds["time"].shape), dims="time") - ds.groupby_bins( - "time", pd.date_range(start="2010-08-01", end="2010-08-15", freq="24.8H") - ) - + ds["val"] = xr.DataArray(np.ones(*ds["time"].shape), dims="time") + time_bins = pd.date_range(start="2010-08-01", end="2010-08-15", freq="24H") + actual = ds.groupby_bins("time", time_bins).sum() + expected = xr.DataArray( + 96 * np.ones((14,)), + dims=["time_bins"], + coords={"time_bins": pd.cut(time_bins, time_bins).categories}, + ).to_dataset(name="val") + assert_identical(actual, expected) # TODO: move other groupby tests from test_dataset and test_dataarray over here From 99fac7fb45762119ed5a113b0357cc6f0e5e7b87 Mon Sep 17 00:00:00 2001 From: Dan Nowacki Date: Wed, 16 Oct 2019 09:57:37 -0700 Subject: [PATCH 3/3] black --- xarray/tests/test_groupby.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 2342b35983d..be494c4ae2b 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -291,4 +291,5 @@ def test_groupby_bins_timeseries(): ).to_dataset(name="val") assert_identical(actual, expected) + # TODO: move other groupby tests from test_dataset and test_dataarray over here