Skip to content

Commit

Permalink
BUG: more proper handling of no rows / no columns in DataFrame.apply
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Oct 24, 2011
1 parent f1ab24c commit e4b66db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ feedback on the library.
- Fix many corner cases in MultiIndex set operations
- Fix MultiIndex-handling bug with GroupBy.apply when returned groups are not
indexed the same
- Fix corner case bugs in DataFrame.apply

Thanks
------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ def apply(self, func, axis=0, broadcast=False):
-------
applied : Series or DataFrame
"""
if not len(self.columns):
if len(self.columns) == 0 and len(self.index) == 0:
return self

if isinstance(func, np.ufunc):
Expand Down Expand Up @@ -2902,7 +2902,7 @@ def _write_to_buffer(self):
to_write = []

if len(frame.columns) == 0 or len(frame.index) == 0:
to_write.append('Empty %s' % type(self).__name__)
to_write.append('Empty %s\n' % type(self.frame).__name__)
to_write.append(repr(frame.index))
else:
# may include levels names also
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,15 @@ def test_apply(self):
applied = self.empty.apply(np.mean)
self.assert_(not applied)

no_rows = self.frame[:0]
result = no_rows.apply(lambda x: x.mean())
expected = Series(np.nan, index=self.frame.columns)
assert_series_equal(result, expected)

no_cols = self.frame.ix[:, []]
result = no_cols.apply(lambda x: x.mean(), axis=1)
expected = Series(np.nan, index=self.frame.index)
assert_series_equal(result, expected)

def test_apply_broadcast(self):
broadcasted = self.frame.apply(np.mean, broadcast=True)
Expand Down

0 comments on commit e4b66db

Please sign in to comment.