Skip to content

Commit

Permalink
BUG: imprecise logic leading to malformed Series in Series.apply, close
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed May 7, 2012
1 parent 09736e5 commit e8fc1c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1872,8 +1872,10 @@ def apply(self, func):
"""
try:
result = func(self)
if not isinstance(result, Series):
if isinstance(result, np.ndarray):
result = Series(result, index=self.index, name=self.name)
else:
raise ValueError('Must yield array')
return result
except Exception:
mapped = lib.map_infer(self.values, func)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,19 @@ def test_apply(self):
result = self.ts.apply(lambda x: x.values * 2)
assert_series_equal(result, self.ts * 2)

def test_apply_same_length_inference_bug(self):
s = Series([1, 2])
f = lambda x: (x, x + 1)

result = s.apply(f)
expected = s.map(f)
assert_series_equal(result, expected)

s = Series([1, 2, 3])
result = s.apply(f)
expected = s.map(f)
assert_series_equal(result, expected)

def test_align(self):
def _check_align(a, b, how='left', fill=None):
aa, ab = a.align(b, join=how, fill_value=fill)
Expand Down

0 comments on commit e8fc1c0

Please sign in to comment.