Skip to content

Commit

Permalink
Assign default group name in groupby if name=None (#158) (#4098)
Browse files Browse the repository at this point in the history
* Assign default group name in groupby (#158)

* When groupby receives a DataArray with name=None assign name='group'

* Previously when name=None a ValueError: `group` must have a name was raised

* Closes #158

* Add test

* Update whats-new.rst

* black

* Add assert statement to test group name was added to DataArray

Co-authored-by: phillipbutcher <phillip.butcher@noaa.gov>
  • Loading branch information
pjbutcher and phillipbutcher authored May 30, 2020
1 parent a2e9804 commit 73b013f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ New Features

Bug fixes
~~~~~~~~~
- If groupby receives a ``DataArray`` with name=None, assign a default name (:issue:`158`)
By `Phil Butcher <https://github.com/pjbutcher>`_.
- Support dark mode in VS code (:issue:`4024`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- Fix bug when converting multiindexed Pandas objects to sparse xarray objects. (:issue:`4019`)
Expand Down
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__(
group = _DummyGroup(obj, group.name, group.coords)

if getattr(group, "name", None) is None:
raise ValueError("`group` must have a name")
group.name = "group"

group, obj, stacked_dim, inserted_dims = _ensure_1d(group, obj)
(group_dim,) = group.dims
Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,16 @@ def test_groupby_bins_timeseries():
assert_identical(actual, expected)


def test_groupby_none_group_name():
# GH158
# xarray should not fail if a DataArray's name attribute is None

data = np.arange(10) + 10
da = xr.DataArray(data) # da.name = None
key = xr.DataArray(np.floor_divide(data, 2))

mean = da.groupby(key).mean()
assert "group" in mean.dims


# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 comments on commit 73b013f

Please sign in to comment.