Skip to content

Commit bb0126f

Browse files
author
Scott Sanderson
committed
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`.
1 parent 12f2c6a commit bb0126f

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Other enhancements
155155
- ``Series/DataFrame.squeeze()`` have gained the ``axis`` parameter. (:issue:`15339`)
156156
- ``DataFrame.to_excel()`` has a new ``freeze_panes`` parameter to turn on Freeze Panes when exporting to Excel (:issue:`15160`)
157157
- HTML table output skips ``colspan`` or ``rowspan`` attribute if equal to 1. (:issue:`15403`)
158+
- Added ``.empty`` property to subclasses of ``Index``. (:issue:`15270`)
158159

159160
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
160161

pandas/core/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,10 @@ def _values(self):
879879
""" the internal implementation """
880880
return self.values
881881

882+
@property
883+
def empty(self):
884+
return not self.size
885+
882886
def max(self):
883887
""" The maximum value of the object """
884888
return nanops.nanmax(self.values)

pandas/tests/indexes/common.py

+6
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,9 @@ def test_nulls(self):
904904
result = isnull(index)
905905
self.assert_numpy_array_equal(index.isnull(), result)
906906
self.assert_numpy_array_equal(index.notnull(), ~result)
907+
908+
def test_empty(self):
909+
# GH 15270
910+
index = self.create_index()
911+
self.assertFalse(index.empty)
912+
self.assertTrue(index[:0].empty)

0 commit comments

Comments
 (0)