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

Conversation

hvardhan20
Copy link
Contributor

@hvardhan20 hvardhan20 commented Feb 19, 2020

Series.append now raises TypeError when a DataFrame or a sequence containing DataFrame is passed.

@@ -2530,6 +2530,13 @@ def append(self, to_append, ignore_index=False, verify_integrity=False):
to_concat.extend(to_append)
else:
to_concat = [self, to_append]
for x in to_concat[1:]:
Copy link
Member

Choose a reason for hiding this comment

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

can you make this a single check with any(...)

ideally annotate the args above while we're here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to @simonjayhawkins here, for(...) fails faster. I guess it makes sense because with if any(isinstance(x, ABCDataFrame) for x in to_concat[1:]): it waits till the list comprehension is built.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With to_append annotated using Union[Series, Sequence[Series]], the static analysis is throwing this error:
pandas/core/series.py:2539: error: List item 1 has incompatible type "Union[Any, Sequence[Any]]"; expected "Series"
Found 1 error in 1 file (checked 880 source files)

Is there a better type or way to annotate the args?

Copy link
Member

Choose a reason for hiding this comment

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

waits till the list comprehension is built

i think that would be true for any([isinstance(x, ABCDataFrame) for x in to_concat[1:]]), but shouldnt be for any(isinstance(x, ABCDataFrame) for x in to_concat[1:]).

@simonjayhawkins am i missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah let's go with any(isinstance(x, ABCDataFrame) for x in to_concat[1:]). I just timed all 3 and this one was the fastest.
What do you think we should print in the msg? to_append.__name__, which will give sequence type if a sequence with DataFrame is passed? or just DataFrame?

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!

@pep8speaks
Copy link

pep8speaks commented Feb 19, 2020

Hello @hvardhan20! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-03-03 06:14:24 UTC

@simonjayhawkins simonjayhawkins added Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Feb 19, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

pls merge master as well. ping on green.

@@ -245,7 +245,7 @@ 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`)
-
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this to reshaping

@jreback jreback added this to the 1.1 milestone Mar 3, 2020
@hvardhan20 hvardhan20 requested a review from jreback March 3, 2020 19:12
@jreback jreback merged commit fd8384e into pandas-dev:master Mar 4, 2020
@jreback
Copy link
Contributor

jreback commented Mar 4, 2020

thanks @hvardhan20

@hvardhan20 hvardhan20 deleted the series_append_raises branch March 4, 2020 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Series.append(DataFrame) should throw TypeError
5 participants