Skip to content
forked from pydata/xarray

Commit c2e576e

Browse files
committed
Fix first, last
1 parent 6d8e822 commit c2e576e

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

xarray/core/groupby.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
safe_cast_to_index,
3434
)
3535
from xarray.core.options import _get_keep_attrs
36-
from xarray.core.pycompat import integer_types
3736
from xarray.core.types import Dims, QuantileMethods, T_DataArray, T_Xarray
3837
from xarray.core.utils import (
3938
either_dict_or_kwargs,
@@ -1296,7 +1295,11 @@ def where(self, cond, other=dtypes.NA) -> T_Xarray:
12961295
return ops.where_method(self, cond, other)
12971296

12981297
def _first_or_last(self, op, skipna, keep_attrs):
1299-
if isinstance(self._group_indices[0], integer_types):
1298+
if all(
1299+
isinstance(maybe_slice, slice)
1300+
and (maybe_slice.stop == maybe_slice.start + 1)
1301+
for maybe_slice in self._group_indices
1302+
):
13001303
# NB. this is currently only used for reductions along an existing
13011304
# dimension
13021305
return self._obj

xarray/tests/test_groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1099,14 +1099,14 @@ def test_stack_groupby_unsorted_coord(self):
10991099
y_vals = [2, 3]
11001100

11011101
arr = xr.DataArray(data, dims=dims, coords={"y": y_vals})
1102-
actual1 = arr.stack(z=dims).groupby("z", squeeze=False).first()
1102+
actual1 = arr.stack(z=dims).groupby("z").first()
11031103
midx1 = pd.MultiIndex.from_product([[0, 1], [2, 3]], names=dims)
11041104
expected1 = xr.DataArray(data_flat, dims=["z"], coords={"z": midx1})
11051105
assert_equal(actual1, expected1)
11061106

11071107
# GH: 3287. Note that y coord values are not in sorted order.
11081108
arr = xr.DataArray(data, dims=dims, coords={"y": y_vals[::-1]})
1109-
actual2 = arr.stack(z=dims).groupby("z", squeeze=False).first()
1109+
actual2 = arr.stack(z=dims).groupby("z").first()
11101110
midx2 = pd.MultiIndex.from_product([[0, 1], [3, 2]], names=dims)
11111111
expected2 = xr.DataArray(data_flat, dims=["z"], coords={"z": midx2})
11121112
assert_equal(actual2, expected2)
@@ -1420,7 +1420,7 @@ def test_groupby_first_and_last(self):
14201420
actual = array.groupby(by).first()
14211421
assert_identical(expected, actual)
14221422

1423-
actual = array.groupby("x", squeeze=False).first()
1423+
actual = array.groupby("x").first()
14241424
expected = array # should be a no-op
14251425
assert_identical(expected, actual)
14261426

0 commit comments

Comments
 (0)