-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: np array indexer modifed in iloc #21867
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 5 commits
8c6161c
f71eb24
594e805
9a9054d
e0e82f3
7bd51cb
5d65995
5db95df
376fb3b
7a5357c
a5c1a13
350680e
77bd054
cb5fa3e
e15b9d2
4e3d34b
4ce3a47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# pylint: disable=W0223 | ||
from copy import deepcopy | ||
import textwrap | ||
import warnings | ||
import numpy as np | ||
|
@@ -2586,6 +2587,7 @@ def maybe_convert_indices(indices, n): | |
IndexError : one of the converted indices either exceeded the number | ||
of elements (specified by `n`) OR was still negative. | ||
""" | ||
indices = deepcopy(indices) | ||
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. Can this be done down on line 2601? Avoid a copy if at all possible 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. Ok moving deepcopy to 2601 makes sense; will do. Can't see a way to avoid copying altogether. 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. Should be able to just use 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. Yes, true. Will change to .copy() 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. Can get rid of this now |
||
|
||
if isinstance(indices, list): | ||
indices = np.array(indices) | ||
|
@@ -2596,6 +2598,8 @@ def maybe_convert_indices(indices, n): | |
|
||
mask = indices < 0 | ||
if mask.any(): | ||
# indices is np.array, so we don't use deepcopy | ||
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. you don't need the comment |
||
indices = np.copy(indices) | ||
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 |
||
indices[mask] += n | ||
|
||
mask = (indices >= n) | (indices < 0) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,24 @@ def test_iloc_getitem_neg_int(self): | |
typs=['labels', 'mixed', 'ts', 'floats', 'empty'], | ||
fails=IndexError) | ||
|
||
def test_iloc_array_not_mutating_negative_indices(self): | ||
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. can you add the issue number here as a comment |
||
|
||
# Test that iloc does not modify index array | ||
# if the array contains negative integers | ||
array_with_neg_numbers = np.array([1, 2, -1]) | ||
array_copy = np.copy(array_with_neg_numbers) | ||
df = pd.DataFrame({ | ||
'A': [100, 101, 102], | ||
'B': [103, 104, 105], | ||
'C': [106, 107, 108]}, | ||
index=[1, 2, 3]) | ||
df.iloc[array_with_neg_numbers] | ||
assert np.all(array_with_neg_numbers == array_copy) | ||
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 |
||
|
||
df.iloc[:, array_with_neg_numbers] | ||
assert np.all(array_with_neg_numbers == array_copy) | ||
|
||
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. Should only need one blank line here (think this will fail LINTing) 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. I can't find LINTing in the contributing guide. Can I LINT myself and how exactly do you guys do that? 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. http://pandas-docs.github.io/pandas-docs-travis/contributing.html#python-pep8 has the quick version, but that doesn't check everything. The actual command run during CI is https://travis-ci.org/pandas-dev/pandas/jobs/403382274#L2928 is the line causing this PR to fail. 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.
|
||
|
||
def test_iloc_getitem_list_int(self): | ||
|
||
# list of ints | ||
|
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.
Don't need
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.
My bad.