Skip to content

Series upconversion in apply #2316

Closed
Closed
@jreback

Description

@jreback

See the below examples. My use case started out as trying to use apply on a Series (and having the applied function return a Series) in order to get a DataFrame - but seeing some inconsistencies of how the returned function is accumulated (obviously can do this by iterating over the series in a list comprehension and using concat too).

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: print pd.__version__
0.8.1

In [4]: 

In [4]: s  = pd.Series(np.random.rand(5))

In [5]: s
Out[5]: 
0    0.681552
1    0.299908
2    0.212221
3    0.365597
4    0.779190

In [6]: print type(s)
<class 'pandas.core.series.Series'>

In [7]: print type(s[0])
<type 'numpy.float64'>

base case

In [6]: y = s.apply(lambda x: x)

In [7]: print type(y)
<class 'pandas.core.series.Series'>

In [8]: print type(y[0])
<type 'numpy.float64'>

In [9]: print y[0]
0.681551901344

this looks like it is doing some sort of conversion
e.g. using the Series values to construct the applied series

In [10]: y = s.apply(lambda x: pd.Series(x))

In [11]: print type(y)
<class 'pandas.core.series.Series'>

In [12]: print type(y[0])
<type 'numpy.float64'>

In [13]: print y[0]
0.681551901344

This is a bit inconsistent with the prior example;
Should this be upconverting to a DataFrame?

In [14]: y = s.apply(lambda x: pd.Series(x, index = ['foo']))

In [15]: print type(y)
<class 'pandas.core.series.Series'>

In [16]: print type(y[0])
<class 'pandas.core.series.Series'>

In [17]: print y[0]
foo    0.681552

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions