Skip to content

Commit

Permalink
BUG: empty frame.apply with func that fails on empty series pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Dec 10, 2012
1 parent 784ce1d commit bb7eaff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4048,8 +4048,14 @@ def apply(self, func, axis=0, broadcast=False, raw=False,
else:
if not broadcast:
if not all(self.shape):
is_reduction = not isinstance(f(_EMPTY_SERIES),
np.ndarray)
# How to determine this better?
is_reduction = False
try:
is_reduction = not isinstance(f(_EMPTY_SERIES),
np.ndarray)
except Exception:
pass

if is_reduction:
return Series(NA, index=self._get_agg_axis(axis))
else:
Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5626,6 +5626,11 @@ def test_apply(self):
self.assertEqual(applied[d], np.mean(self.frame.xs(d)))
self.assert_(applied.index is self.frame.index) # want this

#invalid axis
df = DataFrame([[1,2,3], [4,5,6], [7,8,9]], index=['a','a','c'])
self.assertRaises(ValueError, df.apply, lambda x: x, 2)

def test_apply_empty(self):
# empty
applied = self.empty.apply(np.sqrt)
self.assert_(applied.empty)
Expand All @@ -5643,9 +5648,10 @@ def test_apply(self):
expected = Series(np.nan, index=self.frame.index)
assert_series_equal(result, expected)

#invalid axis
df = DataFrame([[1,2,3], [4,5,6], [7,8,9]], index=['a','a','c'])
self.assertRaises(ValueError, df.apply, lambda x: x, 2)
#2476
xp = DataFrame(index=['a'])
rs = xp.apply(lambda x: x['a'], axis=1)
assert_frame_equal(xp, rs)

def test_apply_standard_nonunique(self):
df = DataFrame([[1,2,3], [4,5,6], [7,8,9]], index=['a','a','c'])
Expand Down

0 comments on commit bb7eaff

Please sign in to comment.