Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
ENH: Add empty property to Index. #15270
Conversation
codecov-io
commented
Jan 31, 2017
•
Codecov Report
@@ Coverage Diff @@
## master #15270 +/- ##
==========================================
- Coverage 90.37% 90.37% -0.01%
==========================================
Files 135 135
Lines 49466 49468 +2
==========================================
+ Hits 44705 44706 +1
- Misses 4761 4762 +1
Continue to review full report at Codecov.
|
|
Happy to open a separate issue for this to close if it's desired. Whatsnew entry is currently just pointing at this PR. |
| @@ -877,6 +877,10 @@ def _values(self): | ||
| """ the internal implementation """ | ||
| return self.values | ||
| + @property | ||
| + def empty(self): | ||
| + return not bool(self.size) |
ssanderson
Jan 31, 2017
•
Contributor
It looks like Series is inheriting its current implementation from NDFrame, but this one will win because IndexOpsMixin appears first in its bases.
ssanderson
Jan 31, 2017
Contributor
Do you think it's worth trying to push the implementation in NDFrame elsewhere?
ssanderson
Jan 31, 2017
Contributor
yeah, I didn't mean to imply that the NDFrame impl worked here, I meant that we could push the NDFrame impl into a subclass (e.g. into an intermediate class between NDFrame and DataFrame/Panel). I doubt that's worth the added complexity though.
| + | ||
| + def test_empty(self): | ||
| + index = self.create_index() | ||
| + self.assertFalse(index.empty) |
|
PR number is fine though pls search for an open issue (might be one) |
|
Whatsnew added and comment added in the tests. |
|
The other PR was linked to the wrong issue. It was indeed fixing something else (corrected that now) |
| @@ -877,6 +877,10 @@ def _values(self): | ||
| """ the internal implementation """ | ||
| return self.values | ||
| + @property | ||
| + def empty(self): | ||
| + return not self.values.size |
jorisvandenbossche
Jan 31, 2017
Owner
Could also use self.size instead?
Index.size uses the _values instead of values. Didn't follow that recently, but I think the values get converted to objects for PeriodIndex, giving an unnecessary slowdown.
jreback
added Indexing Reshaping
labels
Jan 31, 2017
|
@ssanderson trivial change. pls ping when green. |
jreback
added this to the
0.20.0
milestone
Jan 31, 2017
|
@jreback updated to just use |
|
while you are at it, can you add |
| @@ -877,6 +877,10 @@ def _values(self): | ||
| """ the internal implementation """ | ||
| return self.values | ||
| + @property | ||
| + def empty(self): | ||
| + return not self.size |
jreback
Jan 31, 2017
Contributor
actually this might just work for all NDFrame.
can you also move the doc-string to here.
ssanderson
Feb 20, 2017
•
Contributor
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.
jreback
Feb 20, 2017
•
Contributor
you can easily fix the doc-strings by using the _shared_docs. ok with having one for Index and one in generic instead.
|
can you rebase / update ....so close! |
jreback
removed this from the
0.20.0
milestone
Mar 23, 2017
|
can you update |
jreback
added this to the
0.20.0
milestone
Mar 28, 2017
jreback
closed this
in ec84ae3
Mar 28, 2017
|
thanks! |
mattip
added a commit
to mattip/pandas
that referenced
this pull request
Apr 3, 2017
|
|
ssanderson + mattip |
0fd2cbb
|
linebp
added a commit
to linebp/pandas
that referenced
this pull request
Apr 17, 2017
|
|
ssanderson + linebp |
86f80cb
|
ssanderson commentedJan 31, 2017
•
edited
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.git diff upstream/master | flake8 --diff