Skip to content

Commit 504d81f

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF/TYP: define Index methods non-dynamically (pandas-dev#37038)
1 parent 5240c00 commit 504d81f

File tree

3 files changed

+39
-66
lines changed

3 files changed

+39
-66
lines changed

pandas/core/indexes/base.py

+13-22
Original file line numberDiff line numberDiff line change
@@ -5418,29 +5418,23 @@ def _arith_method(self, other, op):
54185418
return (Index(result[0]), Index(result[1]))
54195419
return Index(result)
54205420

5421-
@classmethod
5422-
def _add_numeric_methods_unary(cls):
5423-
"""
5424-
Add in numeric unary methods.
5425-
"""
5421+
def _unary_method(self, op):
5422+
result = op(self._values)
5423+
return Index(result, name=self.name)
54265424

5427-
def _make_evaluate_unary(op, opstr: str_t):
5428-
def _evaluate_numeric_unary(self):
5425+
def __abs__(self):
5426+
return self._unary_method(operator.abs)
54295427

5430-
attrs = self._get_attributes_dict()
5431-
return Index(op(self.values), **attrs)
5428+
def __neg__(self):
5429+
return self._unary_method(operator.neg)
54325430

5433-
_evaluate_numeric_unary.__name__ = opstr
5434-
return _evaluate_numeric_unary
5431+
def __pos__(self):
5432+
return self._unary_method(operator.pos)
54355433

5436-
setattr(cls, "__neg__", _make_evaluate_unary(operator.neg, "__neg__"))
5437-
setattr(cls, "__pos__", _make_evaluate_unary(operator.pos, "__pos__"))
5438-
setattr(cls, "__abs__", _make_evaluate_unary(np.abs, "__abs__"))
5439-
setattr(cls, "__inv__", _make_evaluate_unary(lambda x: -x, "__inv__"))
5440-
5441-
@classmethod
5442-
def _add_numeric_methods(cls):
5443-
cls._add_numeric_methods_unary()
5434+
def __inv__(self):
5435+
# TODO: why not operator.inv?
5436+
# TODO: __inv__ vs __invert__?
5437+
return self._unary_method(lambda x: -x)
54445438

54455439
def any(self, *args, **kwargs):
54465440
"""
@@ -5565,9 +5559,6 @@ def shape(self):
55655559
return self._values.shape
55665560

55675561

5568-
Index._add_numeric_methods()
5569-
5570-
55715562
def ensure_index_from_sequences(sequences, names=None):
55725563
"""
55735564
Construct an index from sequences of data.

pandas/core/indexes/multi.py

+26-37
Original file line numberDiff line numberDiff line change
@@ -3688,43 +3688,32 @@ def isin(self, values, level=None):
36883688
return np.zeros(len(levs), dtype=np.bool_)
36893689
return levs.isin(values)
36903690

3691-
@classmethod
3692-
def _add_numeric_methods_add_sub_disabled(cls):
3693-
"""
3694-
Add in the numeric add/sub methods to disable.
3695-
"""
3696-
cls.__add__ = make_invalid_op("__add__")
3697-
cls.__radd__ = make_invalid_op("__radd__")
3698-
cls.__iadd__ = make_invalid_op("__iadd__")
3699-
cls.__sub__ = make_invalid_op("__sub__")
3700-
cls.__rsub__ = make_invalid_op("__rsub__")
3701-
cls.__isub__ = make_invalid_op("__isub__")
3702-
3703-
@classmethod
3704-
def _add_numeric_methods_disabled(cls):
3705-
"""
3706-
Add in numeric methods to disable other than add/sub.
3707-
"""
3708-
cls.__pow__ = make_invalid_op("__pow__")
3709-
cls.__rpow__ = make_invalid_op("__rpow__")
3710-
cls.__mul__ = make_invalid_op("__mul__")
3711-
cls.__rmul__ = make_invalid_op("__rmul__")
3712-
cls.__floordiv__ = make_invalid_op("__floordiv__")
3713-
cls.__rfloordiv__ = make_invalid_op("__rfloordiv__")
3714-
cls.__truediv__ = make_invalid_op("__truediv__")
3715-
cls.__rtruediv__ = make_invalid_op("__rtruediv__")
3716-
cls.__mod__ = make_invalid_op("__mod__")
3717-
cls.__rmod__ = make_invalid_op("__rmod__")
3718-
cls.__divmod__ = make_invalid_op("__divmod__")
3719-
cls.__rdivmod__ = make_invalid_op("__rdivmod__")
3720-
cls.__neg__ = make_invalid_op("__neg__")
3721-
cls.__pos__ = make_invalid_op("__pos__")
3722-
cls.__abs__ = make_invalid_op("__abs__")
3723-
cls.__inv__ = make_invalid_op("__inv__")
3724-
3725-
3726-
MultiIndex._add_numeric_methods_disabled()
3727-
MultiIndex._add_numeric_methods_add_sub_disabled()
3691+
# ---------------------------------------------------------------
3692+
# Arithmetic/Numeric Methods - Disabled
3693+
3694+
__add__ = make_invalid_op("__add__")
3695+
__radd__ = make_invalid_op("__radd__")
3696+
__iadd__ = make_invalid_op("__iadd__")
3697+
__sub__ = make_invalid_op("__sub__")
3698+
__rsub__ = make_invalid_op("__rsub__")
3699+
__isub__ = make_invalid_op("__isub__")
3700+
__pow__ = make_invalid_op("__pow__")
3701+
__rpow__ = make_invalid_op("__rpow__")
3702+
__mul__ = make_invalid_op("__mul__")
3703+
__rmul__ = make_invalid_op("__rmul__")
3704+
__floordiv__ = make_invalid_op("__floordiv__")
3705+
__rfloordiv__ = make_invalid_op("__rfloordiv__")
3706+
__truediv__ = make_invalid_op("__truediv__")
3707+
__rtruediv__ = make_invalid_op("__rtruediv__")
3708+
__mod__ = make_invalid_op("__mod__")
3709+
__rmod__ = make_invalid_op("__rmod__")
3710+
__divmod__ = make_invalid_op("__divmod__")
3711+
__rdivmod__ = make_invalid_op("__rdivmod__")
3712+
# Unary methods disabled
3713+
__neg__ = make_invalid_op("__neg__")
3714+
__pos__ = make_invalid_op("__pos__")
3715+
__abs__ = make_invalid_op("__abs__")
3716+
__inv__ = make_invalid_op("__inv__")
37283717

37293718

37303719
def sparsify_labels(label_list, start: int = 0, sentinel=""):

pandas/core/indexes/numeric.py

-7
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ def _can_union_without_object_cast(self, other) -> bool:
275275
return other.dtype == "f8" or other.dtype == self.dtype
276276

277277

278-
Int64Index._add_numeric_methods()
279-
280278
_uint64_descr_args = dict(
281279
klass="UInt64Index", ltype="unsigned integer", dtype="uint64", extra=""
282280
)
@@ -321,8 +319,6 @@ def _can_union_without_object_cast(self, other) -> bool:
321319
return other.dtype == "f8" or other.dtype == self.dtype
322320

323321

324-
UInt64Index._add_numeric_methods()
325-
326322
_float64_descr_args = dict(
327323
klass="Float64Index", dtype="float64", ltype="float", extra=""
328324
)
@@ -425,6 +421,3 @@ def isin(self, values, level=None):
425421
def _can_union_without_object_cast(self, other) -> bool:
426422
# See GH#26778, further casting may occur in NumericIndex._union
427423
return is_numeric_dtype(other.dtype)
428-
429-
430-
Float64Index._add_numeric_methods()

0 commit comments

Comments
 (0)