Skip to content

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

Merged
merged 17 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
8c6161c
BUG: np array indexer modifed in iloc
ydovzhenko Jul 11, 2018
f71eb24
BUG: np array indexer modifed in iloc
ydovzhenko Jul 11, 2018
594e805
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 11, 2018
9a9054d
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 11, 2018
e0e82f3
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 11, 2018
7bd51cb
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 11, 2018
5d65995
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 12, 2018
5db95df
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 12, 2018
376fb3b
Merge remote-tracking branch 'upstream/master' into array-modificatio…
ydovzhenko Jul 13, 2018
7a5357c
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
a5c1a13
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
350680e
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
77bd054
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
cb5fa3e
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
e15b9d2
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
4e3d34b
Merge remote-tracking branch 'upstream/master' into array-modificatio…
ydovzhenko Jul 13, 2018
4ce3a47
Merge branch 'array-modification-on-iloc' of https://github.com/ydovz…
ydovzhenko Jul 13, 2018
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
4 changes: 4 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=W0223
from copy import deepcopy
Copy link
Member

Choose a reason for hiding this comment

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

Don't need

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad.

import textwrap
import warnings
import numpy as np
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

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

Should be able to just use .copy() instead of importing deepcopy, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, true. Will change to .copy()

Copy link
Member

Choose a reason for hiding this comment

The 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)
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the comment

indices = np.copy(indices)
Copy link
Contributor

Choose a reason for hiding this comment

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

use indices.copy()

indices[mask] += n

mask = (indices >= n) | (indices < 0)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

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

Use tm.assert_numpy_array_equal instead both here and below


df.iloc[:, array_with_neg_numbers]
assert np.all(array_with_neg_numbers == array_copy)

Copy link
Member

Choose a reason for hiding this comment

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

Should only need one blank line here (think this will fail LINTing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 LINT=1 ci/lint.sh, but that takes longer and may have additional dependencies.

https://travis-ci.org/pandas-dev/pandas/jobs/403382274#L2928 is the line causing this PR to fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

flake8 pandas/tests/indexing/test_iloc.py should work for you.


def test_iloc_getitem_list_int(self):

# list of ints
Expand Down