Skip to content

TST: boolean indexing using .iloc #20627 #34622

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 5 commits into from
Jun 13, 2020
Merged
Changes from 1 commit
Commits
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
20 changes: 20 additions & 0 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,23 @@ def test_set_value_by_index(self):

df.iloc[:, 0] = 3
tm.assert_series_equal(df.iloc[:, 1], expected)


def test_iloc_with_boolean_operation():
# GH 20627
result = DataFrame([[0, 1], [2, 3], [4, 5], [6, np.nan]])
result.iloc[result.index <= 2] *= 2
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to copy result each time you are using it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure ? result is modified inplace ? (or I don't understand)

Copy link
Contributor

Choose a reason for hiding this comment

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

i get it. for each part of the test you want to reconstruct result, so that the in place mods only affect 1 part and are not consecutive.

expected = DataFrame([[0, 2], [4, 6], [8, 10], [6, np.nan]])
tm.assert_frame_equal(result, expected)

result.iloc[result.index > 2] *= 2
expected = DataFrame([[0, 2], [4, 6], [8, 10], [12, np.nan]])
tm.assert_frame_equal(result, expected)

result.iloc[[True, True, False, False]] *= 2
expected = DataFrame([[0, 4], [8, 12], [8, 10], [12, np.nan]])
tm.assert_frame_equal(result, expected)

result.iloc[[False, False, True, True]] /= 2
expected = DataFrame([[0.0, 4.0], [8.0, 12.0], [4.0, 5.0], [6.0, np.nan]])
tm.assert_frame_equal(result, expected)