question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DOC: Dataframe.apply does not always return a Series when reduce=True

See original GitHub issue

Code 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jorisvandenbosschecommented, Mar 9, 2017

@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.

0reactions
jrebackcommented, Mar 9, 2017
reduce : boolean or None, default None
    Try to apply reduction procedures. If the DataFrame is empty,
    apply will use reduce to determine whether the result should be a
    Series or a DataFrame. If reduce is None (the default), apply's
    return value will be guessed by calling func an empty Series (note:
    while guessing, exceptions raised by func will be ignored). If
    reduce is True a Series will always be returned, and if False a
    DataFrame will always be returned.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found