Skip to content
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

API: update Series.apply to follow changes in DataFrame.apply #19866

Open
jorisvandenbossche opened this issue Feb 23, 2018 · 0 comments
Open
Labels
API - Consistency Internal Consistency of API/Behavior Apply Apply, Aggregate, Transform, Map Enhancement

Comments

@jorisvandenbossche
Copy link
Member

As a follow-up to the changes in DataFrame.apply (#18577), we should make sure Series.apply behaves consistently with that as well.

I think the default behaviour is OK (it already did what we now do by default for DataFrame: expand Series, keep lists or arrays as scalars, see below).

But we could in principle also add the same control over the output type by adding a similar result_type keyword.

Current behaviour:

In [42]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])

In [44]: s.apply(lambda x: [1, 2])
Out[44]: 
a    [1, 2]
b    [1, 2]
c    [1, 2]
d    [1, 2]
dtype: object

In [45]: s.apply(lambda x: pd.Series([1, 2]))
Out[45]: 
   0  1
a  1  2
b  1  2
c  1  2
d  1  2

In [46]: s.apply(lambda x: pd.Series([1, 2], index=['A', 'B']))
Out[46]: 
   A  B
a  1  2
b  1  2
c  1  2
d  1  2

In [47]: s.apply(lambda x: np.array([1, 2]))
Out[47]: 
a    [1, 2]
b    [1, 2]
c    [1, 2]
d    [1, 2]
dtype: object
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode API Design Difficulty Intermediate labels Feb 24, 2018
@jreback jreback added this to the Next Major Release milestone Feb 24, 2018
@jbrockmendel jbrockmendel added Apply Apply, Aggregate, Transform, Map and removed Effort Medium labels Oct 16, 2019
@mroeschke mroeschke added Enhancement and removed Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jun 28, 2020
@mroeschke mroeschke added API - Consistency Internal Consistency of API/Behavior and removed API Design labels Jun 19, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Apply Apply, Aggregate, Transform, Map Enhancement
Projects
None yet
Development

No branches or pull requests

4 participants