Skip to content

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

Closed
@n-splv

Description

@n-splv

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapDeprecateFunctionality to remove in pandasNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions