From aaaccf198be688d38495b6f84d7c65b34aba781a Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 13 Oct 2020 17:58:01 -0700 Subject: [PATCH] CLN: ops, unnecessary mixin --- pandas/core/arrays/datetimelike.py | 39 +++++++++++++-------------- pandas/core/ops/__init__.py | 6 ++--- pandas/core/ops/array_ops.py | 14 +++++----- pandas/core/ops/docstrings.py | 29 +------------------- scripts/validate_unwanted_patterns.py | 2 -- 5 files changed, 29 insertions(+), 61 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index eb5e5b03fe243..dd9adc59c909b 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -75,6 +75,7 @@ from pandas.core.arrays import DatetimeArray, TimedeltaArray DTScalarOrNaT = Union[DatetimeLikeScalar, NaTType] +DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin") class InvalidComparison(Exception): @@ -86,7 +87,20 @@ class InvalidComparison(Exception): pass -class AttributesMixin: +class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray): + """ + Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray + + Assumes that __new__/__init__ defines: + _data + _freq + + and that the inheriting class has methods: + _generate_range + """ + + _is_recognized_dtype: Callable[[DtypeObj], bool] + _recognized_scalars: Tuple[Type, ...] _data: np.ndarray def __init__(self, data, dtype=None, freq=None, copy=False): @@ -184,25 +198,6 @@ def _check_compatible_with( """ raise AbstractMethodError(self) - -DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin") - - -class DatetimeLikeArrayMixin(OpsMixin, AttributesMixin, NDArrayBackedExtensionArray): - """ - Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray - - Assumes that __new__/__init__ defines: - _data - _freq - - and that the inheriting class has methods: - _generate_range - """ - - _is_recognized_dtype: Callable[[DtypeObj], bool] - _recognized_scalars: Tuple[Type, ...] - # ------------------------------------------------------------------ # NDArrayBackedExtensionArray compat @@ -861,7 +856,9 @@ def _cmp_method(self, other, op): # comparison otherwise it would fail to raise when # comparing tz-aware and tz-naive with np.errstate(all="ignore"): - result = ops.comp_method_OBJECT_ARRAY(op, self.astype(object), other) + result = ops.comp_method_OBJECT_ARRAY( + op, np.asarray(self.astype(object)), other + ) return result other_i8 = self._unbox(other) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 69b5abe65057a..27b6ad37bb612 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -28,8 +28,8 @@ from pandas.core.ops.common import unpack_zerodim_and_defer # noqa:F401 from pandas.core.ops.docstrings import ( _flex_comp_doc_FRAME, - _make_flex_doc, _op_descriptions, + make_flex_doc, ) from pandas.core.ops.invalid import invalid_comparison # noqa:F401 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): def flex_method_SERIES(op): name = op.__name__.strip("_") - doc = _make_flex_doc(name, "series") + doc = make_flex_doc(name, "series") @Appender(doc) def flex_wrapper(self, other, level=None, fill_value=None, axis=0): @@ -445,7 +445,7 @@ def flex_arith_method_FRAME(op): default_axis = "columns" na_op = get_array_op(op) - doc = _make_flex_doc(op_name, "dataframe") + doc = make_flex_doc(op_name, "dataframe") @Appender(doc) def f(self, other, axis=default_axis, level=None, fill_value=None): diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index fd5f126051c53..97fa7988c1774 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -57,7 +57,7 @@ def comp_method_OBJECT_ARRAY(op, x, y): return result.reshape(x.shape) -def masked_arith_op(x: np.ndarray, y, op): +def _masked_arith_op(x: np.ndarray, y, op): """ If the given arithmetic operation fails, attempt it again on only the non-null elements of the input array(s). @@ -116,7 +116,7 @@ def masked_arith_op(x: np.ndarray, y, op): return result -def na_arithmetic_op(left, right, op, is_cmp: bool = False): +def _na_arithmetic_op(left, right, op, is_cmp: bool = False): """ Return the result of evaluating op on the passed in values. @@ -147,7 +147,7 @@ def na_arithmetic_op(left, right, op, is_cmp: bool = False): # In this case we do not fall back to the masked op, as that # will handle complex numbers incorrectly, see GH#32047 raise - result = masked_arith_op(left, right, op) + result = _masked_arith_op(left, right, op) if is_cmp and (is_scalar(result) or result is NotImplemented): # numpy returned a scalar instead of operating element-wise @@ -179,7 +179,7 @@ def arithmetic_op(left: ArrayLike, right: Any, op): # on `left` and `right`. lvalues = maybe_upcast_datetimelike_array(left) rvalues = maybe_upcast_datetimelike_array(right) - rvalues = maybe_upcast_for_op(rvalues, lvalues.shape) + rvalues = _maybe_upcast_for_op(rvalues, lvalues.shape) if should_extension_dispatch(lvalues, rvalues) or isinstance(rvalues, Timedelta): # Timedelta is included because numexpr will fail on it, see GH#31457 @@ -187,7 +187,7 @@ def arithmetic_op(left: ArrayLike, right: Any, op): else: with np.errstate(all="ignore"): - res_values = na_arithmetic_op(lvalues, rvalues, op) + res_values = _na_arithmetic_op(lvalues, rvalues, op) return res_values @@ -248,7 +248,7 @@ def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike: # suppress warnings from numpy about element-wise comparison warnings.simplefilter("ignore", DeprecationWarning) with np.errstate(all="ignore"): - res_values = na_arithmetic_op(lvalues, rvalues, op, is_cmp=True) + res_values = _na_arithmetic_op(lvalues, rvalues, op, is_cmp=True) return res_values @@ -427,7 +427,7 @@ def maybe_upcast_datetimelike_array(obj: ArrayLike) -> ArrayLike: return obj -def maybe_upcast_for_op(obj, shape: Tuple[int, ...]): +def _maybe_upcast_for_op(obj, shape: Tuple[int, ...]): """ Cast non-pandas objects to pandas types to unify behavior of arithmetic and comparison operations. diff --git a/pandas/core/ops/docstrings.py b/pandas/core/ops/docstrings.py index 839bdbfb2444a..08b7b0e89ea5f 100644 --- a/pandas/core/ops/docstrings.py +++ b/pandas/core/ops/docstrings.py @@ -4,7 +4,7 @@ from typing import Dict, Optional -def _make_flex_doc(op_name, typ: str): +def make_flex_doc(op_name: str, typ: str) -> str: """ Make the appropriate substitutions for the given operation and class-typ 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): Series.{reverse} : {see_also_desc}. """ -_arith_doc_FRAME = """ -Binary operator %s with support to substitute a fill_value for missing data in -one of the inputs - -Parameters ----------- -other : Series, DataFrame, or constant -axis : {0, 1, 'index', 'columns'} - For Series input, axis to match Series index on -fill_value : None or float value, default None - Fill existing missing (NaN) values, and any new element needed for - successful DataFrame alignment, with this value before computation. - If data in both corresponding DataFrame locations is missing - the result will be missing -level : int or name - Broadcast across a level, matching Index values on the - passed MultiIndex level - -Returns -------- -result : DataFrame - -Notes ------ -Mismatched indices will be unioned together -""" - _flex_doc_FRAME = """ Get {desc} of dataframe and other, element-wise (binary operator `{op_name}`). diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py index b6ffab1482bbc..119ae1cce2ff0 100755 --- a/scripts/validate_unwanted_patterns.py +++ b/scripts/validate_unwanted_patterns.py @@ -33,9 +33,7 @@ "_get_version", "__main__", "_transform_template", - "_arith_doc_FRAME", "_flex_comp_doc_FRAME", - "_make_flex_doc", "_op_descriptions", "_IntegerDtype", "_use_inf_as_na",