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..be494c4ae2b 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -276,4 +276,20 @@ 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.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