Skip to content

BUG: array ufuncs with more than 2 inputs fails with 2 or more pd.Series and scalar arguments #48280

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 36 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e85a4f8
add test
Aug 22, 2022
29c7c12
add test
Aug 22, 2022
cb013a6
Merge branch 'pandas-dev:main' into main
Aug 23, 2022
0f5ee99
Merge branch 'pandas-dev:main' into main
Aug 24, 2022
8b84b83
add test
Aug 24, 2022
21e30ac
revert
Aug 24, 2022
e36efb2
revert
Aug 24, 2022
833a9cc
Merge branch 'main' into interval_dtype_38316
Aug 24, 2022
3273feb
add test
Aug 24, 2022
0b9a429
add test
Aug 24, 2022
6f530f4
Merge branch 'main' into interval_dtype_38316
Aug 25, 2022
a833d28
Merge branch 'main' into interval_dtype_38316
Aug 25, 2022
c6ff57b
add test
Aug 27, 2022
c8e4406
revert
Aug 27, 2022
87d0f52
Merge branch 'main' into array_ufunc_39853
Aug 27, 2022
05a06cf
add test
Aug 28, 2022
756c1cf
Merge branch 'main' into array_ufunc_39853
Aug 29, 2022
715fa96
add test
Aug 30, 2022
d399b77
Merge branch 'main' into array_ufunc_39853
Aug 31, 2022
993c0ff
Merge branch 'main' into array_ufunc_39853
Aug 31, 2022
ecae8ef
Merge branch 'main' of https://github.com/weikhor/pandas into array_u…
Sep 1, 2022
db2c34a
add
Sep 1, 2022
5ceb0f7
add whatsnew
Sep 1, 2022
d57fea8
Merge branch 'main' into array_ufunc_39853
Sep 1, 2022
4ba3030
add whatsnew
Sep 1, 2022
a7c4b71
Merge branch 'array_ufunc_39853' of https://github.com/weikhor/pandas…
Sep 1, 2022
1e63699
add whatsnew
Sep 1, 2022
52cc50b
Merge branch 'main' into array_ufunc_39853
Sep 1, 2022
1b29ab2
Merge branch 'main' into array_ufunc_39853
Sep 1, 2022
5134632
add doc
Sep 6, 2022
cc0d441
Merge branch 'main' into array_ufunc_39853
Sep 6, 2022
68c021f
add test
Sep 10, 2022
ef5af72
Merge branch 'main' into array_ufunc_39853
Sep 10, 2022
5ef68f8
Merge branch 'main' into array_ufunc_39853
Sep 16, 2022
aeb1b91
Merge branch 'main' of https://github.com/weikhor/pandas into array_u…
Oct 2, 2022
657fc70
Merge branch 'main' into array_ufunc_39853
Oct 9, 2022
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Timezones

Numeric
^^^^^^^
-
- Bug in :meth:`DataFrame.add` cannot apply ufunc when inputs contain mixed DataFrame type and Series type (:issue:`39853`)
-

Conversion
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/arraylike.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
--------
numpy.org/doc/stable/reference/arrays.classes.html#numpy.class.__array_ufunc__
"""
from pandas.core.frame import (
DataFrame,
Series,
)
from pandas.core.generic import NDFrame
from pandas.core.internals import BlockManager

Expand Down Expand Up @@ -295,8 +299,8 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
# At the moment, there aren't any ufuncs with more than two inputs
# so this ends up just being x1.index | x2.index, but we write
# it to handle *args.

if len(set(types)) > 1:
set_types = set(types)
if len(set_types) > 1 and {DataFrame, Series}.issubset(set_types):
# We currently don't handle ufunc(DataFrame, Series)
# well. Previously this raised an internal ValueError. We might
# support it someday, so raise a NotImplementedError.
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/test_ufunc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import partial
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -301,3 +302,24 @@ def my_ufunc(x, y, z):
result = my_ufunc(df1.values, df2, df3)
expected = expected.set_axis(["b", "c"], axis=1)
tm.assert_frame_equal(result, expected)


def test_array_ufuncs_for_many_arguments():
# GH39853
def add3(x, y, z):
return x + y + z

ufunc = np.frompyfunc(add3, 3, 1)
df = pd.DataFrame([[1, 2], [3, 4]])

result = ufunc(df, df, 1)
expected = pd.DataFrame([[3, 5], [7, 9]], dtype=object)
tm.assert_frame_equal(result, expected)

ser = pd.Series([1, 2])
msg = (
"Cannot apply ufunc <ufunc 'add3 (vectorized)'> "
"to mixed DataFrame and Series inputs."
)
with pytest.raises(NotImplementedError, match=re.escape(msg)):
ufunc(df, df, ser)
23 changes: 23 additions & 0 deletions pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import deque
import re
import string

import numpy as np
Expand Down Expand Up @@ -453,3 +454,25 @@ def test_np_matmul():
expected_result,
result,
)


def test_array_ufuncs_for_many_arguments():
# GH39853
def add3(x, y, z):
return x + y + z

ufunc = np.frompyfunc(add3, 3, 1)
ser = pd.Series([1, 2])

result = ufunc(ser, ser, 1)
expected = pd.Series([3, 5], dtype=object)
tm.assert_series_equal(result, expected)

df = pd.DataFrame([[1, 2]])

msg = (
"Cannot apply ufunc <ufunc 'add3 (vectorized)'> "
"to mixed DataFrame and Series inputs."
)
with pytest.raises(NotImplementedError, match=re.escape(msg)):
ufunc(ser, ser, df)