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

TST: avoid chained assignment in tests outside of specific tests on chaining #49474

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jorisvandenbossche
Copy link
Member

@jorisvandenbossche jorisvandenbossche commented Nov 2, 2022

Similarly as #46980, now splitting off a part of the test changes in #49467 that can be done separately

We have specific tests about chained indexing (in pandas/tests/indexing, eg test_chaining_and_caching.py), so outside those specific indexing tests, I can think we can avoid using chained indexing (regardless of the new CoW implementation, this would follow our own recommendation on best indexing practices, although it also shows that some cases of mixed positional/label based setting is somewhat convoluted ..)

@jorisvandenbossche jorisvandenbossche added the Testing pandas testing functions or related to the test suite label Nov 2, 2022
@jorisvandenbossche
Copy link
Member Author

jorisvandenbossche commented Nov 2, 2022

There are in general two ways to avoid chained assignment if you have mixed label / positional based indexing. Considering the example of frame["A"][:10] (where frame has a non-integer index, so :10 is positional), you can use either loc or iloc instead:

frame.loc[: frame.index[9], "A"] = ...
# or
frame.iloc[:10, frame.columns.get_loc("A")]

Do we have a preference for which to use in general?
I started using the first, but this becomes inconvenient when slicing (label based is inclusive, so you need to decrement the slice start/stop for getting the equivalent index label; although for many tests this wouldn't actually matter for what is being tested). So later on I switched to iloc seeing this is a bit easier in that case. I can update the PR to more consistently use one of both methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant