Skip to content

Commit

Permalink
BUG Deprecating use of bool() on DataFrame, GH pandas-dev#1069
Browse files Browse the repository at this point in the history
Calling bool() on DataFrame raises issues described in pandas-dev#1069. This
change causes DataFrame to raise a ValueError when called with bool(),
and instead provides a new property, dataframe.empty.
  • Loading branch information
Eric Chlebek committed Apr 17, 2012
1 parent e1552a6 commit a79f4d6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
14 changes: 9 additions & 5 deletions pandas/core/frame.py
Expand Up @@ -449,9 +449,12 @@ def shape(self):
#----------------------------------------------------------------------
# Class behavior

@property
def empty(self):
return not (len(self.columns) > 0 and len(self.index) > 0)

def __nonzero__(self):
# e.g. "if frame: ..."
return len(self.columns) > 0 and len(self.index) > 0
raise ValueError("Cannot call bool() on DataFrame.")

def _need_info_repr_(self):
"""
Expand Down Expand Up @@ -2652,8 +2655,9 @@ def _combine_match_columns(self, other, func, fill_value=None):
columns=left.columns, copy=False)

def _combine_const(self, other, func):
if not self:
if self.empty:
return self

result_values = func(self.values, other)

if not isinstance(result_values, np.ndarray):
Expand Down Expand Up @@ -2691,10 +2695,10 @@ def combine(self, other, func, fill_value=None):
-------
result : DataFrame
"""
if not other:
if other.empty:
return self.copy()

if not self:
if self.empty:
return other.copy()

this, other = self.align(other, copy=False)
Expand Down
2 changes: 1 addition & 1 deletion pandas/sparse/frame.py
Expand Up @@ -415,7 +415,7 @@ def _combine_frame(self, other, func, fill_value=None, level=None):
if level is not None:
raise NotImplementedError

if not self and not other:
if self.empty and other.empty:
return SparseDataFrame(index=new_index)

new_data = {}
Expand Down
2 changes: 1 addition & 1 deletion pandas/sparse/tests/test_sparse.py
Expand Up @@ -870,7 +870,7 @@ def _compare_to_dense(a, b, da, db, op):

def test_op_corners(self):
empty = self.empty + self.empty
self.assert_(not empty)
self.assert_(empty.empty)

foo = self.frame + self.empty
self.assert_(isinstance(foo.index, DatetimeIndex))
Expand Down
2 changes: 1 addition & 1 deletion pandas/stats/plm.py
Expand Up @@ -149,7 +149,7 @@ def _filter_data(self):
x = data_long.filter(x_names)
y = data_long['__y__']

if self._weights:
if self._weights is not None and not self._weights.empty:
weights = data_long['__weights__']
else:
weights = None
Expand Down
27 changes: 17 additions & 10 deletions pandas/tests/test_frame.py
Expand Up @@ -1975,17 +1975,17 @@ def test_get_agg_axis(self):
self.assertRaises(Exception, self.frame._get_agg_axis, 2)

def test_nonzero(self):
self.assertFalse(self.empty)
self.assertTrue(self.empty.empty)

self.assert_(self.frame)
self.assert_(self.mixed_frame)
self.assertFalse(self.frame.empty)
self.assertFalse(self.mixed_frame.empty)

# corner case
df = DataFrame({'A' : [1., 2., 3.],
'B' : ['a', 'b', 'c']},
index=np.arange(3))
del df['A']
self.assert_(df)
self.assertFalse(df.empty)

def test_repr(self):
buf = StringIO()
Expand Down Expand Up @@ -2329,7 +2329,7 @@ def test_combineFrame(self):
self.assert_(np.isnan(empty_plus.values).all())

empty_empty = self.empty + self.empty
self.assert_(not empty_empty)
self.assertTrue(empty_empty.empty)

# out of order
reverse = self.frame.reindex(columns=self.frame.columns[::-1])
Expand Down Expand Up @@ -3360,7 +3360,7 @@ def test_reindex(self):

# length zero
newFrame = self.frame.reindex([])
self.assert_(not newFrame)
self.assert_(newFrame.empty)
self.assertEqual(len(newFrame.columns), len(self.frame.columns))

# length zero with columns reindexed with non-empty index
Expand Down Expand Up @@ -3415,7 +3415,7 @@ def test_reindex_columns(self):

# length zero
newFrame = self.frame.reindex(columns=[])
self.assert_(not newFrame)
self.assert_(newFrame.empty)

def test_reindex_fill_value(self):
df = DataFrame(np.random.randn(10, 4))
Expand Down Expand Up @@ -3738,10 +3738,10 @@ def test_apply(self):

# empty
applied = self.empty.apply(np.sqrt)
self.assert_(not applied)
self.assert_(applied.empty)

applied = self.empty.apply(np.mean)
self.assert_(not applied)
self.assert_(applied.empty)

no_rows = self.frame[:0]
result = no_rows.apply(lambda x: x.mean())
Expand Down Expand Up @@ -5121,13 +5121,20 @@ def test_stale_cached_series_bug_473(self):
self.assert_(isnull(Y['g']['c']))

def test_index_namedtuple(self):
# Skipping until 1026 is properly resolved
raise nose.SkipTest
from collections import namedtuple
IndexType = namedtuple("IndexType", ["a", "b"])
idx1 = IndexType("foo", "bar")
idx2 = IndexType("baz", "bof")
index = Index([idx1, idx2], name="composite_index")
df = DataFrame([(1, 2), (3, 4)], index=index, columns=["A", "B"])
self.assertEqual(df.ix[IndexType("foo", "bar")], (1, 2))
print df.ix[IndexType("foo", "bar")]["A"]
self.assertEqual(df.ix[IndexType("foo", "bar")]["A"], 1)

def test_bool_raises_value_error_1069(self):
df = DataFrame([1, 2, 3])
self.failUnlessRaises(ValueError, lambda: bool(df))

if __name__ == '__main__':
# unittest.main()
Expand Down

0 comments on commit a79f4d6

Please sign in to comment.