Skip to content

Commit bb7eaff

Browse files
committed
BUG: empty frame.apply with func that fails on empty series #2476
1 parent 784ce1d commit bb7eaff

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pandas/core/frame.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -4048,8 +4048,14 @@ def apply(self, func, axis=0, broadcast=False, raw=False,
40484048
else:
40494049
if not broadcast:
40504050
if not all(self.shape):
4051-
is_reduction = not isinstance(f(_EMPTY_SERIES),
4052-
np.ndarray)
4051+
# How to determine this better?
4052+
is_reduction = False
4053+
try:
4054+
is_reduction = not isinstance(f(_EMPTY_SERIES),
4055+
np.ndarray)
4056+
except Exception:
4057+
pass
4058+
40534059
if is_reduction:
40544060
return Series(NA, index=self._get_agg_axis(axis))
40554061
else:

pandas/tests/test_frame.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -5626,6 +5626,11 @@ def test_apply(self):
56265626
self.assertEqual(applied[d], np.mean(self.frame.xs(d)))
56275627
self.assert_(applied.index is self.frame.index) # want this
56285628

5629+
#invalid axis
5630+
df = DataFrame([[1,2,3], [4,5,6], [7,8,9]], index=['a','a','c'])
5631+
self.assertRaises(ValueError, df.apply, lambda x: x, 2)
5632+
5633+
def test_apply_empty(self):
56295634
# empty
56305635
applied = self.empty.apply(np.sqrt)
56315636
self.assert_(applied.empty)
@@ -5643,9 +5648,10 @@ def test_apply(self):
56435648
expected = Series(np.nan, index=self.frame.index)
56445649
assert_series_equal(result, expected)
56455650

5646-
#invalid axis
5647-
df = DataFrame([[1,2,3], [4,5,6], [7,8,9]], index=['a','a','c'])
5648-
self.assertRaises(ValueError, df.apply, lambda x: x, 2)
5651+
#2476
5652+
xp = DataFrame(index=['a'])
5653+
rs = xp.apply(lambda x: x['a'], axis=1)
5654+
assert_frame_equal(xp, rs)
56495655

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

0 commit comments

Comments
 (0)