Skip to content

Commit 0b2826f

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 c26e5bb commit 0b2826f

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pandas/core/base.py

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

880+
@property
881+
def empty(self):
882+
return not bool(self.size)
883+
880884
def max(self):
881885
""" The maximum value of the object """
882886
return nanops.nanmax(self.values)

pandas/tests/indexes/common.py

+5
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,8 @@ def test_fillna(self):
879879
expected[1] = True
880880
self.assert_numpy_array_equal(idx._isnan, expected)
881881
self.assertTrue(idx.hasnans)
882+
883+
def test_empty(self):
884+
index = self.create_index()
885+
self.assertFalse(index.empty)
886+
self.assertTrue(index[:0].empty)

0 commit comments

Comments
 (0)