Skip to content

BUG Deprecating use of bool() on DataFrame, GH #1069 #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
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
5 changes: 4 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,10 @@ def _is_label_like(key):


def _is_list_like(obj):
return np.iterable(obj) and not isinstance(obj, basestring)
# Consider namedtuples to be not list like as they are useful as indices
return (np.iterable(obj)
and not isinstance(obj, basestring)
and not (isinstance(obj, tuple) and type(obj) is not tuple))


def _need_slice(obj):
Expand Down
2 changes: 1 addition & 1 deletion pandas/sparse/frame.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
26 changes: 16 additions & 10 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
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,19 @@ 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
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