Skip to content

Commit ec84ae3

Browse files
Scott Sandersonjreback
Scott Sanderson
authored andcommitted
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`. Author: Scott Sanderson <[email protected]> This patch had conflicts when merged, resolved by Committer: Jeff Reback <[email protected]> closes #13207 Closes #15270 from ssanderson/add-empty-to-index and squashes the following commits: bb0126f [Scott Sanderson] ENH: Add empty property to Index.
1 parent 6f789e1 commit ec84ae3

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ Attributes
12771277
Index.nbytes
12781278
Index.ndim
12791279
Index.size
1280+
Index.empty
12801281
Index.strides
12811282
Index.itemsize
12821283
Index.base

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,13 @@ Other Enhancements
310310
- ``Series/DataFrame.squeeze()`` have gained the ``axis`` parameter. (:issue:`15339`)
311311
- ``DataFrame.to_excel()`` has a new ``freeze_panes`` parameter to turn on Freeze Panes when exporting to Excel (:issue:`15160`)
312312
- HTML table output skips ``colspan`` or ``rowspan`` attribute if equal to 1. (:issue:`15403`)
313+
313314
- ``pd.TimedeltaIndex`` now has a custom datetick formatter specifically designed for nanosecond level precision (:issue:`8711`)
314315
- ``pd.types.concat.union_categoricals`` gained the ``ignore_ordered`` argument to allow ignoring the ordered attribute of unioned categoricals (:issue:`13410`). See the :ref:`categorical union docs <categorical.union>` for more information.
315316
- ``pandas.io.json.json_normalize()`` with an empty ``list`` will return an empty ``DataFrame`` (:issue:`15534`)
316317
- ``pd.DataFrame.to_latex`` and ``pd.DataFrame.to_string`` now allow optional header aliases. (:issue:`15536`)
317318
- Re-enable the ``parse_dates`` keyword of ``read_excel`` to parse string columns as dates (:issue:`14326`)
319+
- Added ``.empty`` property to subclasses of ``Index``. (:issue:`15270`)
318320

319321
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
320322

pandas/core/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,10 @@ def _values(self):
774774
""" the internal implementation """
775775
return self.values
776776

777+
@property
778+
def empty(self):
779+
return not self.size
780+
777781
def max(self):
778782
""" The maximum value of the object """
779783
return nanops.nanmax(self.values)

pandas/tests/indexes/common.py

+6
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,9 @@ def test_nulls(self):
910910
result = isnull(index)
911911
self.assert_numpy_array_equal(index.isnull(), result)
912912
self.assert_numpy_array_equal(index.notnull(), ~result)
913+
914+
def test_empty(self):
915+
# GH 15270
916+
index = self.create_index()
917+
self.assertFalse(index.empty)
918+
self.assertTrue(index[:0].empty)

0 commit comments

Comments
 (0)