Skip to content

Commit ddbfb3c

Browse files
committed
BUG: Series.apply fails on empty Series #2815
1 parent 7c51d54 commit ddbfb3c

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,9 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
22862286
-------
22872287
y : Series or DataFrame if func returns a Series
22882288
"""
2289+
if len(self) == 0:
2290+
return Series()
2291+
22892292
if kwds or args and not isinstance(func, np.ufunc):
22902293
f = lambda x: func(x, *args, **kwds)
22912294
else:

pandas/tests/test_series.py

+10
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,16 @@ def test_apply(self):
26852685
expected = DataFrame({'x': self.ts, 'x^2': self.ts ** 2})
26862686
tm.assert_frame_equal(result, expected)
26872687

2688+
# empty series
2689+
s = Series()
2690+
rs = s.apply(lambda x: x)
2691+
tm.assert_series_equal(s, rs)
2692+
2693+
# index but no data
2694+
s = Series(index=[1,2,3])
2695+
rs = s.apply(lambda x: x)
2696+
tm.assert_series_equal(s, rs)
2697+
26882698
def test_apply_same_length_inference_bug(self):
26892699
s = Series([1, 2])
26902700
f = lambda x: (x, x + 1)

0 commit comments

Comments
 (0)