Skip to content

REGR: fix op(frame, series) with extension dtypes #34312

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

Closed
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
2 changes: 1 addition & 1 deletion pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def _combine_series_frame(left, right, func, axis: int):
# We assume that self.align(other, ...) has already been called

rvalues = right._values
if isinstance(rvalues, np.ndarray):
if not left._mgr.any_extension_types and isinstance(rvalues, np.ndarray):
# TODO(EA2D): no need to special-case with 2D EAs
# We can operate block-wise
if axis == 0:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,3 +1309,18 @@ def test_dataframe_div_silenced():
)
with tm.assert_produces_warning(None):
pdf1.div(pdf2, fill_value=0)


def test_dataframe_series_extension_dtypes():
# https://github.com/pandas-dev/pandas/issues/34311
df = pd.DataFrame(np.random.randint(0, 100, (10, 3)), columns=["a", "b", "c"])
s = pd.Series([1, 2, 3], index=["a", "b", "c"])

expected = df.to_numpy("int64") + s.to_numpy("int64").reshape(-1, 3)
expected = pd.DataFrame(expected, columns=df.columns, dtype="Int64")

df_ea = df.astype("Int64")
result = df_ea + s
tm.assert_frame_equal(result, expected)
result = df_ea + s.astype("Int64")
tm.assert_frame_equal(result, expected)
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname):
result = getattr(empty, opname)(const).dtypes.value_counts()
tm.assert_series_equal(result, pd.Series([2], index=[np.dtype(bool)]))

def test_df_flex_cmp_ea_dtype_with_ndarray_series(self):
ii = pd.IntervalIndex.from_breaks([1, 2, 3])
df = pd.DataFrame({"A": ii, "B": ii})

ser = pd.Series([0, 0])
res = df.eq(ser, axis=0)

expected = pd.DataFrame({"A": [False, False], "B": [False, False]})
tm.assert_frame_equal(res, expected)

ser2 = pd.Series([1, 2], index=["A", "B"])
res2 = df.eq(ser2, axis=1)
tm.assert_frame_equal(res2, expected)


# -------------------------------------------------------------------
# Arithmetic
Expand Down