-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: fix index op names and pinning #19723
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
Changes from 9 commits
e20da51
885dd68
25af8eb
c5fdf53
a68826d
eb23db0
c3d9d9f
ef845af
88c9638
ba2d4ca
5773fd8
f6412e5
f2e852c
4c5e54f
0824382
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1988,6 +1988,17 @@ def test_addsub_arithmetic(self, dtype, delta): | |
tm.assert_index_equal(idx - idx, 0 * idx) | ||
assert not (idx - idx).empty | ||
|
||
def test_iadd_preserves_name(self): | ||
# GH#17067, GH#19723 __iadd__ and __isub__ should preserve index name | ||
ser = pd.Series([1, 2, 3]) | ||
ser.index.name = 'foo' | ||
|
||
ser.index += 1 | ||
assert ser.index.name == "foo" | ||
|
||
ser.index -= 1 | ||
assert ser.index.name == "foo" | ||
|
||
|
||
class TestMixedIntIndex(Base): | ||
# Mostly the tests from common.py for which the results differ | ||
|
@@ -2301,9 +2312,16 @@ def test_ensure_index_from_sequences(self, data, names, expected): | |
tm.assert_index_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize('opname', ['eq', 'ne', 'le', 'lt', 'ge', 'gt']) | ||
@pytest.mark.parametrize('opname', ['eq', 'ne', 'le', 'lt', 'ge', 'gt', | ||
'add', 'radd', 'sub', 'rsub', | ||
'mul', 'rmul', 'truediv', 'rtruediv', | ||
'floordiv', 'rfloordiv', | ||
'pow', 'rpow', 'mod', 'divmod']) | ||
def test_generated_op_names(opname, indices): | ||
index = indices | ||
if type(index) is pd.Index and opname == 'rsub': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is specific to pd.Index, not subclasses. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls change this |
||
# method doesn't exist, see GH#19723 | ||
return | ||
opname = '__{name}__'.format(name=opname) | ||
method = getattr(index, opname) | ||
assert method.__name__ == opname |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 forIndex.__iadd__.__name__
we'll get__add__
instead of__iadd__
. Not a big deal, but its cheap to make it pretty.