Skip to content

Commit

Permalink
BUG: fix empty DataFrame corner cases described in #378
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Nov 18, 2011
1 parent f16158c commit 53ca3f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Expand Up @@ -132,6 +132,7 @@ pandas 0.5.1
- Fix block consolidation bug after inserting column into MultiIndex (GH #366)
- Fix bug in join operations between Index and Int64Index (GH #367)
- Handle min_periods=0 case in moving window functions (GH #365)
- Fixed corner cases in DataFrame.apply/pivot with empty DataFrame (GH #378)

Thanks
------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Expand Up @@ -2256,7 +2256,7 @@ def _apply_standard(self, func, axis, ignore_failures=False):
for k, v in series_gen:
results[k] = func(v)

if hasattr(results.values()[0], '__iter__'):
if len(results) > 0 and hasattr(results.values()[0], '__iter__'):
result = self._constructor(data=results, index=res_columns,
columns=res_index)

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/reshape.py
Expand Up @@ -119,8 +119,9 @@ def get_result(self):
index = self.get_new_index()

# filter out missing levels
values = values[:, mask]
columns = columns[mask]
if values.shape[1] > 0:
values = values[:, mask]
columns = columns[mask]

return DataFrame(values, index=index, columns=columns)

Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/test_frame.py
Expand Up @@ -2282,6 +2282,12 @@ def test_pivot_duplicates(self):
'c' : [1., 2., 3., 3., 4.]})
self.assertRaises(Exception, data.pivot, 'a', 'b', 'c')

def test_pivot_empty(self):
df = DataFrame({}, columns=['a', 'b', 'c'])
result = df.pivot('a', 'b', 'c')
expected = DataFrame({})
assert_frame_equal(result, expected)

def test_reindex(self):
newFrame = self.frame.reindex(self.ts1.index)

Expand Down Expand Up @@ -2631,10 +2637,18 @@ def test_apply_ignore_failures(self):

# test with hierarchical index

def test_apply_mixed_dtype_corner(self):
df = DataFrame({'A' : ['foo'],
'B' : [1.]})
result = df[:0].apply(np.mean, axis=1)
# the result here is actually kind of ambiguous, should it be a Series
# or a DataFrame?
expected = Series(np.nan, index=[])
assert_series_equal(result, expected)

def test_applymap(self):
applied = self.frame.applymap(lambda x: x * 2)
assert_frame_equal(applied, self.frame * 2)

result = self.frame.applymap(type)

def test_filter(self):
Expand Down

0 comments on commit 53ca3f1

Please sign in to comment.