Skip to content

Commit 9ee52a4

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 9ee52a4

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
@@ -146,6 +146,7 @@ Other enhancements
146146
- ``Series/DataFrame.asfreq()`` have gained a ``fill_value`` parameter, to fill missing values (:issue:`3715`).
147147
- ``Series/DataFrame.resample.asfreq`` have gained a ``fill_value`` parameter, to fill missing values during resampling (:issue:`3715`).
148148
- ``pandas.tools.hashing`` has gained a ``hash_tuples`` routine, and ``hash_pandas_object`` has gained the ability to hash a ``MultiIndex`` (:issue:`15224`)
149+
- Added ``.empty`` property to subclasses of ``Index``. (:issue:`15270`)
149150

150151
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
151152

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 self.values.size
883+
880884
def max(self):
881885
""" The maximum value of the object """
882886
return nanops.nanmax(self.values)

pandas/tests/indexes/common.py

+6
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,9 @@ 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+
# GH 15270
885+
index = self.create_index()
886+
self.assertFalse(index.empty)
887+
self.assertTrue(index[:0].empty)

0 commit comments

Comments
 (0)