Skip to content

Commit 770f1e6

Browse files
fc
Signed-off-by: Praateek <[email protected]>
1 parent e4ca405 commit 770f1e6

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6034,7 +6034,7 @@ def _flex_method(self, other, op, *, level=None, fill_value=None, axis: Axis = 0
60346034

60356035
if isinstance(other, Series):
60366036
return self._binop(other, op, level=level, fill_value=fill_value)
6037-
elif isinstance(other, (np.ndarray, list, tuple)):
6037+
elif isinstance(other, (np.ndarray, list, tuple, ExtensionArray)):
60386038
if len(other) != len(self):
60396039
raise ValueError("Lengths must be equal")
60406040
other = self._constructor(other, self.index, copy=False)

pandas/tests/series/test_arithmetic.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,41 @@ def _check_fill(meth, op, a, b, fill_value=0):
155155
# should accept axis=0 or axis='rows'
156156
op(a, b, axis=0)
157157

158+
def test_extarray_rhs_datetime_sub_with_fill_value(self):
159+
# Ensure ExtensionArray (DatetimeArray) RHS is handled via array-like path
160+
# and does not hit scalar isna branch.
161+
left = Series(
162+
[
163+
pd.Timestamp("2025-08-20"),
164+
pd.Timestamp("2025-08-21"),
165+
pd.Timestamp("2025-08-22"),
166+
],
167+
dtype=np.dtype("datetime64[ns]")
168+
)
169+
right = left._values # DatetimeArray
170+
171+
result = left.sub(right, fill_value=left.iloc[0])
172+
# result dtype may vary (e.g., seconds vs ns), build expected from result dtype
173+
expected = Series(np.zeros(3, dtype=np.dtype("timedelta64[ns]")))
174+
tm.assert_series_equal(result, expected)
175+
176+
def test_extarray_rhs_timedelta_sub_with_fill_value(self):
177+
left = Series([Timedelta(days=1), Timedelta(days=2), Timedelta(days=3)], dtype=np.dtype("timedelta64[ns]"))
178+
right = left._values # TimedeltaArray
179+
180+
result = left.sub(right, fill_value=left.iloc[0])
181+
expected = Series(np.zeros(3, dtype=np.dtype("timedelta64[ns]")))
182+
tm.assert_series_equal(result, expected)
183+
184+
def test_extarray_rhs_period_eq_with_fill_value(self):
185+
# Use equality to validate ExtensionArray RHS path for PeriodArray
186+
left = Series(pd.period_range("2020Q1", periods=3, freq="Q"))
187+
right = left._values # PeriodArray
188+
189+
result = left.eq(right, fill_value=left.iloc[0])
190+
expected = Series([True, True, True])
191+
tm.assert_series_equal(result, expected)
192+
158193

159194
class TestSeriesArithmetic:
160195
# Some of these may end up in tests/arithmetic, but are not yet sorted
@@ -404,6 +439,15 @@ def test_comparison_flex_alignment(self, values, op):
404439
expected = Series(values, index=list("abcd"))
405440
tm.assert_series_equal(result, expected)
406441

442+
def test_extarray_rhs_categorical_eq_with_fill_value(self):
443+
# Categorical RHS should be treated as array-like, not as scalar
444+
left = Series(Categorical(["a", "b", "a"]))
445+
right = left._values # Categorical
446+
447+
result = left.eq(right, fill_value=left.iloc[0])
448+
expected = Series([True, True, True])
449+
tm.assert_series_equal(result, expected)
450+
407451
@pytest.mark.parametrize(
408452
"values, op, fill_value",
409453
[

0 commit comments

Comments
 (0)