Open
Description
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