DOC: Dataframe.apply does not always return a Series when reduce=True
See original GitHub issueCode Sample, a copy-pastable example if possible
pd.DataFrame({'col_1':['val_1','val_2','val_3']}).apply(lambda row: [1], axis=1, reduce=True)
Problem description
Per the documentation, calling Dataframe.apply w/ reduce=True should always produce a Series. However, if the applied function returns list values Dataframe.apply returns a Dataframe, not a Series.
Expected Output
When called with reduce=True, I expect Dataframe.apply to produce a Series where each element is a list, not a Dataframe.
Output of pd.show_versions()
0.18.1
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Why does df.apply(tuple) work but not df.apply(list)?
15628: Dataframe.apply does not always return a Series when reduce=True. That latter issue got closed, then reopened, and converted into a ...
Read more >pandas.DataFrame.apply — pandas 0.23.1 documentation
Apply a function along an axis of the DataFrame. ... If reduce=True a Series will always be returned, and if reduce=False a DataFrame...
Read more >How and why to stop using pandas .apply() (so much)
This function takes the sin of all the values in each dataframe, sum s them into a series, and then sum s the...
Read more >[Solved]-Why does df.apply(tuple) work but not df.apply(list)?
15628: Dataframe.apply does not always return a Series when reduce=True. That latter issue got closed, then reopened, and converted into a docs enhancement ......
Read more >Pandas DataFrame apply() Examples - DigitalOcean
We can apply a function along the axis. But, in the last example, there is no use of the axis. The function is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jreback I agree that
apply
does a lot of guessing in the default case, and it is sometimes difficult to predict what the output will be. But isn’t that the point of the keyword? To be able to specify this and be sure about the resulting shape? In any case, if this is a won’t fix, then the documentation is wrong.I guess the doc-string is misleading.
reduce
really only applies to empty frames and has no effect otherwise, which it says in the end.I suppose some nice examples of what not to do in .apply (like returning a 1-element list and expecting it work, rather returning tuples) might be in order.
@NelsonAndrew I’ll re-open as a doc issue.
.apply
is already pretty magical, I think it could use a comprehensive fix but that is likely to break back-compat.