diff --git a/pandas/core/frame.py b/pandas/core/frame.py index de52249451542..f8a7f6ed90f7e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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: diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 20d8d07313341..ef5bc2973ceac 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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) @@ -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'])