Skip to content

apply with values return wrong output without convert to list #17487

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
jesrael opened this issue Sep 10, 2017 · 4 comments · Fixed by #52912
Closed

apply with values return wrong output without convert to list #17487

jesrael opened this issue Sep 10, 2017 · 4 comments · Fixed by #52912
Assignees
Labels
Apply Apply, Aggregate, Transform, Map Bug good first issue Needs Tests Unit test(s) needed to prevent regressions Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@jesrael
Copy link

jesrael commented Sep 10, 2017

from SO

df = pd.DataFrame({'a':[1,2], 'b':[2,3]})
print (df)
   a  b
0  1  2
1  2  3

And output is wrong:

print (df.apply(lambda x: [x.values]))
a    [[8791439669712, 8791439669712]]
b    [[8791439669712, 8791439669712]]
dtype: object

But if convert to list all working nice:

print (df.apply(lambda x: [x.values.tolist()]))
a    [[1, 2]]
b    [[2, 3]]
dtype: object

print (df.apply(lambda x: [list(x.values)]))
a    [[1, 2]]
b    [[2, 3]]
dtype: object

What is problem?

@gfyoung gfyoung added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Bug labels Sep 10, 2017
@gfyoung
Copy link
Member

gfyoung commented Sep 10, 2017

@jesrael : Thanks for reporting this! I'm not getting your output, but I do get something buggy too:

df.apply(lambda x: [x.values])
a    [[2, 2]]
b    [[2, 2]]

Feel free to investigate and put up a PR to patch this!

@jreback
Copy link
Contributor

jreback commented Sep 10, 2017

This is actually a numpy bug on how they handle object arrays; and of course a complete mis-use of the guarantees of apply.

In [13]: pd.lib.reduce(df.values, lambda x: [x])
Out[13]: array([list([array([0, 1])]), list([array([0, 1])])], dtype=object)

In [14]: pd.lib.reduce(df.values, lambda x: list([x]))
Out[14]: 
array([list([array([4569766464, 4569081312])]),
       list([array([4569766464, 4569081312])])], dtype=object)

@mroeschke mroeschke added Apply Apply, Aggregate, Transform, Map Bug and removed Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Oct 15, 2019
@jbrockmendel jbrockmendel added the Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). label Sep 22, 2020
@rhshadrach
Copy link
Member

I'm now getting

        a       b
0  [1, 2]  [2, 3]

which I think is the expected output.

@rhshadrach rhshadrach added good first issue Needs Tests Unit test(s) needed to prevent regressions labels Apr 23, 2023
@srkds
Copy link
Contributor

srkds commented Apr 25, 2023

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug good first issue Needs Tests Unit test(s) needed to prevent regressions Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants