Skip to content

Commit 241740c

Browse files
authored
CLN: ops, unnecessary mixin (#37111)
1 parent 0d9be88 commit 241740c

File tree

5 files changed

+29
-61
lines changed

5 files changed

+29
-61
lines changed

pandas/core/arrays/datetimelike.py

+18-21
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from pandas.core.arrays import DatetimeArray, TimedeltaArray
7676

7777
DTScalarOrNaT = Union[DatetimeLikeScalar, NaTType]
78+
DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin")
7879

7980

8081
class InvalidComparison(Exception):
@@ -86,7 +87,20 @@ class InvalidComparison(Exception):
8687
pass
8788

8889

89-
class AttributesMixin:
90+
class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
91+
"""
92+
Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray
93+
94+
Assumes that __new__/__init__ defines:
95+
_data
96+
_freq
97+
98+
and that the inheriting class has methods:
99+
_generate_range
100+
"""
101+
102+
_is_recognized_dtype: Callable[[DtypeObj], bool]
103+
_recognized_scalars: Tuple[Type, ...]
90104
_data: np.ndarray
91105

92106
def __init__(self, data, dtype=None, freq=None, copy=False):
@@ -184,25 +198,6 @@ def _check_compatible_with(
184198
"""
185199
raise AbstractMethodError(self)
186200

187-
188-
DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin")
189-
190-
191-
class DatetimeLikeArrayMixin(OpsMixin, AttributesMixin, NDArrayBackedExtensionArray):
192-
"""
193-
Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray
194-
195-
Assumes that __new__/__init__ defines:
196-
_data
197-
_freq
198-
199-
and that the inheriting class has methods:
200-
_generate_range
201-
"""
202-
203-
_is_recognized_dtype: Callable[[DtypeObj], bool]
204-
_recognized_scalars: Tuple[Type, ...]
205-
206201
# ------------------------------------------------------------------
207202
# NDArrayBackedExtensionArray compat
208203

@@ -861,7 +856,9 @@ def _cmp_method(self, other, op):
861856
# comparison otherwise it would fail to raise when
862857
# comparing tz-aware and tz-naive
863858
with np.errstate(all="ignore"):
864-
result = ops.comp_method_OBJECT_ARRAY(op, self.astype(object), other)
859+
result = ops.comp_method_OBJECT_ARRAY(
860+
op, np.asarray(self.astype(object)), other
861+
)
865862
return result
866863

867864
other_i8 = self._unbox(other)

pandas/core/ops/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
from pandas.core.ops.common import unpack_zerodim_and_defer # noqa:F401
2929
from pandas.core.ops.docstrings import (
3030
_flex_comp_doc_FRAME,
31-
_make_flex_doc,
3231
_op_descriptions,
32+
make_flex_doc,
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
@@ -209,7 +209,7 @@ def align_method_SERIES(left: "Series", right, align_asobject: bool = False):
209209

210210
def flex_method_SERIES(op):
211211
name = op.__name__.strip("_")
212-
doc = _make_flex_doc(name, "series")
212+
doc = make_flex_doc(name, "series")
213213

214214
@Appender(doc)
215215
def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
@@ -445,7 +445,7 @@ def flex_arith_method_FRAME(op):
445445
default_axis = "columns"
446446

447447
na_op = get_array_op(op)
448-
doc = _make_flex_doc(op_name, "dataframe")
448+
doc = make_flex_doc(op_name, "dataframe")
449449

450450
@Appender(doc)
451451
def f(self, other, axis=default_axis, level=None, fill_value=None):

pandas/core/ops/array_ops.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def comp_method_OBJECT_ARRAY(op, x, y):
5757
return result.reshape(x.shape)
5858

5959

60-
def masked_arith_op(x: np.ndarray, y, op):
60+
def _masked_arith_op(x: np.ndarray, y, op):
6161
"""
6262
If the given arithmetic operation fails, attempt it again on
6363
only the non-null elements of the input array(s).
@@ -116,7 +116,7 @@ def masked_arith_op(x: np.ndarray, y, op):
116116
return result
117117

118118

119-
def na_arithmetic_op(left, right, op, is_cmp: bool = False):
119+
def _na_arithmetic_op(left, right, op, is_cmp: bool = False):
120120
"""
121121
Return the result of evaluating op on the passed in values.
122122
@@ -147,7 +147,7 @@ def na_arithmetic_op(left, right, op, is_cmp: bool = False):
147147
# In this case we do not fall back to the masked op, as that
148148
# will handle complex numbers incorrectly, see GH#32047
149149
raise
150-
result = masked_arith_op(left, right, op)
150+
result = _masked_arith_op(left, right, op)
151151

152152
if is_cmp and (is_scalar(result) or result is NotImplemented):
153153
# numpy returned a scalar instead of operating element-wise
@@ -179,15 +179,15 @@ def arithmetic_op(left: ArrayLike, right: Any, op):
179179
# on `left` and `right`.
180180
lvalues = maybe_upcast_datetimelike_array(left)
181181
rvalues = maybe_upcast_datetimelike_array(right)
182-
rvalues = maybe_upcast_for_op(rvalues, lvalues.shape)
182+
rvalues = _maybe_upcast_for_op(rvalues, lvalues.shape)
183183

184184
if should_extension_dispatch(lvalues, rvalues) or isinstance(rvalues, Timedelta):
185185
# Timedelta is included because numexpr will fail on it, see GH#31457
186186
res_values = op(lvalues, rvalues)
187187

188188
else:
189189
with np.errstate(all="ignore"):
190-
res_values = na_arithmetic_op(lvalues, rvalues, op)
190+
res_values = _na_arithmetic_op(lvalues, rvalues, op)
191191

192192
return res_values
193193

@@ -248,7 +248,7 @@ def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
248248
# suppress warnings from numpy about element-wise comparison
249249
warnings.simplefilter("ignore", DeprecationWarning)
250250
with np.errstate(all="ignore"):
251-
res_values = na_arithmetic_op(lvalues, rvalues, op, is_cmp=True)
251+
res_values = _na_arithmetic_op(lvalues, rvalues, op, is_cmp=True)
252252

253253
return res_values
254254

@@ -427,7 +427,7 @@ def maybe_upcast_datetimelike_array(obj: ArrayLike) -> ArrayLike:
427427
return obj
428428

429429

430-
def maybe_upcast_for_op(obj, shape: Tuple[int, ...]):
430+
def _maybe_upcast_for_op(obj, shape: Tuple[int, ...]):
431431
"""
432432
Cast non-pandas objects to pandas types to unify behavior of arithmetic
433433
and comparison operations.

pandas/core/ops/docstrings.py

+1-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Dict, Optional
55

66

7-
def _make_flex_doc(op_name, typ: str):
7+
def make_flex_doc(op_name: str, typ: str) -> str:
88
"""
99
Make the appropriate substitutions for the given operation and class-typ
1010
into either _flex_doc_SERIES or _flex_doc_FRAME to return the docstring
@@ -427,33 +427,6 @@ def _make_flex_doc(op_name, typ: str):
427427
Series.{reverse} : {see_also_desc}.
428428
"""
429429

430-
_arith_doc_FRAME = """
431-
Binary operator %s with support to substitute a fill_value for missing data in
432-
one of the inputs
433-
434-
Parameters
435-
----------
436-
other : Series, DataFrame, or constant
437-
axis : {0, 1, 'index', 'columns'}
438-
For Series input, axis to match Series index on
439-
fill_value : None or float value, default None
440-
Fill existing missing (NaN) values, and any new element needed for
441-
successful DataFrame alignment, with this value before computation.
442-
If data in both corresponding DataFrame locations is missing
443-
the result will be missing
444-
level : int or name
445-
Broadcast across a level, matching Index values on the
446-
passed MultiIndex level
447-
448-
Returns
449-
-------
450-
result : DataFrame
451-
452-
Notes
453-
-----
454-
Mismatched indices will be unioned together
455-
"""
456-
457430
_flex_doc_FRAME = """
458431
Get {desc} of dataframe and other, element-wise (binary operator `{op_name}`).
459432

scripts/validate_unwanted_patterns.py

-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
"_get_version",
3434
"__main__",
3535
"_transform_template",
36-
"_arith_doc_FRAME",
3736
"_flex_comp_doc_FRAME",
38-
"_make_flex_doc",
3937
"_op_descriptions",
4038
"_IntegerDtype",
4139
"_use_inf_as_na",

0 commit comments

Comments
 (0)