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

Already on GitHub? Sign in to your account

ENH/API: implement __nonzero__ for NDFrame #3696

Merged
merged 2 commits into from May 30, 2013
Jump to file or symbol
Failed to load files and symbols.
+20 −10
Split
View
@@ -112,6 +112,7 @@ pandas 0.11.1
- added top-level ``pd.read_sql`` and ``to_sql`` DataFrame methods
- the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are
deprecated
+ - Implement ``__nonzero__`` for ``NDFrame`` objects (GH3691_, GH3696_)
**Bug Fixes**
@@ -266,6 +267,8 @@ pandas 0.11.1
.. _GH3675: https://github.com/pydata/pandas/issues/3675
.. _GH3682: https://github.com/pydata/pandas/issues/3682
.. _GH3702: https://github.com/pydata/pandas/issues/3702
+.. _GH3691: https://github.com/pydata/pandas/issues/3691
+.. _GH3696: https://github.com/pydata/pandas/issues/3696
pandas 0.11.0
=============
View
@@ -88,6 +88,8 @@ API changes
- Add the keyword ``allow_duplicates`` to ``DataFrame.insert`` to allow a duplicate column
to be inserted if ``True``, default is ``False`` (same as prior to 0.11.1) (GH3679_)
+ - Implement ``__nonzero__`` for ``NDFrame`` objects (GH3691_, GH3696_)
+
- IO api
View
@@ -595,14 +595,6 @@ def shape(self):
#----------------------------------------------------------------------
# Class behavior
-
- @property
- def empty(self):
- return not (len(self.columns) > 0 and len(self.index) > 0)
-
- def __nonzero__(self):
- raise ValueError("Cannot call bool() on DataFrame.")
-
def _repr_fits_vertical_(self):
"""
Check length against max_rows.
View
@@ -560,6 +560,13 @@ def values(self):
return self._data.as_matrix()
@property
+ def empty(self):
+ return not all(len(ax) > 0 for ax in self.axes)
+
+ def __nonzero__(self):
+ return not self.empty
+
+ @property
def ndim(self):
return self._data.ndim
View
@@ -10379,9 +10379,15 @@ def test_index_namedtuple(self):
df = DataFrame([(1, 2), (3, 4)], index=index, columns=["A", "B"])
self.assertEqual(df.ix[IndexType("foo", "bar")]["A"], 1)
- def test_bool_raises_value_error_1069(self):
+ def test_bool_empty_nonzero(self):
df = DataFrame([1, 2, 3])
- self.failUnlessRaises(ValueError, lambda: bool(df))
+ self.assertTrue(bool(df))
+ self.assertFalse(df.empty)
+ df = DataFrame(index=['a', 'b'], columns=['c', 'd']).dropna()
+ self.assertFalse(bool(df))
+ self.assertFalse(bool(df.T))
+ self.assertTrue(df.empty)
+ self.assertTrue(df.T.empty)
def test_any_all(self):
self._check_bool_op('any', np.any, has_skipna=True, has_bool_only=True)