Skip to content

REF: move __array_ufunc__ to base NumericArray #38412

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
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
50 changes: 0 additions & 50 deletions pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numbers
from typing import List, Optional, Tuple, Type
import warnings

Expand All @@ -22,7 +21,6 @@
from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype
from pandas.core.dtypes.missing import isna

from pandas.core import ops
from pandas.core.ops import invalid_comparison
from pandas.core.tools.numeric import to_numeric

Expand Down Expand Up @@ -255,54 +253,6 @@ def _from_sequence_of_strings(
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

_HANDLED_TYPES = (np.ndarray, numbers.Number)

def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
# For FloatingArray inputs, we apply the ufunc to ._data
# and mask the result.
if method == "reduce":
# Not clear how to handle missing values in reductions. Raise.
raise NotImplementedError("The 'reduce' method is not supported.")
out = kwargs.get("out", ())

for x in inputs + out:
if not isinstance(x, self._HANDLED_TYPES + (FloatingArray,)):
return NotImplemented

# for binary ops, use our custom dunder methods
result = ops.maybe_dispatch_ufunc_to_dunder_op(
self, ufunc, method, *inputs, **kwargs
)
if result is not NotImplemented:
return result

mask = np.zeros(len(self), dtype=bool)
inputs2 = []
for x in inputs:
if isinstance(x, FloatingArray):
mask |= x._mask
inputs2.append(x._data)
else:
inputs2.append(x)

def reconstruct(x):
# we don't worry about scalar `x` here, since we
# raise for reduce up above.

# TODO
if is_float_dtype(x.dtype):
m = mask.copy()
return FloatingArray(x, m)
else:
x[mask] = np.nan
return x

result = getattr(ufunc, method)(*inputs2, **kwargs)
if isinstance(result, tuple):
tuple(reconstruct(x) for x in result)
else:
return reconstruct(result)

def _coerce_to_array(self, value) -> Tuple[np.ndarray, np.ndarray]:
return coerce_to_array(value, dtype=self.dtype)

Expand Down
49 changes: 0 additions & 49 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numbers
from typing import Dict, List, Optional, Tuple, Type
import warnings

Expand All @@ -22,7 +21,6 @@
)
from pandas.core.dtypes.missing import isna

from pandas.core import ops
from pandas.core.ops import invalid_comparison
from pandas.core.tools.numeric import to_numeric

Expand Down Expand Up @@ -316,53 +314,6 @@ def _from_sequence_of_strings(
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

_HANDLED_TYPES = (np.ndarray, numbers.Number)

def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
# For IntegerArray inputs, we apply the ufunc to ._data
# and mask the result.
if method == "reduce":
# Not clear how to handle missing values in reductions. Raise.
raise NotImplementedError("The 'reduce' method is not supported.")
out = kwargs.get("out", ())

for x in inputs + out:
if not isinstance(x, self._HANDLED_TYPES + (IntegerArray,)):
return NotImplemented

# for binary ops, use our custom dunder methods
result = ops.maybe_dispatch_ufunc_to_dunder_op(
self, ufunc, method, *inputs, **kwargs
)
if result is not NotImplemented:
return result

mask = np.zeros(len(self), dtype=bool)
inputs2 = []
for x in inputs:
if isinstance(x, IntegerArray):
mask |= x._mask
inputs2.append(x._data)
else:
inputs2.append(x)

def reconstruct(x):
# we don't worry about scalar `x` here, since we
# raise for reduce up above.

if is_integer_dtype(x.dtype):
m = mask.copy()
return IntegerArray(x, m)
else:
x[mask] = np.nan
return x

result = getattr(ufunc, method)(*inputs2, **kwargs)
if isinstance(result, tuple):
return tuple(reconstruct(x) for x in result)
else:
return reconstruct(result)

def _coerce_to_array(self, value) -> Tuple[np.ndarray, np.ndarray]:
return coerce_to_array(value, dtype=self.dtype)

Expand Down
59 changes: 58 additions & 1 deletion pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from typing import TYPE_CHECKING, Union
import numbers
from typing import TYPE_CHECKING, Any, List, Union

import numpy as np

Expand All @@ -14,6 +15,8 @@
is_list_like,
)

from pandas.core import ops

from .masked import BaseMaskedArray, BaseMaskedDtype

if TYPE_CHECKING:
Expand Down Expand Up @@ -130,3 +133,57 @@ def _arith_method(self, other, op):
)

return self._maybe_mask_result(result, mask, other, op_name)

_HANDLED_TYPES = (np.ndarray, numbers.Number)

def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
# For NumericArray inputs, we apply the ufunc to ._data
# and mask the result.
if method == "reduce":
# Not clear how to handle missing values in reductions. Raise.
raise NotImplementedError("The 'reduce' method is not supported.")
out = kwargs.get("out", ())

for x in inputs + out:
if not isinstance(x, self._HANDLED_TYPES + (NumericArray,)):
return NotImplemented

# for binary ops, use our custom dunder methods
result = ops.maybe_dispatch_ufunc_to_dunder_op(
self, ufunc, method, *inputs, **kwargs
)
if result is not NotImplemented:
return result

mask = np.zeros(len(self), dtype=bool)
inputs2: List[Any] = []
for x in inputs:
if isinstance(x, NumericArray):
mask |= x._mask
inputs2.append(x._data)
else:
inputs2.append(x)

def reconstruct(x):
# we don't worry about scalar `x` here, since we
# raise for reduce up above.

if is_integer_dtype(x.dtype):
from pandas.core.arrays import IntegerArray

m = mask.copy()
return IntegerArray(x, m)
elif is_float_dtype(x.dtype):
from pandas.core.arrays import FloatingArray

m = mask.copy()
return FloatingArray(x, m)
else:
x[mask] = np.nan
return x

result = getattr(ufunc, method)(*inputs2, **kwargs)
if isinstance(result, tuple):
return tuple(reconstruct(x) for x in result)
else:
return reconstruct(result)
12 changes: 6 additions & 6 deletions pandas/tests/arrays/integer/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import FloatingArray


@pytest.mark.parametrize("ufunc", [np.abs, np.sign])
Expand All @@ -25,13 +26,13 @@ def test_ufuncs_single_float(ufunc):
a = pd.array([1, 2, -3, np.nan])
with np.errstate(invalid="ignore"):
result = ufunc(a)
expected = ufunc(a.astype(float))
tm.assert_numpy_array_equal(result, expected)
expected = FloatingArray(ufunc(a.astype(float)), mask=a._mask)
tm.assert_extension_array_equal(result, expected)

s = pd.Series(a)
with np.errstate(invalid="ignore"):
result = ufunc(s)
expected = ufunc(s.astype(float))
expected = pd.Series(expected)
tm.assert_series_equal(result, expected)


Expand Down Expand Up @@ -67,14 +68,13 @@ def test_ufunc_binary_output():
a = pd.array([1, 2, np.nan])
result = np.modf(a)
expected = np.modf(a.to_numpy(na_value=np.nan, dtype="float"))
expected = (pd.array(expected[0]), pd.array(expected[1]))

assert isinstance(result, tuple)
assert len(result) == 2

for x, y in zip(result, expected):
# TODO(FloatArray): This will return an extension array.
# y = pd.array(y)
tm.assert_numpy_array_equal(x, y)
tm.assert_extension_array_equal(x, y)


@pytest.mark.parametrize("values", [[0, 1], [0, None]])
Expand Down