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: np array indexer modifed in iloc #21867

Merged
merged 17 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from 14 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
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ Indexing
- ``DataFrame.__getitem__`` now accepts dictionaries and dictionary keys as list-likes of labels, consistently with ``Series.__getitem__`` (:issue:`21294`)
- Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`)
- Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`)

-
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`)

Missing
^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,7 @@ def maybe_convert_indices(indices, n):

mask = indices < 0
if mask.any():
indices = indices.copy()
indices[mask] += n

mask = (indices >= n) | (indices < 0)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ 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


# GH 21867
array_with_neg_numbers = np.array([1, 2, -1])
array_copy = array_with_neg_numbers.copy()
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]
tm.assert_numpy_array_equal(array_with_neg_numbers, array_copy)

df.iloc[:, array_with_neg_numbers]
tm.assert_numpy_array_equal(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