|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING, Any, Sequence, TypeVar |
| 3 | +import inspect |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any, Callable, Sequence, TypeVar |
4 | 6 | from typing import cast as type_cast |
5 | 7 |
|
6 | 8 | import numpy as np |
|
9 | 11 | from pandas.api.extensions import ExtensionArray |
10 | 12 |
|
11 | 13 | from arkouda.numpy.dtypes import dtype as ak_dtype |
| 14 | +from arkouda.pandas.groupbyclass import GroupByReductionType |
12 | 15 |
|
13 | 16 | from ._arkouda_extension_array import ArkoudaExtensionArray |
14 | 17 | from ._dtypes import ( |
@@ -224,21 +227,95 @@ def equals(self, other): |
224 | 227 | return False |
225 | 228 | return self._data.equals(other._data) |
226 | 229 |
|
227 | | - def _reduce(self, name, skipna=True, **kwargs): |
228 | | - if name == "all": |
229 | | - return self._data.all() |
230 | | - elif name == "any": |
231 | | - return self._data.any() |
232 | | - elif name == "sum": |
233 | | - return self._data.sum() |
234 | | - elif name == "prod": |
235 | | - return self._data.prod() |
236 | | - elif name == "min": |
237 | | - return self._data.min() |
238 | | - elif name == "max": |
239 | | - return self._data.max() |
240 | | - else: |
241 | | - raise TypeError(f"'ArkoudaArray' with dtype arkouda does not support reduction '{name}'") |
| 230 | + def _reduce( |
| 231 | + self, |
| 232 | + name: str | GroupByReductionType, |
| 233 | + skipna: bool = True, |
| 234 | + **kwargs: Any, |
| 235 | + ) -> Any: |
| 236 | + """ |
| 237 | + Reduce the underlying data. |
| 238 | +
|
| 239 | + Parameters |
| 240 | + ---------- |
| 241 | + name : str | GroupByReductionType |
| 242 | + Reduction name, e.g. "sum", "mean", "nunique", ... |
| 243 | + skipna : bool |
| 244 | + If supported by the underlying implementation, skip NaN/NA values. |
| 245 | + Default is True. |
| 246 | + **kwargs : Any |
| 247 | + Extra args for compatibility (e.g. ddof for var/std). |
| 248 | +
|
| 249 | + Returns |
| 250 | + ------- |
| 251 | + Any |
| 252 | + The reduction result. |
| 253 | +
|
| 254 | + Raises |
| 255 | + ------ |
| 256 | + TypeError |
| 257 | + If ``name`` is not a supported reduction or the underlying data does not |
| 258 | + implement the requested reduction. |
| 259 | + """ |
| 260 | + # Normalize: accept Enum or str |
| 261 | + if hasattr(name, "value"): # enum-like |
| 262 | + name = name.value |
| 263 | + if isinstance(name, tuple) and len(name) == 1: # guards against UNIQUE="unique", |
| 264 | + name = name[0] |
| 265 | + if not isinstance(name, str): |
| 266 | + raise TypeError(f"Reduction name must be a string or GroupByReductionType, got {type(name)}") |
| 267 | + |
| 268 | + data = self._data |
| 269 | + |
| 270 | + def _call_method(method_name: str, *args: Any, **kw: Any) -> Any: |
| 271 | + if not hasattr(data, method_name): |
| 272 | + raise TypeError( |
| 273 | + f"'ArkoudaArray' with dtype {self.dtype} does not support reduction '{name}' " |
| 274 | + f"(missing method {method_name!r} on {type(data).__name__})" |
| 275 | + ) |
| 276 | + meth = getattr(data, method_name) |
| 277 | + |
| 278 | + # Best-effort: pass skipna/ddof/etc only if the method accepts them. |
| 279 | + try: |
| 280 | + sig = inspect.signature(meth) |
| 281 | + except (TypeError, ValueError): |
| 282 | + return meth(*args, **kw) |
| 283 | + |
| 284 | + params = sig.parameters |
| 285 | + filtered: dict[str, Any] = {k: v for k, v in kw.items() if k in params} |
| 286 | + return meth(*args, **filtered) |
| 287 | + |
| 288 | + reductions: dict[str, Callable[[], Any]] = { |
| 289 | + "all": lambda: _call_method("all", skipna=skipna, **kwargs), |
| 290 | + "any": lambda: _call_method("any", skipna=skipna, **kwargs), |
| 291 | + "sum": lambda: _call_method("sum", skipna=skipna, **kwargs), |
| 292 | + "prod": lambda: _call_method("prod", skipna=skipna, **kwargs), |
| 293 | + "min": lambda: _call_method("min", skipna=skipna, **kwargs), |
| 294 | + "max": lambda: _call_method("max", skipna=skipna, **kwargs), |
| 295 | + "mean": lambda: _call_method("mean", skipna=skipna, **kwargs), |
| 296 | + "median": lambda: _call_method("median", skipna=skipna, **kwargs), |
| 297 | + "var": lambda: _call_method("var", skipna=skipna, **kwargs), |
| 298 | + "std": lambda: _call_method("std", skipna=skipna, **kwargs), |
| 299 | + "argmin": lambda: _call_method("argmin", skipna=skipna, **kwargs), |
| 300 | + "argmax": lambda: _call_method("argmax", skipna=skipna, **kwargs), |
| 301 | + "count": lambda: _call_method("count", **kwargs), |
| 302 | + "nunique": lambda: _call_method("nunique", **kwargs), |
| 303 | + "or": lambda: _call_method("or", skipna=skipna, **kwargs), |
| 304 | + "and": lambda: _call_method("and", skipna=skipna, **kwargs), |
| 305 | + "xor": lambda: _call_method("xor", skipna=skipna, **kwargs), |
| 306 | + "first": lambda: _call_method("first", skipna=skipna, **kwargs), |
| 307 | + "mode": lambda: _call_method("mode", skipna=skipna, **kwargs), |
| 308 | + "unique": lambda: _call_method("unique", **kwargs), |
| 309 | + } |
| 310 | + |
| 311 | + fn = reductions.get(name) |
| 312 | + if fn is None: |
| 313 | + raise TypeError( |
| 314 | + f"'ArkoudaArray' with dtype {self.dtype} does not support reduction '{name}'. " |
| 315 | + f"Supported: {sorted(reductions)}" |
| 316 | + ) |
| 317 | + |
| 318 | + return fn() |
242 | 319 |
|
243 | 320 | def __eq__(self, other): |
244 | 321 | """ |
|
0 commit comments