Skip to content

Commit bf6f018

Browse files
committed
Merge pull request #3696 from cpcloud/nonzero-3691
ENH/API: implement __nonzero__ for NDFrame
2 parents 22ef2bb + 8002b71 commit bf6f018

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pandas 0.11.1
112112
- added top-level ``pd.read_sql`` and ``to_sql`` DataFrame methods
113113
- the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are
114114
deprecated
115+
- Implement ``__nonzero__`` for ``NDFrame`` objects (GH3691_, GH3696_)
115116

116117
**Bug Fixes**
117118

@@ -266,6 +267,8 @@ pandas 0.11.1
266267
.. _GH3675: https://github.com/pydata/pandas/issues/3675
267268
.. _GH3682: https://github.com/pydata/pandas/issues/3682
268269
.. _GH3702: https://github.com/pydata/pandas/issues/3702
270+
.. _GH3691: https://github.com/pydata/pandas/issues/3691
271+
.. _GH3696: https://github.com/pydata/pandas/issues/3696
269272

270273
pandas 0.11.0
271274
=============

doc/source/v0.11.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ API changes
8888

8989
- Add the keyword ``allow_duplicates`` to ``DataFrame.insert`` to allow a duplicate column
9090
to be inserted if ``True``, default is ``False`` (same as prior to 0.11.1) (GH3679_)
91+
- Implement ``__nonzero__`` for ``NDFrame`` objects (GH3691_, GH3696_)
92+
9193

9294
- IO api
9395

pandas/core/frame.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -595,14 +595,6 @@ def shape(self):
595595

596596
#----------------------------------------------------------------------
597597
# Class behavior
598-
599-
@property
600-
def empty(self):
601-
return not (len(self.columns) > 0 and len(self.index) > 0)
602-
603-
def __nonzero__(self):
604-
raise ValueError("Cannot call bool() on DataFrame.")
605-
606598
def _repr_fits_vertical_(self):
607599
"""
608600
Check length against max_rows.

pandas/core/generic.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ def __repr__(self):
559559
def values(self):
560560
return self._data.as_matrix()
561561

562+
@property
563+
def empty(self):
564+
return not all(len(ax) > 0 for ax in self.axes)
565+
566+
def __nonzero__(self):
567+
return not self.empty
568+
562569
@property
563570
def ndim(self):
564571
return self._data.ndim

pandas/tests/test_frame.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10379,9 +10379,15 @@ def test_index_namedtuple(self):
1037910379
df = DataFrame([(1, 2), (3, 4)], index=index, columns=["A", "B"])
1038010380
self.assertEqual(df.ix[IndexType("foo", "bar")]["A"], 1)
1038110381

10382-
def test_bool_raises_value_error_1069(self):
10382+
def test_bool_empty_nonzero(self):
1038310383
df = DataFrame([1, 2, 3])
10384-
self.failUnlessRaises(ValueError, lambda: bool(df))
10384+
self.assertTrue(bool(df))
10385+
self.assertFalse(df.empty)
10386+
df = DataFrame(index=['a', 'b'], columns=['c', 'd']).dropna()
10387+
self.assertFalse(bool(df))
10388+
self.assertFalse(bool(df.T))
10389+
self.assertTrue(df.empty)
10390+
self.assertTrue(df.T.empty)
1038510391

1038610392
def test_any_all(self):
1038710393
self._check_bool_op('any', np.any, has_skipna=True, has_bool_only=True)

0 commit comments

Comments
 (0)