Skip to content

Commit 321c560

Browse files
authored
fix DataArray groupby returning a Dataset (#6394)
1 parent c604ee1 commit 321c560

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

xarray/core/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def _combine(self, applied, shortcut=False):
866866
if coord is not None and dim not in applied_example.dims:
867867
index, index_vars = create_default_index_implicit(coord)
868868
indexes = {k: index for k in index_vars}
869-
combined = combined._overwrite_indexes(indexes, coords=index_vars)
869+
combined = combined._overwrite_indexes(indexes, index_vars)
870870
combined = self._maybe_restore_empty_groups(combined)
871871
combined = self._maybe_unstack(combined)
872872
return combined

xarray/tests/test_groupby.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,9 +936,17 @@ def test_groupby_dataset_assign():
936936

937937
def test_groupby_dataset_map_dataarray_func():
938938
# regression GH6379
939-
ds = xr.Dataset({"foo": ("x", [1, 2, 3, 4])}, coords={"x": [0, 0, 1, 1]})
939+
ds = Dataset({"foo": ("x", [1, 2, 3, 4])}, coords={"x": [0, 0, 1, 1]})
940940
actual = ds.groupby("x").map(lambda grp: grp.foo.mean())
941-
expected = xr.DataArray([1.5, 3.5], coords={"x": [0, 1]}, dims="x", name="foo")
941+
expected = DataArray([1.5, 3.5], coords={"x": [0, 1]}, dims="x", name="foo")
942+
assert_identical(actual, expected)
943+
944+
945+
def test_groupby_dataarray_map_dataset_func():
946+
# regression GH6379
947+
da = DataArray([1, 2, 3, 4], coords={"x": [0, 0, 1, 1]}, dims="x", name="foo")
948+
actual = da.groupby("x").map(lambda grp: grp.mean().to_dataset())
949+
expected = xr.Dataset({"foo": ("x", [1.5, 3.5])}, coords={"x": [0, 1]})
942950
assert_identical(actual, expected)
943951

944952

0 commit comments

Comments
 (0)