Skip to content

ENH: Don't remove the ability to expand a Series to a DataFrame by applying a function that returns Series #55134

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

Closed
3 tasks
n-splv opened this issue Sep 14, 2023 · 5 comments · Fixed by #55189
Closed
3 tasks
Labels
Apply Apply, Aggregate, Transform, Map Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action

Comments

@n-splv
Copy link

n-splv commented Sep 14, 2023

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Being able to expand a Series by applying a function to its cells is very convenient, but in 2.1 in produces a
FutureWarning: Returning a DataFrame from Series.apply when the supplied function returns a Series is deprecated and will be removed in a future version

Feature Description

Right now I can do this:

def expand_cell(s: Union[str, np.ndarray, list]) -> pd.Series:

    result = process_cell(s)
    data = {
        'foo': result.foo,
        'bar': result.bar,
        ...
    }
    return pd.Series(data)
        
df = pd.concat([
    df,
    df[column].apply(expand_cell),
], axis=1)

I tried to get rid of the FutureWarning, but changing the expand_cell return value to be pd.Series(data).to_frame() didn't help, because in such case the result of apply() is a Series with each cell containing a DataFrame.

I might be missing something, but as of right now the only working solution which doesn't raise a Warning seems be the replacement of the concise df[column].apply(expand_cell) with a more ugly pd.DataFrame([expand_cell(c) for c in df[column]]).

Alternative Solutions

Keep everything as is, I guess? :)

Additional Context

No response

@n-splv n-splv added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 14, 2023
@lithomas1
Copy link
Member

cc @topper-123

@lithomas1 lithomas1 added Usage Question Apply Apply, Aggregate, Transform, Map and removed Enhancement labels Sep 14, 2023
@rhshadrach rhshadrach added Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action and removed Usage Question Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 14, 2023
@rhshadrach
Copy link
Member

Additional discussion on this is taking place on the PR #52123.

@topper-123
Copy link
Contributor

There is also a more current discussion here: #52116.

The reason it was removed was partly that it didn't fit into how apply works in other contexts ( DataFrame.apply etc.), but also partly because it is very very slow (See perf example in OP in #52116). My recommendation is to do it this without apply e.g. in a loop and then concat the results, but that hasn't been communicated very well + there isn't 100 % agreement on this as seen in #52116.

Slightly related, we should really implement series.explode(axis=1), so series with listlikes could be easily be turned into dataframes. AFAIK there isn't easy functionality for doing that currently.

@rhshadrach
Copy link
Member

I think we should revert the deprecation for the time being.

@miroSalted
Copy link

Are you going to remove the warning as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants