Skip to content

Commit 0f7ab0e

Browse files
dnowacki-usgsmax-sixty
authored andcommitted
Fix and add test for groupby_bins() isnan TypeError. (#3405)
* Fix and add test for groupby_bins() isnan TypeError. * Better testing * black
1 parent 6cd50cc commit 0f7ab0e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

xarray/core/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def __init__(
321321
full_index = None
322322

323323
if bins is not None:
324-
if np.isnan(bins).all():
324+
if duck_array_ops.isnull(bins).all():
325325
raise ValueError("All bin edges are NaN.")
326326
binned = pd.cut(group.values, bins, **cut_kwargs)
327327
new_dim_name = group.name + "_bins"

xarray/tests/test_groupby.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,20 @@ def test_groupby_grouping_errors():
276276
dataset.to_array().groupby(dataset.foo * np.nan)
277277

278278

279+
def test_groupby_bins_timeseries():
280+
ds = xr.Dataset()
281+
ds["time"] = xr.DataArray(
282+
pd.date_range("2010-08-01", "2010-08-15", freq="15min"), dims="time"
283+
)
284+
ds["val"] = xr.DataArray(np.ones(*ds["time"].shape), dims="time")
285+
time_bins = pd.date_range(start="2010-08-01", end="2010-08-15", freq="24H")
286+
actual = ds.groupby_bins("time", time_bins).sum()
287+
expected = xr.DataArray(
288+
96 * np.ones((14,)),
289+
dims=["time_bins"],
290+
coords={"time_bins": pd.cut(time_bins, time_bins).categories},
291+
).to_dataset(name="val")
292+
assert_identical(actual, expected)
293+
294+
279295
# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 commit comments

Comments
 (0)