|
42 | 42 | is_bool,
|
43 | 43 | is_integer,
|
44 | 44 | is_list_like,
|
| 45 | + is_numeric_dtype, |
45 | 46 | is_scalar,
|
46 | 47 | needs_i8_conversion,
|
47 | 48 | )
|
@@ -259,6 +260,26 @@ def _slice_axis_for_step(self, index: Index, result: Sized | None = None) -> Ind
|
259 | 260 | else index[:: self.step]
|
260 | 261 | )
|
261 | 262 |
|
| 263 | + def _validate_numeric_only(self, name: str, numeric_only: bool) -> None: |
| 264 | + """ |
| 265 | + Validate numeric_only argument, raising if invalid for the input. |
| 266 | +
|
| 267 | + Parameters |
| 268 | + ---------- |
| 269 | + name : str |
| 270 | + Name of the operator (kernel). |
| 271 | + numeric_only : bool |
| 272 | + Value passed by user. |
| 273 | + """ |
| 274 | + if ( |
| 275 | + self._selected_obj.ndim == 1 |
| 276 | + and numeric_only |
| 277 | + and not is_numeric_dtype(self._selected_obj.dtype) |
| 278 | + ): |
| 279 | + raise NotImplementedError( |
| 280 | + f"{type(self).__name__}.{name} does not implement numeric_only" |
| 281 | + ) |
| 282 | + |
262 | 283 | def _make_numeric_only(self, obj: NDFrameT) -> NDFrameT:
|
263 | 284 | """Subset DataFrame to numeric columns.
|
264 | 285 |
|
@@ -469,13 +490,14 @@ def _apply_series(
|
469 | 490 | def _apply_blockwise(
|
470 | 491 | self,
|
471 | 492 | homogeneous_func: Callable[..., ArrayLike],
|
472 |
| - name: str | None = None, |
| 493 | + name: str, |
473 | 494 | numeric_only: bool = False,
|
474 | 495 | ) -> DataFrame | Series:
|
475 | 496 | """
|
476 | 497 | Apply the given function to the DataFrame broken down into homogeneous
|
477 | 498 | sub-frames.
|
478 | 499 | """
|
| 500 | + self._validate_numeric_only(name, numeric_only) |
479 | 501 | if self._selected_obj.ndim == 1:
|
480 | 502 | return self._apply_series(homogeneous_func, name)
|
481 | 503 |
|
@@ -583,7 +605,7 @@ def _apply_pairwise(
|
583 | 605 | def _apply(
|
584 | 606 | self,
|
585 | 607 | func: Callable[..., Any],
|
586 |
| - name: str | None = None, |
| 608 | + name: str, |
587 | 609 | numeric_only: bool = False,
|
588 | 610 | numba_args: tuple[Any, ...] = (),
|
589 | 611 | **kwargs,
|
@@ -726,7 +748,7 @@ def __init__(
|
726 | 748 | def _apply(
|
727 | 749 | self,
|
728 | 750 | func: Callable[..., Any],
|
729 |
| - name: str | None = None, |
| 751 | + name: str, |
730 | 752 | numeric_only: bool = False,
|
731 | 753 | numba_args: tuple[Any, ...] = (),
|
732 | 754 | **kwargs,
|
@@ -1166,7 +1188,7 @@ def _center_window(self, result: np.ndarray, offset: int) -> np.ndarray:
|
1166 | 1188 | def _apply(
|
1167 | 1189 | self,
|
1168 | 1190 | func: Callable[[np.ndarray, int, int], np.ndarray],
|
1169 |
| - name: str | None = None, |
| 1191 | + name: str, |
1170 | 1192 | numeric_only: bool = False,
|
1171 | 1193 | numba_args: tuple[Any, ...] = (),
|
1172 | 1194 | **kwargs,
|
@@ -1398,6 +1420,7 @@ def apply(
|
1398 | 1420 |
|
1399 | 1421 | return self._apply(
|
1400 | 1422 | apply_func,
|
| 1423 | + name="apply", |
1401 | 1424 | numba_args=numba_args,
|
1402 | 1425 | )
|
1403 | 1426 |
|
@@ -1617,6 +1640,8 @@ def skew(self, numeric_only: bool = False, **kwargs):
|
1617 | 1640 |
|
1618 | 1641 | def sem(self, ddof: int = 1, numeric_only: bool = False, *args, **kwargs):
|
1619 | 1642 | nv.validate_rolling_func("sem", args, kwargs)
|
| 1643 | + # Raise here so error message says sem instead of std |
| 1644 | + self._validate_numeric_only("sem", numeric_only) |
1620 | 1645 | return self.std(numeric_only=numeric_only, **kwargs) / (
|
1621 | 1646 | self.count(numeric_only=numeric_only) - ddof
|
1622 | 1647 | ).pow(0.5)
|
@@ -2428,6 +2453,8 @@ def skew(self, numeric_only: bool = False, **kwargs):
|
2428 | 2453 | )
|
2429 | 2454 | def sem(self, ddof: int = 1, numeric_only: bool = False, *args, **kwargs):
|
2430 | 2455 | nv.validate_rolling_func("sem", args, kwargs)
|
| 2456 | + # Raise here so error message says sem instead of std |
| 2457 | + self._validate_numeric_only("sem", numeric_only) |
2431 | 2458 | return self.std(numeric_only=numeric_only, **kwargs) / (
|
2432 | 2459 | self.count(numeric_only) - ddof
|
2433 | 2460 | ).pow(0.5)
|
|
0 commit comments