Skip to content

ERR: Raise a better message when mixing positional and label indexing in loc/iloc #25753

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

Closed
LucaPaterlini opened this issue Mar 17, 2019 · 4 comments · Fixed by #25759
Closed
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@LucaPaterlini
Copy link

LucaPaterlini commented Mar 17, 2019

Code Sample, a copy-pastable example if possible

import numpy as np

data = DataFrame(np.arange(16).reshape((4, 4)),
    index=['Ohio', 'Colorado', 'Utah', 'New York'],
    columns=['one', 'two', 'three', 'four'])

data[data < 5] = 0


print(data.iloc[['Colorado', 'Utah'], [3, 0, 1]])
# .ix is deprecated. Please use
# .loc for label based indexing or
# .iloc for positional indexing


print(data.iloc[['Colorado', 'Utah'], [3, 0, 1]])
#TypeError: cannot perform reduce with flexible type

print(data.loc[['Colorado', 'Utah'], [3, 0, 1]])
#KeyError: "None of [Int64Index([3, 0, 1], dtype='int64')] are in the [columns]"

Problem description

I followed the suggesion to move the ix to iloc and this is what I have got
I have used python 3.7.2 on an elemntary os 18.04

@mroeschke
Copy link
Member

Mixing positional and label based indexing is not allowed in loc or iloc.

So either data.iloc[[1, 2], [3, 0, 1]] or data.loc[['Colorado', 'Utah'], ['four', 'one', 'two]]

@mroeschke mroeschke added this to the No action milestone Mar 17, 2019
@simonjayhawkins
Copy link
Member

@mroeschke : the error message TypeError: cannot perform reduce with flexible type does not appear helpful. perhaps this should be raised as a seperate issue.

@LucaPaterlini
Copy link
Author

LucaPaterlini commented Mar 17, 2019

I am happy with the answer provided.
Just unhappy for this decision becouse It would potentially break old code as soon as ix will be fully deprecated.

The example cames from data analysis with python book and can be easily reproduced.

@mroeschke
Copy link
Member

Fair. iloc and loc could have a better error messages with mixed positional/label indexing.

The decision to deprecate ix was made was because its indexing operation was too flexible and not explicit compared to the other multitude of indexers.

@mroeschke mroeschke reopened this Mar 17, 2019
@mroeschke mroeschke added Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves and removed Usage Question labels Mar 17, 2019
@mroeschke mroeschke modified the milestones: No action, Contributions Welcome Mar 17, 2019
@mroeschke mroeschke changed the title .ix to .iloc update ERR: Raise a better message when mixing positional and label indexing in loc/iloc Mar 17, 2019
@jreback jreback modified the milestones: Contributions Welcome, 0.25.0 Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants