Skip to content
Draft
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/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6034,7 +6034,7 @@ def _flex_method(self, other, op, *, level=None, fill_value=None, axis: Axis = 0

if isinstance(other, Series):
return self._binop(other, op, level=level, fill_value=fill_value)
elif isinstance(other, (np.ndarray, list, tuple)):
elif isinstance(other, (np.ndarray, list, tuple, ExtensionArray)):
if len(other) != len(self):
raise ValueError("Lengths must be equal")
other = self._constructor(other, self.index, copy=False)
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ def _check_fill(meth, op, a, b, fill_value=0):
# should accept axis=0 or axis='rows'
op(a, b, axis=0)

@pytest.mark.parametrize("kind", ["datetime", "timedelta"])
def test_rhs_extension_array_sub_with_fill_value(self, kind):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment # GH#??? pointing back to the original issue pls

if kind == "datetime":
left = Series(
[pd.Timestamp("2025-08-20"), pd.Timestamp("2025-08-21")],
dtype=np.dtype("datetime64[ns]"),
)
else:
left = Series(
[Timedelta(days=1), Timedelta(days=2)],
dtype=np.dtype("timedelta64[ns]"),
)

right = (
left._values
) # DatetimeArray or TimedeltaArray which is an ExtensionArray

result = left.sub(right, fill_value=left.iloc[0])
expected = Series(np.zeros(len(left), dtype=np.dtype("timedelta64[ns]")))
tm.assert_series_equal(result, expected)


class TestSeriesArithmetic:
# Some of these may end up in tests/arithmetic, but are not yet sorted
Expand Down Expand Up @@ -404,6 +425,21 @@ def test_comparison_flex_alignment(self, values, op):
expected = Series(values, index=list("abcd"))
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"left",
[
Series(Categorical(["a", "b", "a"])),
Series(pd.period_range("2020Q1", periods=3, freq="Q")),
],
ids=["categorical", "period"],
)
def test_rhs_extension_array_eq_with_fill_value(self, left):
right = left._values # this is an ExtensionArray

result = left.eq(right, fill_value=left.iloc[0])
expected = Series([True, True, True])
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"values, op, fill_value",
[
Expand Down
Loading