Skip to content

Series append raises TypeError #32090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 4, 2020
Merged
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 doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ Reshaping
- Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`)
- :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`)
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
- :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`)


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

.. ---------------------------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,12 @@ def append(self, to_append, ignore_index=False, verify_integrity=False):
to_concat.extend(to_append)
else:
to_concat = [self, to_append]
if any(isinstance(x, (ABCDataFrame,)) for x in to_concat[1:]):
msg = (
f"to_append should be a Series or list/tuple of Series, "
f"got DataFrame"
)
raise TypeError(msg)
return concat(
to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity
)
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/series/methods/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def test_append_tuples(self):

tm.assert_series_equal(expected, result)

def test_append_dataframe_regression(self):
# GH 30975
df = pd.DataFrame({"A": [1, 2]})
result = df.A.append([df])
expected = pd.DataFrame(
{0: [1.0, 2.0, None, None], "A": [None, None, 1.0, 2.0]}, index=[0, 1, 0, 1]
Copy link
Member

Choose a reason for hiding this comment

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

is there anything about this old test that was correct that should be retained?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. That case is testing for the successful appendage of a Series and a DataFrame. Which is exactly what we're trying to raise an error for. So I guess there's nothing right about the case.
What do you think about this @jbrockmendel

Thanks!

)

tm.assert_frame_equal(expected, result)
def test_append_dataframe_raises(self):
# GH 31413
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})

msg = "to_append should be a Series or list/tuple of Series, got DataFrame"
with pytest.raises(TypeError, match=msg):
df.A.append(df)
with pytest.raises(TypeError, match=msg):
df.A.append([df])


class TestSeriesAppendWithDatetimeIndex:
Expand Down