Skip to content

Commit fd8384e

Browse files
authored
Series append raises TypeError (#32090)
1 parent 3242b4e commit fd8384e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ Reshaping
327327
- Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`)
328328
- :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`)
329329
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
330+
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
330331
- :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`)
331332

332333

@@ -349,7 +350,6 @@ Other
349350
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
350351
- Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`)
351352
- Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`)
352-
-
353353

354354
.. ---------------------------------------------------------------------------
355355

pandas/core/series.py

+6
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,12 @@ def append(self, to_append, ignore_index=False, verify_integrity=False):
25342534
to_concat.extend(to_append)
25352535
else:
25362536
to_concat = [self, to_append]
2537+
if any(isinstance(x, (ABCDataFrame,)) for x in to_concat[1:]):
2538+
msg = (
2539+
f"to_append should be a Series or list/tuple of Series, "
2540+
f"got DataFrame"
2541+
)
2542+
raise TypeError(msg)
25372543
return concat(
25382544
to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity
25392545
)

pandas/tests/series/methods/test_append.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ def test_append_tuples(self):
6161

6262
tm.assert_series_equal(expected, result)
6363

64-
def test_append_dataframe_regression(self):
65-
# GH 30975
66-
df = pd.DataFrame({"A": [1, 2]})
67-
result = df.A.append([df])
68-
expected = pd.DataFrame(
69-
{0: [1.0, 2.0, None, None], "A": [None, None, 1.0, 2.0]}, index=[0, 1, 0, 1]
70-
)
71-
72-
tm.assert_frame_equal(expected, result)
64+
def test_append_dataframe_raises(self):
65+
# GH 31413
66+
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
67+
68+
msg = "to_append should be a Series or list/tuple of Series, got DataFrame"
69+
with pytest.raises(TypeError, match=msg):
70+
df.A.append(df)
71+
with pytest.raises(TypeError, match=msg):
72+
df.A.append([df])
7373

7474

7575
class TestSeriesAppendWithDatetimeIndex:

0 commit comments

Comments
 (0)