From 86743b3a55ebb9917a7fa56ea008bad7af895bdb Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 26 May 2013 21:39:07 -0400 Subject: [PATCH 1/2] ENH/API: implemenet __nonzero__ for NDFrame ENH/API: remove __nonzero__ in frames, use NDFrame implementation --- pandas/core/frame.py | 8 -------- pandas/core/generic.py | 7 +++++++ pandas/tests/test_frame.py | 10 ++++++++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1dfeae997451a..8dc1a921eecad 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index aa574219a259e..7dd0315d7d90e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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 diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 1de643985d893..39452ece7a33d 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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) From 8002b71eb4d5ced6d82a81c99cd22438fc4a887f Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Tue, 28 May 2013 12:17:49 -0400 Subject: [PATCH 2/2] DOC: add release notes --- RELEASE.rst | 3 +++ doc/source/v0.11.1.txt | 2 ++ 2 files changed, 5 insertions(+) diff --git a/RELEASE.rst b/RELEASE.rst index e611b330b08f0..9283bada2d720 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -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 ============= diff --git a/doc/source/v0.11.1.txt b/doc/source/v0.11.1.txt index c025450c44cca..289c011f7a7a9 100644 --- a/doc/source/v0.11.1.txt +++ b/doc/source/v0.11.1.txt @@ -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