Skip to content

BUG: .apply with collections #39166

@svjack

Description

@svjack

consider following example:

pd.DataFrame([[0, 1, 2], [3, 4, 5]]).apply(lambda x: [x] * 2, axis = 1) [[3, 4, 5], [3, 4, 5]] [[3, 4, 5], [3, 4, 5]] 

but if use numpy array to replace list, i.e.

pd.DataFrame([[0, 1, 2], [3, 4, 5]]).apply(lambda x: np.asarray([x]) * 2, axis = 1) [[0, 2, 4]] [[6, 4, 8]] 

I can not understand the conclusion use list (first example)
if pandas take element in apply(axis = 1) as pd.Series
And [x] cast it to nest list for example ,
First Row [[0, 1, 2]] * 2 -> [[0, 1, 2], [0, 1, 2]]
Second Row [[3, 4, 5]] * 2 -> [[3, 4, 5], [3, 4, 5]]
should not have above conclusion.
It seems pandas only maintain the last row and apply it to all rows.
And when use replace [x] by {“x”: x},
Have similar problem.

I can understand use numpy collection and pandas collection
inside the elements of DataFrame (avoid use list tuple or dict) may be more safe.
But please explain this conclusion for me.

My pandas version is 1.1.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions