Skip to content

Commit

Permalink
Fix and add test for groupby_bins() isnan TypeError. (#3405)
Browse files Browse the repository at this point in the history
* Fix and add test for groupby_bins() isnan TypeError.

* Better testing

* black
  • Loading branch information
dnowacki-usgs authored and max-sixty committed Oct 17, 2019
1 parent 6cd50cc commit 0f7ab0e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0f7ab0e

Please sign in to comment.