Skip to content
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

ENH/API: implement __nonzero__ for NDFrame #3696

Merged
merged 2 commits into from
May 30, 2013
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
3 changes: 3 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down Expand Up @@ -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
=============
Expand Down
2 changes: 2 additions & 0 deletions doc/source/v0.11.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 0 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ def __repr__(self):
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
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down