Skip to content

DEPR: ExtensionOpsMixin -> OpsMixin #38142

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 5 commits into from
Nov 29, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ Deprecations
- Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`.DatetimeIndex`, :class:`.TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`)
- The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)
- The ``null_counts`` parameter of :meth:`DataFrame.info` is deprecated and replaced by ``show_counts``. It will be removed in a future version (:issue:`37999`)
- :class:`ExtensionOpsMixin` and :class:`ExtensionScalarOpsMixin` are deprecated and will be removed in a future version. Use ``pd.core.arraylike.OpsMixin`` instead (:issue:`37080`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't ever document something in pandas.core as public. If this is going to change, it should be exposed outside of core.


.. ---------------------------------------------------------------------------

Expand Down
16 changes: 16 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Union,
cast,
)
import warnings

import numpy as np

Expand Down Expand Up @@ -1219,6 +1220,21 @@ class ExtensionOpsMixin:
with NumPy arrays.
"""

def __init_subclass__(cls, **kwargs):
# We use __init_subclass__ to handle deprecations
super().__init_subclass__()

if cls.__name__ != "ExtensionScalarOpsMixin":
# We only want to warn for user-defined subclasses,
# and cannot reference ExtensionScalarOpsMixin directly at this point.
warnings.warn(
"ExtensionOpsMixin and ExtensionScalarOpsMixin are deprecated "
"and will be removed in a future version. Use "
"pd.core.arraylike.OpsMixin instead.",
FutureWarning,
stacklevel=2,
)

@classmethod
def _create_arithmetic_method(cls, op):
raise AbstractMethodError(cls)
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/arrays/test_deprecations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pandas._testing as tm
from pandas.core.arrays import (
ExtensionArray,
ExtensionOpsMixin,
ExtensionScalarOpsMixin,
)


def test_extension_ops_mixin_deprecated():
# GH#37080 deprecated in favor of OpsMixin
with tm.assert_produces_warning(FutureWarning):

class MySubclass(ExtensionOpsMixin, ExtensionArray):
pass

with tm.assert_produces_warning(FutureWarning):

class MyOtherSubclass(ExtensionScalarOpsMixin, ExtensionArray):
pass
44 changes: 39 additions & 5 deletions pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import numpy as np

from pandas.core.dtypes.base import ExtensionDtype
from pandas.core.dtypes.cast import maybe_cast_to_extension_array
from pandas.core.dtypes.common import is_dtype_equal, is_list_like, pandas_dtype

import pandas as pd
from pandas.api.extensions import no_default, register_extension_dtype
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays import ExtensionArray, ExtensionScalarOpsMixin
from pandas.core.arrays import ExtensionArray
from pandas.core.indexers import check_array_indexer


Expand Down Expand Up @@ -45,7 +46,7 @@ def _is_numeric(self) -> bool:
return True


class DecimalArray(OpsMixin, ExtensionScalarOpsMixin, ExtensionArray):
class DecimalArray(OpsMixin, ExtensionArray):
__array_priority__ = 1000

def __init__(self, values, dtype=None, copy=False, context=None):
Expand Down Expand Up @@ -217,13 +218,46 @@ def convert_values(param):

return np.asarray(res, dtype=bool)

_do_coerce = True # overriden in DecimalArrayWithoutCoercion

def _arith_method(self, other, op):
def convert_values(param):
if isinstance(param, ExtensionArray) or is_list_like(param):
ovalues = param
else: # Assume its an object
ovalues = [param] * len(self)
return ovalues

lvalues = self
rvalues = convert_values(other)

# If the operator is not defined for the underlying objects,
# a TypeError should be raised
res = [op(a, b) for (a, b) in zip(lvalues, rvalues)]

def _maybe_convert(arr):
if self._do_coerce:
# https://github.com/pandas-dev/pandas/issues/22850
# We catch all regular exceptions here, and fall back
# to an ndarray.
res = maybe_cast_to_extension_array(type(self), arr)
if not isinstance(res, type(self)):
# exception raised in _from_sequence; ensure we have ndarray
res = np.asarray(arr)
else:
res = np.asarray(arr)
return res

if op.__name__ in {"divmod", "rdivmod"}:
a, b = zip(*res)
return _maybe_convert(a), _maybe_convert(b)

return _maybe_convert(res)


def to_decimal(values, context=None):
return DecimalArray([decimal.Decimal(x) for x in values], context=context)


def make_data():
return [decimal.Decimal(random.random()) for _ in range(100)]


DecimalArray._add_arithmetic_ops()
7 changes: 1 addition & 6 deletions pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,7 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):


class DecimalArrayWithoutCoercion(DecimalArrayWithoutFromSequence):
@classmethod
def _create_arithmetic_method(cls, op):
return cls._create_method(op, coerce_to_dtype=False)


DecimalArrayWithoutCoercion._add_arithmetic_ops()
_do_coerce = False


def test_combine_from_sequence_raises():
Expand Down