-
-
Notifications
You must be signed in to change notification settings - Fork 19k
Add __array_ufunc__ to Series / Array #23293
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
Changes from 1 commit
2fffac3
c5a4664
dd332a4
71c058e
a0d11d9
4cfeb9b
607f8a6
c4fcae7
65dea1b
134df14
5239b70
3d91885
429f15c
41f4158
0d6a663
8f46391
44e3c7e
e179913
27208c1
0b1e745
9be1dff
775c2ef
4d7f249
0b359d7
bbbf269
64d8908
d1788b0
ef5d508
fe0ee4e
971e347
95e8aef
7bfd584
06e5739
feee015
3702b9b
a0f84ed
d83fe7a
edad466
e4ae8dc
db60f6c
a9bd6ef
1a8b807
0b0466d
1f67866
d3089bd
6e770e8
15a3fb1
b5e7f45
4f4bd93
5dbff49
b623be2
2237233
5b5c547
10bc2cc
5380b77
9f4d110
6c15ee7
ab48bd8
30fced8
7486d26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import numbers | ||
import sys | ||
import warnings | ||
import copy | ||
|
@@ -293,6 +294,33 @@ def __array__(self, dtype=None): | |
""" | ||
return self._coerce_to_ndarray() | ||
|
||
_HANDLED_TYPES = (np.ndarray, numbers.Number) | ||
|
||
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | ||
|
||
out = kwargs.get('out', ()) | ||
|
||
for x in inputs + out: | ||
if not isinstance(x, self._HANDLED_TYPES + (IntegerArray,)): | ||
return NotImplemented | ||
|
||
if method == '__call__': | ||
if ufunc.signature is None and ufunc.nout == 1: | ||
args = [a._data for a in inputs] | ||
masks = [a._mask for a in inputs] | ||
result = ufunc(*args, **kwargs) | ||
mask = np.logical_or.reduce(masks) | ||
if result.dtype.kind in ('i', 'u'): | ||
return IntegerArray(result, mask) | ||
else: | ||
result[mask] = np.nan | ||
return result | ||
|
||
# fall back to array for other ufuncs | ||
|
||
return np.array(self).__array_ufunc__( | ||
|
||
ufunc, method, *inputs, **kwargs) | ||
return NotImplemented | ||
|
||
def __iter__(self): | ||
for i in range(len(self)): | ||
if self._mask[i]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -623,6 +623,23 @@ def view(self, dtype=None): | |
return self._constructor(self._values.view(dtype), | ||
index=self.index).__finalize__(self) | ||
|
||
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | ||
TomAugspurger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
inputs = tuple( | ||
|
||
x._values if isinstance(x, type(self)) else x | ||
|
||
for x in inputs | ||
) | ||
|
||
if hasattr(self._values, '__array_ufunc__'): | ||
result = self._values.__array_ufunc__( | ||
ufunc, method, *inputs, **kwargs) | ||
else: | ||
result = np.array(self._values).__array_ufunc__( | ||
ufunc, method, *inputs, **kwargs) | ||
if result is NotImplemented: | ||
raise TypeError("The '{0}' operation is not supported for " | ||
"dtype {1}.".format(ufunc.__name__, self.dtype)) | ||
|
||
return self._constructor(result, index=self.index, | ||
|
||
copy=False).__finalize__(self) | ||
|
||
def __array__(self, result=None): | ||
""" | ||
the array interface, return my values | ||
|
@@ -640,10 +657,9 @@ def __array_prepare__(self, result, context=None): | |
""" | ||
Gets called prior to a ufunc | ||
""" | ||
|
||
# nice error message for non-ufunc types | ||
if (context is not None and | ||
not isinstance(self._values, (np.ndarray, ABCSparseArray))): | ||
not isinstance(self._values, (np.ndarray, ExtensionArray))): | ||
obj = context[1][0] | ||
raise TypeError("{obj} with dtype {dtype} cannot perform " | ||
"the numpy op {op}".format( | ||
|
Uh oh!
There was an error while loading. Please reload this page.