From bb0126f2748825d3c9cf315c8ea1482cb8cbaefa Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 30 Jan 2017 21:19:50 -0500 Subject: [PATCH] ENH: Add empty property to Index. Previously, attempting to evaluate an Index in a boolean context prints an error message listing various alternatives, one of which is `.empty`, which was not actually implemented on `Index`. --- doc/source/whatsnew/v0.20.0.txt | 1 + pandas/core/base.py | 4 ++++ pandas/tests/indexes/common.py | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 9e71b9a11c8eb..72511a38e991f 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -155,6 +155,7 @@ Other enhancements - ``Series/DataFrame.squeeze()`` have gained the ``axis`` parameter. (:issue:`15339`) - ``DataFrame.to_excel()`` has a new ``freeze_panes`` parameter to turn on Freeze Panes when exporting to Excel (:issue:`15160`) - HTML table output skips ``colspan`` or ``rowspan`` attribute if equal to 1. (:issue:`15403`) +- Added ``.empty`` property to subclasses of ``Index``. (:issue:`15270`) .. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations diff --git a/pandas/core/base.py b/pandas/core/base.py index 92ec6bb3d73e6..fa874ddddbeda 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -879,6 +879,10 @@ def _values(self): """ the internal implementation """ return self.values + @property + def empty(self): + return not self.size + def max(self): """ The maximum value of the object """ return nanops.nanmax(self.values) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 81ad0524807f3..0fa5eb1cdcb67 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -904,3 +904,9 @@ def test_nulls(self): result = isnull(index) self.assert_numpy_array_equal(index.isnull(), result) self.assert_numpy_array_equal(index.notnull(), ~result) + + def test_empty(self): + # GH 15270 + index = self.create_index() + self.assertFalse(index.empty) + self.assertTrue(index[:0].empty)