|
68 | 68 | is_integer,
|
69 | 69 | is_iterator,
|
70 | 70 | is_list_like,
|
71 |
| - is_numeric_dtype, |
72 | 71 | is_object_dtype,
|
73 | 72 | is_scalar,
|
74 | 73 | pandas_dtype,
|
|
101 | 100 | extract_array,
|
102 | 101 | sanitize_array,
|
103 | 102 | )
|
104 |
| -from pandas.core.generic import NDFrame |
| 103 | +from pandas.core.generic import ( |
| 104 | + NDFrame, |
| 105 | + make_doc, |
| 106 | +) |
105 | 107 | from pandas.core.indexers import (
|
106 | 108 | disallow_ndim_indexing,
|
107 | 109 | unpack_1tuple,
|
@@ -4527,45 +4529,6 @@ def apply(
|
4527 | 4529 | )
|
4528 | 4530 | return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
|
4529 | 4531 |
|
4530 |
| - def _reduce( |
4531 |
| - self, |
4532 |
| - op, |
4533 |
| - name: str, |
4534 |
| - *, |
4535 |
| - axis: Axis = 0, |
4536 |
| - skipna: bool = True, |
4537 |
| - numeric_only: bool = False, |
4538 |
| - filter_type=None, |
4539 |
| - **kwds, |
4540 |
| - ): |
4541 |
| - """ |
4542 |
| - Perform a reduction operation. |
4543 |
| -
|
4544 |
| - If we have an ndarray as a value, then simply perform the operation, |
4545 |
| - otherwise delegate to the object. |
4546 |
| - """ |
4547 |
| - delegate = self._values |
4548 |
| - |
4549 |
| - if axis is not None: |
4550 |
| - self._get_axis_number(axis) |
4551 |
| - |
4552 |
| - if isinstance(delegate, ExtensionArray): |
4553 |
| - # dispatch to ExtensionArray interface |
4554 |
| - return delegate._reduce(name, skipna=skipna, **kwds) |
4555 |
| - |
4556 |
| - else: |
4557 |
| - # dispatch to numpy arrays |
4558 |
| - if numeric_only and not is_numeric_dtype(self.dtype): |
4559 |
| - kwd_name = "numeric_only" |
4560 |
| - if name in ["any", "all"]: |
4561 |
| - kwd_name = "bool_only" |
4562 |
| - # GH#47500 - change to TypeError to match other methods |
4563 |
| - raise TypeError( |
4564 |
| - f"Series.{name} does not allow {kwd_name}={numeric_only} " |
4565 |
| - "with non-numeric dtypes." |
4566 |
| - ) |
4567 |
| - return op(delegate, skipna=skipna, **kwds) |
4568 |
| - |
4569 | 4532 | def _reindex_indexer(
|
4570 | 4533 | self,
|
4571 | 4534 | new_index: Index | None,
|
@@ -6044,5 +6007,89 @@ def rdivmod(self, other, level=None, fill_value=None, axis: Axis = 0):
|
6044 | 6007 | other, roperator.rdivmod, level=level, fill_value=fill_value, axis=axis
|
6045 | 6008 | )
|
6046 | 6009 |
|
| 6010 | + # ---------------------------------------------------------------------- |
| 6011 | + # Reductions |
| 6012 | + |
| 6013 | + def _reduce( |
| 6014 | + self, |
| 6015 | + op, |
| 6016 | + # error: Variable "pandas.core.series.Series.str" is not valid as a type |
| 6017 | + name: str, # type: ignore[valid-type] |
| 6018 | + *, |
| 6019 | + axis: Axis = 0, |
| 6020 | + skipna: bool = True, |
| 6021 | + numeric_only: bool = False, |
| 6022 | + filter_type=None, |
| 6023 | + **kwds, |
| 6024 | + ): |
| 6025 | + """ |
| 6026 | + Perform a reduction operation. |
| 6027 | +
|
| 6028 | + If we have an ndarray as a value, then simply perform the operation, |
| 6029 | + otherwise delegate to the object. |
| 6030 | + """ |
| 6031 | + delegate = self._values |
| 6032 | + |
| 6033 | + if axis is not None: |
| 6034 | + self._get_axis_number(axis) |
| 6035 | + |
| 6036 | + if isinstance(delegate, ExtensionArray): |
| 6037 | + # dispatch to ExtensionArray interface |
| 6038 | + return delegate._reduce(name, skipna=skipna, **kwds) |
| 6039 | + |
| 6040 | + else: |
| 6041 | + # dispatch to numpy arrays |
| 6042 | + if numeric_only and self.dtype.kind not in "iufcb": |
| 6043 | + # i.e. not is_numeric_dtype(self.dtype) |
| 6044 | + kwd_name = "numeric_only" |
| 6045 | + if name in ["any", "all"]: |
| 6046 | + kwd_name = "bool_only" |
| 6047 | + # GH#47500 - change to TypeError to match other methods |
| 6048 | + raise TypeError( |
| 6049 | + f"Series.{name} does not allow {kwd_name}={numeric_only} " |
| 6050 | + "with non-numeric dtypes." |
| 6051 | + ) |
| 6052 | + return op(delegate, skipna=skipna, **kwds) |
| 6053 | + |
| 6054 | + @Appender(make_doc("any", ndim=1)) |
| 6055 | + # error: Signature of "any" incompatible with supertype "NDFrame" |
| 6056 | + def any( # type: ignore[override] |
| 6057 | + self, |
| 6058 | + *, |
| 6059 | + axis: Axis = 0, |
| 6060 | + bool_only=None, |
| 6061 | + skipna: bool = True, |
| 6062 | + **kwargs, |
| 6063 | + ) -> bool: |
| 6064 | + nv.validate_logical_func((), kwargs, fname="any") |
| 6065 | + validate_bool_kwarg(skipna, "skipna", none_allowed=False) |
| 6066 | + return self._reduce( |
| 6067 | + nanops.nanany, |
| 6068 | + name="any", |
| 6069 | + axis=axis, |
| 6070 | + numeric_only=bool_only, |
| 6071 | + skipna=skipna, |
| 6072 | + filter_type="bool", |
| 6073 | + ) |
| 6074 | + |
| 6075 | + @Appender(make_doc("all", ndim=1)) |
| 6076 | + def all( |
| 6077 | + self, |
| 6078 | + axis: Axis = 0, |
| 6079 | + bool_only=None, |
| 6080 | + skipna: bool = True, |
| 6081 | + **kwargs, |
| 6082 | + ) -> bool: |
| 6083 | + nv.validate_logical_func((), kwargs, fname="all") |
| 6084 | + validate_bool_kwarg(skipna, "skipna", none_allowed=False) |
| 6085 | + return self._reduce( |
| 6086 | + nanops.nanall, |
| 6087 | + name="all", |
| 6088 | + axis=axis, |
| 6089 | + numeric_only=bool_only, |
| 6090 | + skipna=skipna, |
| 6091 | + filter_type="bool", |
| 6092 | + ) |
| 6093 | + |
6047 | 6094 |
|
6048 | 6095 | Series._add_numeric_operations()
|
0 commit comments