Skip to content

Commit b584b35

Browse files
committed
GH 30975-Fix for Series.append(DataFrame) Modified
1 parent b717007 commit b584b35

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

pandas/core/series.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,15 +2534,21 @@ def append(self, to_append, ignore_index=False, verify_integrity=False) -> "Seri
25342534
"""
25352535
from pandas.core.reshape.concat import concat
25362536

2537+
is_sequence: bool = False
25372538
if isinstance(to_append, (list, tuple)):
25382539
to_concat = [self]
25392540
to_concat.extend(to_append)
2541+
is_sequence = True
25402542
else:
2541-
if not isinstance(to_append, type(self)):
2542-
msg = f"to_append should be a Series or list/tuple of Series, " \
2543-
f"got {type(to_append)}"
2544-
raise TypeError(msg)
25452543
to_concat = [self, to_append]
2544+
for x in to_concat[1:]:
2545+
if isinstance(x, (pd.DataFrame,)):
2546+
msg = (
2547+
f"to_append should be a Series or list/tuple of Series, "
2548+
f"got {type(to_append).__name__} "
2549+
f'{("of "+type(x).__name__) if is_sequence else ""}'
2550+
)
2551+
raise TypeError(msg)
25462552
return self._ensure_type(
25472553
concat(
25482554
to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity

pandas/tests/series/methods/test_append.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,21 @@ def test_append_tuples(self):
6161

6262
tm.assert_series_equal(expected, result)
6363

64-
def test_append_dataframe(self):
64+
def test_append_dataframe_raises(self):
6565
# GH 30975
6666
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
67-
df2 = pd.DataFrame({"C": [5, 6], "D": [7, 8]})
67+
li = [df.B, df]
6868

69-
expected = pd.Series(pd.concat([df.A, df2.D]))
70-
result = df.A.append(df2.D)
71-
72-
tm.assert_series_equal(expected, result)
73-
74-
msg = "to_append should be a Series or list/tuple of Series, got " \
75-
"<class 'pandas.core.frame.DataFrame'>"
69+
msg = "to_append should be a Series or list/tuple of Series, got DataFrame"
7670
with pytest.raises(TypeError, match=msg):
7771
df.A.append(df)
72+
msg = (
73+
"to_append should be a Series or list/tuple of Series,"
74+
" got list of DataFrame"
75+
)
76+
with pytest.raises(TypeError, match=msg):
77+
df.A.append(li)
78+
7879

7980
class TestSeriesAppendWithDatetimeIndex:
8081
def test_append(self):

0 commit comments

Comments
 (0)