Skip to content
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

BUG: fix index op names and pinning #19723

Merged
merged 15 commits into from
Feb 23, 2018
Merged

Conversation

jbrockmendel
Copy link
Member

This is a moderately big one, but changes very little.

Fixes incorrect Index ops names and several methods that are defined but not pinned to the class (#19716).

Avoids the need to pass reversed and str_rep in a bunch of places, cleans up some of the Index classmethods.

Removes the no-longer-needed catching of AttributeError in RangeIndex ops.

The part that may be controversial (and almost certainly needs new tests) is that Index.__rsub__ is implemented, since it doesn't currently exist. This implements it in "the obvious way", but the fact that Index.__sub__ isn't defined in the obvious way suggests this might not be desired. Comments welcome.

@codecov
Copy link

codecov bot commented Feb 16, 2018

Codecov Report

Merging #19723 into master will increase coverage by 0.01%.
The diff coverage is 92.36%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #19723      +/-   ##
==========================================
+ Coverage   91.57%   91.58%   +0.01%     
==========================================
  Files         150      150              
  Lines       48903    48890      -13     
==========================================
- Hits        44785    44778       -7     
+ Misses       4118     4112       -6
Flag Coverage Δ
#multiple 89.96% <92.36%> (ø) ⬆️
#single 41.83% <77.86%> (+0.04%) ⬆️
Impacted Files Coverage Δ
pandas/core/indexes/datetimes.py 95.56% <100%> (ø) ⬆️
pandas/core/indexes/timedeltas.py 90.63% <100%> (ø) ⬆️
pandas/core/indexes/period.py 93.02% <100%> (+0.14%) ⬆️
pandas/core/indexes/datetimelike.py 97.33% <100%> (+0.43%) ⬆️
pandas/core/indexes/range.py 95.63% <85.71%> (-0.11%) ⬇️
pandas/core/indexes/base.py 96.53% <91.3%> (+0.11%) ⬆️
pandas/core/frame.py 97.23% <0%> (ø) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b585e3b...0824382. Read the comment docs.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

so leave out the rsub changes for now. the refactor part is ok

raise TypeError("cannot subtract TimedeltaIndex and {typ}"
.format(typ=type(other).__name__))
return self._add_delta(-other)
assert not is_timedelta64_dtype(other)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is redundant

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove.

@jreback jreback added Datetime Datetime data dtype API Design Numeric Operations Arithmetic, Comparison, and Logical operations Timedelta Timedelta data type labels Feb 16, 2018
@jreback
Copy link
Contributor

jreback commented Feb 16, 2018

its also not clear the changes are actually being tested.

@jbrockmendel
Copy link
Member Author

its also not clear the changes are actually being tested.

The added parameters in tests.indexes.test_base test the relevant changes here. That test would fail under the status-quo.

@jbrockmendel
Copy link
Member Author

so leave out the rsub changes for now. the refactor part is ok

OK. That will require special-casing to escape that case in tests.indexes.test_base; we can revisit later.

@jbrockmendel
Copy link
Member Author

This one isn’t particularly conducive to splitting into smaller parts, but I could try if that’ll help.

def test_generated_op_names(opname, indices):
index = indices
if type(index) is pd.Index and opname == 'rsub':
Copy link
Contributor

Choose a reason for hiding this comment

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

use isinstance, or is this a very specific check if some is NOT a Index. maybe better to use ABCIndex and/or ABCIndexClass

Copy link
Member Author

Choose a reason for hiding this comment

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

This is specific to pd.Index, not subclasses.

Copy link
Contributor

Choose a reason for hiding this comment

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

pls change this

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

need some tests for iadd/isub, in particular

#17067 (2 things going on in that issue(

e.g.

s = pd.Series([1, 2, 3])
s.index.name = "foo"

s.index += 1
assert s.index.name == "foo"

@jbrockmendel
Copy link
Member Author

The reset_index thing looks unrelated. Am I reading it wrong?

@jreback
Copy link
Contributor

jreback commented Feb 20, 2018

the reset_index is unrelated
the i ops are the relevant parts

@jbrockmendel
Copy link
Member Author

This may be all set.

def __iadd__(self, other):
# alias for __add__
return self.__add__(other)
cls.__iadd__ = __iadd__
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need this double assignment ?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we just set cls.__iadd__ = __add__ then when we check for Index.__iadd__.__name__ we'll get __add__ instead of __iadd__. Not a big deal, but its cheap to make it pretty.

def test_generated_op_names(opname, indices):
index = indices
if type(index) is pd.Index and opname == 'rsub':
Copy link
Contributor

Choose a reason for hiding this comment

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

pls change this

@jbrockmendel
Copy link
Member Author

pls change this

Sure, to issubclass(index, ABCIndex)? Not worth arguing about, but whats the upside? It's definitely not clearer (especially b/c it is easy for newcomers to not be aware that ABCIndex doesnt include subclasses)

@jorisvandenbossche
Copy link
Member

It's definitely not clearer (especially b/c it is easy for newcomers to not be aware that ABCIndex doesnt include subclasses)

I for one didn't know (and wouldn't know directly from reading code, and find it also surprising) that ABCIndex is only the Index class and not its subclasses. type(..) is Index is much more explicit IMO.

@jorisvandenbossche
Copy link
Member

I for one didn't know (and wouldn't know directly from reading code, and find it also surprising) that ABCIndex is only the Index class and not its subclasses. type(..) is Index is much more explicit IMO.

But I have to agree it is clear from the abc code (I just don't interact much with that part of the code base that needs to use ABCs). I only intuitively would have expected the names to be the other way around :-)

@jorisvandenbossche jorisvandenbossche changed the title Fix index op names and pinning BUG: fix index op names and pinning Feb 21, 2018
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Does this need a whatsnew note?

@jbrockmendel
Copy link
Member Author

Does this need a whatsnew note?

I guess its technically user-facing, but it seems like it would be mostly clutter. I'm fine either way.

@jreback jreback added this to the 0.23.0 milestone Feb 22, 2018
@jreback
Copy link
Contributor

jreback commented Feb 22, 2018

needs a rebase

@jbrockmendel
Copy link
Member Author

I messed up most recent merge, will fix in AM.

@jreback
Copy link
Contributor

jreback commented Feb 23, 2018

lgtm. ping on green. (I think just appeveyor left).

@jbrockmendel
Copy link
Member Author

ping

@jreback jreback merged commit 0ffc4b5 into pandas-dev:master Feb 23, 2018
@jreback
Copy link
Contributor

jreback commented Feb 23, 2018

thanks

harisbal pushed a commit to harisbal/pandas that referenced this pull request Feb 28, 2018
@jbrockmendel jbrockmendel deleted the opprep branch June 22, 2018 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype Numeric Operations Arithmetic, Comparison, and Logical operations Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrectly assigned Index ops/names
3 participants