diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index aece69d49a68b..b3f5fb6f0291a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5418,29 +5418,23 @@ def _arith_method(self, other, op): return (Index(result[0]), Index(result[1])) return Index(result) - @classmethod - def _add_numeric_methods_unary(cls): - """ - Add in numeric unary methods. - """ + def _unary_method(self, op): + result = op(self._values) + return Index(result, name=self.name) - def _make_evaluate_unary(op, opstr: str_t): - def _evaluate_numeric_unary(self): + def __abs__(self): + return self._unary_method(operator.abs) - attrs = self._get_attributes_dict() - return Index(op(self.values), **attrs) + def __neg__(self): + return self._unary_method(operator.neg) - _evaluate_numeric_unary.__name__ = opstr - return _evaluate_numeric_unary + def __pos__(self): + return self._unary_method(operator.pos) - setattr(cls, "__neg__", _make_evaluate_unary(operator.neg, "__neg__")) - setattr(cls, "__pos__", _make_evaluate_unary(operator.pos, "__pos__")) - setattr(cls, "__abs__", _make_evaluate_unary(np.abs, "__abs__")) - setattr(cls, "__inv__", _make_evaluate_unary(lambda x: -x, "__inv__")) - - @classmethod - def _add_numeric_methods(cls): - cls._add_numeric_methods_unary() + def __inv__(self): + # TODO: why not operator.inv? + # TODO: __inv__ vs __invert__? + return self._unary_method(lambda x: -x) def any(self, *args, **kwargs): """ @@ -5565,9 +5559,6 @@ def shape(self): return self._values.shape -Index._add_numeric_methods() - - def ensure_index_from_sequences(sequences, names=None): """ Construct an index from sequences of data. diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 41f046a7f5f8a..2ce4538a63d25 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -3688,43 +3688,32 @@ def isin(self, values, level=None): return np.zeros(len(levs), dtype=np.bool_) return levs.isin(values) - @classmethod - def _add_numeric_methods_add_sub_disabled(cls): - """ - Add in the numeric add/sub methods to disable. - """ - cls.__add__ = make_invalid_op("__add__") - cls.__radd__ = make_invalid_op("__radd__") - cls.__iadd__ = make_invalid_op("__iadd__") - cls.__sub__ = make_invalid_op("__sub__") - cls.__rsub__ = make_invalid_op("__rsub__") - cls.__isub__ = make_invalid_op("__isub__") - - @classmethod - def _add_numeric_methods_disabled(cls): - """ - Add in numeric methods to disable other than add/sub. - """ - cls.__pow__ = make_invalid_op("__pow__") - cls.__rpow__ = make_invalid_op("__rpow__") - cls.__mul__ = make_invalid_op("__mul__") - cls.__rmul__ = make_invalid_op("__rmul__") - cls.__floordiv__ = make_invalid_op("__floordiv__") - cls.__rfloordiv__ = make_invalid_op("__rfloordiv__") - cls.__truediv__ = make_invalid_op("__truediv__") - cls.__rtruediv__ = make_invalid_op("__rtruediv__") - cls.__mod__ = make_invalid_op("__mod__") - cls.__rmod__ = make_invalid_op("__rmod__") - cls.__divmod__ = make_invalid_op("__divmod__") - cls.__rdivmod__ = make_invalid_op("__rdivmod__") - cls.__neg__ = make_invalid_op("__neg__") - cls.__pos__ = make_invalid_op("__pos__") - cls.__abs__ = make_invalid_op("__abs__") - cls.__inv__ = make_invalid_op("__inv__") - - -MultiIndex._add_numeric_methods_disabled() -MultiIndex._add_numeric_methods_add_sub_disabled() + # --------------------------------------------------------------- + # Arithmetic/Numeric Methods - Disabled + + __add__ = make_invalid_op("__add__") + __radd__ = make_invalid_op("__radd__") + __iadd__ = make_invalid_op("__iadd__") + __sub__ = make_invalid_op("__sub__") + __rsub__ = make_invalid_op("__rsub__") + __isub__ = make_invalid_op("__isub__") + __pow__ = make_invalid_op("__pow__") + __rpow__ = make_invalid_op("__rpow__") + __mul__ = make_invalid_op("__mul__") + __rmul__ = make_invalid_op("__rmul__") + __floordiv__ = make_invalid_op("__floordiv__") + __rfloordiv__ = make_invalid_op("__rfloordiv__") + __truediv__ = make_invalid_op("__truediv__") + __rtruediv__ = make_invalid_op("__rtruediv__") + __mod__ = make_invalid_op("__mod__") + __rmod__ = make_invalid_op("__rmod__") + __divmod__ = make_invalid_op("__divmod__") + __rdivmod__ = make_invalid_op("__rdivmod__") + # Unary methods disabled + __neg__ = make_invalid_op("__neg__") + __pos__ = make_invalid_op("__pos__") + __abs__ = make_invalid_op("__abs__") + __inv__ = make_invalid_op("__inv__") def sparsify_labels(label_list, start: int = 0, sentinel=""): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 60a206a5344de..385bffa4bc315 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -275,8 +275,6 @@ def _can_union_without_object_cast(self, other) -> bool: return other.dtype == "f8" or other.dtype == self.dtype -Int64Index._add_numeric_methods() - _uint64_descr_args = dict( klass="UInt64Index", ltype="unsigned integer", dtype="uint64", extra="" ) @@ -321,8 +319,6 @@ def _can_union_without_object_cast(self, other) -> bool: return other.dtype == "f8" or other.dtype == self.dtype -UInt64Index._add_numeric_methods() - _float64_descr_args = dict( klass="Float64Index", dtype="float64", ltype="float", extra="" ) @@ -425,6 +421,3 @@ def isin(self, values, level=None): def _can_union_without_object_cast(self, other) -> bool: # See GH#26778, further casting may occur in NumericIndex._union return is_numeric_dtype(other.dtype) - - -Float64Index._add_numeric_methods()