Skip to content

Commit

Permalink
ENH: Add empty property to Index.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Scott Sanderson committed Feb 20, 2017
1 parent 12f2c6a commit bb0126f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit bb0126f

Please sign in to comment.