Skip to content

Commit 531c0e3

Browse files
authored
API: Disallow DataFrame other for Series flex ops (#62648)
1 parent c3fce1a commit 531c0e3

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ Other API changes
655655
an empty ``RangeIndex`` or empty ``Index`` with object dtype when determining
656656
the dtype of the resulting Index (:issue:`60797`)
657657
- :class:`IncompatibleFrequency` now subclasses ``TypeError`` instead of ``ValueError``. As a result, joins with mismatched frequencies now cast to object like other non-comparable joins, and arithmetic with indexes with mismatched frequencies align (:issue:`55782`)
658+
- :class:`Series` "flex" methods like :meth:`Series.add` no longer allow passing a :class:`DataFrame` for ``other``; use the DataFrame reversed method instead (:issue:`46179`)
658659
- :meth:`CategoricalIndex.append` no longer attempts to cast different-dtype indexes to the caller's dtype (:issue:`41626`)
659660
- :meth:`ExtensionDtype.construct_array_type` is now a regular method instead of a ``classmethod`` (:issue:`58663`)
660661
- Comparison operations between :class:`Index` and :class:`Series` now consistently return :class:`Series` regardless of which object is on the left or right (:issue:`36759`)

pandas/core/series.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6069,6 +6069,12 @@ def _flex_method(self, other, op, *, level=None, fill_value=None, axis: Axis = 0
60696069
result = self._binop(other, op, level=level, fill_value=fill_value)
60706070
result._name = res_name
60716071
return result
6072+
elif isinstance(other, ABCDataFrame):
6073+
# GH#46179
6074+
raise TypeError(
6075+
f"Series.{op.__name__.strip('_')} does not support a DataFrame "
6076+
f"`other`. Use df.{op.__name__.strip('_')}(ser) instead."
6077+
)
60726078
else:
60736079
if fill_value is not None:
60746080
if isna(other):

pandas/tests/series/test_arithmetic.py

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

159+
def test_flex_disallows_dataframe(self):
160+
# GH#46179
161+
df = pd.DataFrame(
162+
{2010: [1], 2020: [3]},
163+
index=pd.MultiIndex.from_product([["a"], ["b"]], names=["scen", "mod"]),
164+
)
165+
166+
ser = Series(
167+
[10.0, 20.0, 30.0],
168+
index=pd.MultiIndex.from_product(
169+
[["a"], ["b"], [0, 1, 2]], names=["scen", "mod", "id"]
170+
),
171+
)
172+
173+
msg = "Series.add does not support a DataFrame `other`"
174+
with pytest.raises(TypeError, match=msg):
175+
ser.add(df, axis=0)
176+
159177

160178
class TestSeriesArithmetic:
161179
# Some of these may end up in tests/arithmetic, but are not yet sorted

0 commit comments

Comments
 (0)