Skip to content

Commit d6767d6

Browse files
committed
DEPR: Deprecate outer ufunc in Series.__array_ufunc__
Closes pandas-dev#27186
1 parent 7ab9ff5 commit d6767d6

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

doc/source/getting_started/dsintro.rst

+5
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.

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ Other deprecations
755755
- The :meth:`Series.get_values`, :meth:`DataFrame.get_values`, :meth:`Index.get_values`,
756756
:meth:`SparseArray.get_values` and :meth:`Categorical.get_values` methods are deprecated.
757757
One of ``np.asarray(..)`` or :meth:`~Series.to_numpy` can be used instead (:issue:`19617`).
758+
- The 'outer' method on NumPy ufuncs, e.g. ``np.subtract.outer`` has been deprecated on :class:`Series` objects. Convert the input to an array with :attr:`Series.array` first (:issue:`27186`)
758759
- :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.resolution_string`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`)
759760
- :func:`read_table` has been undeprecated. (:issue:`25220`)
760761
- :attr:`Index.dtype_str` is deprecated. (:issue:`18262`)

pandas/core/series.py

+13
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,19 @@ 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+
if method == 'outer':
786+
msg = (
787+
"outer method for ufunc {} is not implemented on "
788+
"pandas objects. Returning an ndarray, but in the "
789+
"future this will raise a 'NotImplementedError'. "
790+
"Consider explicitly converting the Series "
791+
"to an array with '.array' first."
792+
)
793+
warnings.warn(msg.format(ufunc), FutureWarning,
794+
stacklevel=3)
795+
return result
783796
return self._constructor(result,
784797
index=index,
785798
name=name,

pandas/tests/series/test_ufunc.py

+15
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,18 @@ 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+
with tm.assert_produces_warning(FutureWarning):
321+
result = np.subtract.outer(s, o)
322+
expected = np.array([
323+
[0, -1, -2],
324+
[1, 0, -1],
325+
[2, 1, 0]
326+
])
327+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)