Skip to content

REF/TYP: define Index methods non-dynamically #37038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand Down
63 changes: 26 additions & 37 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=""):
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
)
Expand Down Expand Up @@ -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=""
)
Expand Down Expand Up @@ -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()