Skip to content

Commit dfdc96a

Browse files
committed
Typing fixes
1 parent 93e786b commit dfdc96a

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

xarray/core/groupby.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def values(self) -> range:
191191
return range(self.size)
192192

193193
@property
194-
def data(self) -> range:
195-
return range(self.size)
194+
def data(self) -> np.ndarray:
195+
return np.arange(self.size, dtype=int)
196196

197197
def __array__(self) -> np.ndarray:
198198
return np.arange(self.size)
@@ -517,6 +517,7 @@ class GroupBy(Generic[T_Xarray]):
517517
"_dims",
518518
"_sizes",
519519
"_len",
520+
"_by_chunked",
520521
# Save unstacked object for flox
521522
"_original_obj",
522523
"_codes",

xarray/groupers.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import numpy as np
1515
import pandas as pd
16+
from numpy.typing import ArrayLike
1617

1718
from xarray.coding.cftime_offsets import BaseCFTimeOffset, _new_to_legacy_freq
1819
from xarray.core import duck_array_ops
@@ -99,12 +100,18 @@ def __init__(
99100
assert isinstance(full_index, pd.Index)
100101
self.full_index = full_index
101102

102-
if group_indices is None and not is_chunked_array(codes.data):
103-
self.group_indices = tuple(
104-
g
105-
for g in _codes_to_group_indices(codes.data.ravel(), len(full_index))
106-
if g
107-
)
103+
if group_indices is None:
104+
if not is_chunked_array(codes.data):
105+
self.group_indices = tuple(
106+
g
107+
for g in _codes_to_group_indices(
108+
codes.data.ravel(), len(full_index)
109+
)
110+
if g
111+
)
112+
else:
113+
# We will not use this when grouping by a chunked array
114+
self.group_indices = tuple()
108115
else:
109116
self.group_indices = group_indices
110117

@@ -168,13 +175,11 @@ class UniqueGrouper(Grouper):
168175
"""
169176

170177
_group_as_index: pd.Index | None = field(default=None, repr=False)
171-
labels: np.ndarray | None = field(default=None)
178+
labels: ArrayLike | None = field(default=None)
172179

173180
@property
174181
def group_as_index(self) -> pd.Index:
175182
"""Caches the group DataArray as a pandas Index."""
176-
if is_chunked_array(self.group):
177-
raise ValueError("Please call compute manually.")
178183
if self._group_as_index is None:
179184
if self.group.ndim == 1:
180185
self._group_as_index = self.group.to_index()
@@ -214,7 +219,7 @@ def _factorize_given_labels(self, group: T_Group) -> EncodedGroups:
214219
)
215220
return EncodedGroups(
216221
codes=codes,
217-
full_index=pd.Index(self.labels),
222+
full_index=pd.Index(self.labels), # type: ignore[arg-type]
218223
unique_coord=Variable(
219224
dims=codes.name,
220225
data=self.labels,
@@ -332,7 +337,7 @@ def __post_init__(self) -> None:
332337
raise ValueError("All bin edges are NaN.")
333338

334339
def _cut(self, data):
335-
return pd.cut( # type: ignore [call-overload]
340+
return pd.cut(
336341
np.asarray(data).ravel(),
337342
bins=self.bins,
338343
right=self.right,

0 commit comments

Comments
 (0)