Skip to content

Commit fde1232

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF: define inplace ops non-dynamically (pandas-dev#37065)
1 parent 7741519 commit fde1232

File tree

6 files changed

+53
-74
lines changed

6 files changed

+53
-74
lines changed

pandas/core/arraylike.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
"""
77
import operator
88

9-
from pandas.errors import AbstractMethodError
10-
119
from pandas.core.ops import roperator
1210
from pandas.core.ops.common import unpack_zerodim_and_defer
1311

@@ -17,7 +15,7 @@ class OpsMixin:
1715
# Comparisons
1816

1917
def _cmp_method(self, other, op):
20-
raise AbstractMethodError(self)
18+
return NotImplemented
2119

2220
@unpack_zerodim_and_defer("__eq__")
2321
def __eq__(self, other):
@@ -47,7 +45,7 @@ def __ge__(self, other):
4745
# Logical Methods
4846

4947
def _logical_method(self, other, op):
50-
raise AbstractMethodError(self)
48+
return NotImplemented
5149

5250
@unpack_zerodim_and_defer("__and__")
5351
def __and__(self, other):

pandas/core/frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -9366,7 +9366,6 @@ def _AXIS_NAMES(self) -> Dict[int, str]:
93669366
DataFrame._add_numeric_operations()
93679367

93689368
ops.add_flex_arithmetic_methods(DataFrame)
9369-
ops.add_special_arithmetic_methods(DataFrame)
93709369

93719370

93729371
def _from_nested_dict(data) -> collections.defaultdict:

pandas/core/generic.py

+50
Original file line numberDiff line numberDiff line change
@@ -9799,6 +9799,7 @@ def _tz_localize(ax, tz, ambiguous, nonexistent):
97999799

98009800
# ----------------------------------------------------------------------
98019801
# Numeric Methods
9802+
98029803
def abs(self: FrameOrSeries) -> FrameOrSeries:
98039804
"""
98049805
Return a Series/DataFrame with absolute numeric value of each element.
@@ -11088,6 +11089,55 @@ def ewm(
1108811089
times=times,
1108911090
)
1109011091

11092+
# ----------------------------------------------------------------------
11093+
# Arithmetic Methods
11094+
11095+
def _inplace_method(self, other, op):
11096+
"""
11097+
Wrap arithmetic method to operate inplace.
11098+
"""
11099+
result = op(self, other)
11100+
11101+
# Delete cacher
11102+
self._reset_cacher()
11103+
11104+
# this makes sure that we are aligned like the input
11105+
# we are updating inplace so we want to ignore is_copy
11106+
self._update_inplace(
11107+
result.reindex_like(self, copy=False), verify_is_copy=False
11108+
)
11109+
return self
11110+
11111+
def __iadd__(self, other):
11112+
return self._inplace_method(other, type(self).__add__)
11113+
11114+
def __isub__(self, other):
11115+
return self._inplace_method(other, type(self).__sub__)
11116+
11117+
def __imul__(self, other):
11118+
return self._inplace_method(other, type(self).__mul__)
11119+
11120+
def __itruediv__(self, other):
11121+
return self._inplace_method(other, type(self).__truediv__)
11122+
11123+
def __ifloordiv__(self, other):
11124+
return self._inplace_method(other, type(self).__floordiv__)
11125+
11126+
def __imod__(self, other):
11127+
return self._inplace_method(other, type(self).__mod__)
11128+
11129+
def __ipow__(self, other):
11130+
return self._inplace_method(other, type(self).__pow__)
11131+
11132+
def __iand__(self, other):
11133+
return self._inplace_method(other, type(self).__and__)
11134+
11135+
def __ior__(self, other):
11136+
return self._inplace_method(other, type(self).__or__)
11137+
11138+
def __ixor__(self, other):
11139+
return self._inplace_method(other, type(self).__xor__)
11140+
1109111141
# ----------------------------------------------------------------------
1109211142
# Misc methods
1109311143

pandas/core/ops/__init__.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
)
3434
from pandas.core.ops.invalid import invalid_comparison # noqa:F401
3535
from pandas.core.ops.mask_ops import kleene_and, kleene_or, kleene_xor # noqa: F401
36-
from pandas.core.ops.methods import ( # noqa:F401
37-
add_flex_arithmetic_methods,
38-
add_special_arithmetic_methods,
39-
)
36+
from pandas.core.ops.methods import add_flex_arithmetic_methods # noqa:F401
4037
from pandas.core.ops.roperator import ( # noqa:F401
4138
radd,
4239
rand_,

pandas/core/ops/methods.py

-56
Original file line numberDiff line numberDiff line change
@@ -49,62 +49,6 @@ def _get_method_wrappers(cls):
4949
return arith_flex, comp_flex
5050

5151

52-
def add_special_arithmetic_methods(cls):
53-
"""
54-
Adds the full suite of special arithmetic methods (``__add__``,
55-
``__sub__``, etc.) to the class.
56-
57-
Parameters
58-
----------
59-
cls : class
60-
special methods will be defined and pinned to this class
61-
"""
62-
new_methods = {}
63-
64-
def _wrap_inplace_method(method):
65-
"""
66-
return an inplace wrapper for this method
67-
"""
68-
69-
def f(self, other):
70-
result = method(self, other)
71-
# Delete cacher
72-
self._reset_cacher()
73-
# this makes sure that we are aligned like the input
74-
# we are updating inplace so we want to ignore is_copy
75-
self._update_inplace(
76-
result.reindex_like(self, copy=False), verify_is_copy=False
77-
)
78-
79-
return self
80-
81-
name = method.__name__.strip("__")
82-
f.__name__ = f"__i{name}__"
83-
return f
84-
85-
# wrap methods that we get from OpsMixin
86-
new_methods.update(
87-
dict(
88-
__iadd__=_wrap_inplace_method(cls.__add__),
89-
__isub__=_wrap_inplace_method(cls.__sub__),
90-
__imul__=_wrap_inplace_method(cls.__mul__),
91-
__itruediv__=_wrap_inplace_method(cls.__truediv__),
92-
__ifloordiv__=_wrap_inplace_method(cls.__floordiv__),
93-
__imod__=_wrap_inplace_method(cls.__mod__),
94-
__ipow__=_wrap_inplace_method(cls.__pow__),
95-
)
96-
)
97-
new_methods.update(
98-
dict(
99-
__iand__=_wrap_inplace_method(cls.__and__),
100-
__ior__=_wrap_inplace_method(cls.__or__),
101-
__ixor__=_wrap_inplace_method(cls.__xor__),
102-
)
103-
)
104-
105-
_add_methods(cls, new_methods=new_methods)
106-
107-
10852
def add_flex_arithmetic_methods(cls):
10953
"""
11054
Adds the full suite of flex arithmetic methods (``pow``, ``mul``, ``add``)

pandas/core/series.py

-9
Original file line numberDiff line numberDiff line change
@@ -5005,17 +5005,8 @@ def _arith_method(self, other, op):
50055005

50065006
return self._construct_result(result, name=res_name)
50075007

5008-
def __div__(self, other):
5009-
# Alias for backward compat
5010-
return self.__truediv__(other)
5011-
5012-
def __rdiv__(self, other):
5013-
# Alias for backward compat
5014-
return self.__rtruediv__(other)
5015-
50165008

50175009
Series._add_numeric_operations()
50185010

50195011
# Add arithmetic!
50205012
ops.add_flex_arithmetic_methods(Series)
5021-
ops.add_special_arithmetic_methods(Series)

0 commit comments

Comments
 (0)