Skip to content

Commit b35adbc

Browse files
committed
BUG: Handle outer ufunc in Series.__array_ufunc__
Closes pandas-dev#27186
1 parent 7ab9ff5 commit b35adbc

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

doc/source/getting_started/dsintro.rst

+12
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,11 @@ The ufunc is applied to the underlying array in a Series.
753753
ser = pd.Series([1, 2, 3, 4])
754754
np.exp(ser)
755755
756+
.. versionchanged:: 0.25.0
757+
758+
When multiple ``Series`` are passed to a ufunc, they are aligned before
759+
performing the operation.
760+
756761
Like other parts of the library, pandas will automatically align labeled inputs
757762
as part of a ufunc with multiple inputs. For example, using :meth:`numpy.remainder`
758763
on two :class:`Series` with differently ordered labels will align before the operation.
@@ -788,6 +793,13 @@ NumPy ufuncs are safe to apply to :class:`Series` backed by non-ndarray arrays,
788793
for example :class:`SparseArray` (see :ref:`sparse.calculation`). If possible,
789794
the ufunc is applied without converting the underlying data to an ndarray.
790795

796+
If the ``outer`` method is used, a NumPy ndarray is returned.
797+
798+
.. ipython:: python
799+
800+
np.subtract.outer(ser, ser)
801+
802+
791803
Console display
792804
~~~~~~~~~~~~~~~
793805

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ def __array_ufunc__(
780780
def construct_return(result):
781781
if lib.is_scalar(result):
782782
return result
783+
elif result.ndim > 1:
784+
# e.g. np.subtract.outer
785+
return result
783786
return self._constructor(result,
784787
index=index,
785788
name=name,

pandas/tests/series/test_ufunc.py

+14
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,17 @@ def __repr__(self):
310310
result = np.add(s, Thing(1))
311311
expected = pd.Series([Thing(2), Thing(3)])
312312
tm.assert_series_equal(result, expected)
313+
314+
315+
def test_outer():
316+
# https://github.com/pandas-dev/pandas/issues/27186
317+
s = pd.Series([1, 2, 3])
318+
o = np.array([1, 2, 3])
319+
320+
result = np.subtract.outer(s, o)
321+
expected = np.array([
322+
[0, -1, -2],
323+
[1, 0, -1],
324+
[2, 1, 0]
325+
])
326+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)