Skip to content

Returning Series of dicts from DataFrame.apply #1738

@wesm

Description

@wesm

from the mailing list cc @lodagro

That is indeed odd.
There also seems to be a dependency on the dtype. Below an example for all ints and all floats.

In [121]: df
Out[121]: 
   A  B
a  7  0
b  7 -2

In [122]: df.apply(foo, 1)
Out[122]: 
    A   B
a NaN NaN
b NaN NaN

All NaN???
I would expect to see this:

In [123]: s = pandas.Series([foo(row[1]) for row in df.iterrows()], df.index)

In [124]: s
Out[124]: 
a     {'properties': {'A': 7, 'B': 0}}
b    {'properties': {'A': 7, 'B': -2}}

Seems to work fine for all floats.

In [125]: df = pandas.DataFrame(np.random.randn(2, 2), columns=list('AB'), index=list('ab'))

In [126]: df
Out[126]: 
          A         B
a -0.407883  0.018206
b -1.081038  0.492944

In [127]: df.apply(foo, 1)
Out[127]: 
a    {'properties': {'A': -0.407882576359619, 'B': 0.0
b    {'properties': {'A': -1.081038117264707, 'B': 0.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions