Skip to content

ENH: Add empty property to Index. #15270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this might just work for all NDFrame.

can you also move the doc-string to here.

Copy link
Contributor Author

@ssanderson ssanderson Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the current docstring on NDFrame would make sense if moved here, since it makes references to NDFrame, and it has examples geared toward DataFrame, both of which would be confusing if they showed up on a property of Index.

Coming back to this with fresh eyes, I'm now inclined to just put empty on the base Index class and give it an Index-specific docstring. The root issue here is that there are two places from which Series could be getting it's implementation of empty: NDFrame, which provides shared implementations for Series/DataFrame/Panel, and IndexOpsMixin, which provides shared implementations for Index and Series. In the case of empty (and, in general, any property that belongs on both containers and indexes) I think putting the implementation on IndexOpsMixin ends up being more confusing than helpful, because it's no longer clear which implementation will end up getting used for Series. Does that seem reasonable?

Separately, I could replace the current implementation of NDFrame.empty with this if you think it's preferable, though I'm not sure it's a worthwhile improvement by itself.

Copy link
Contributor

@jreback jreback Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can easily fix the doc-strings by using the _shared_docs. ok with having one for Index and one in generic instead.


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)