Skip to content

ERR: improve error message with unalignable label indexing #14491

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
bluenote10 opened this issue Oct 25, 2016 · 4 comments · Fixed by #14762
Closed

ERR: improve error message with unalignable label indexing #14491

bluenote10 opened this issue Oct 25, 2016 · 4 comments · Fixed by #14762
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@bluenote10
Copy link

A small, complete example of the issue

Using a boolean index in a pandas.Series on a data frame with an string index fails with Unalignable boolean Series key provided:

import pandas as pd

# DF without string index
df = pd.DataFrame({"A": [0, 1, 2]})
df.loc[[True, True, True], :]               # works
df.loc[pd.Series([True, True, True]), :]    # works

# DF with string index
df = pd.DataFrame({"A": [0, 1, 2]}, index=["a", "b", "c"])
df.loc[[True, True, True], :]               # works
df.loc[pd.Series([True, True, True]), :]    # fails

Expected Output

Boolean indexing should work with a pandas.Series like it does with regular Python lists and boolean numpy arrays.

Output of pd.show_versions()

## INSTALLED VERSIONS

commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-98-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: None
pip: 8.1.2
setuptools: 27.2.0
Cython: 0.24.1
numpy: 1.11.1
scipy: 0.18.0
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.1.0
tables: None
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: None
pandas_datareader: None

@jorisvandenbossche
Copy link
Member

I think the error message is rather clear: "Unalignable boolean Series key provided" -> the index of the boolean series is not alignable with the index of the DataFrame you are trying to index. Lists or arrays don't have an index, so there is not an issue. If you want to ignore the index of the series, you can use s.values to index.

Not sure if this is documented somewhere thoroughly.

@bluenote10
Copy link
Author

Understood, but then it is mainly the error message that is very confusing: A series of length 3 is alignable intuitively with a length-3-dataframe, it is just not what Pandas considers alignable.

@jorisvandenbossche
Copy link
Member

Ah yes, it is unalignable on the labels of the indices (although the length matches). That is what the term 'alignment' means in pandas-world.
But we would maybe add in the error massage some more explanation.

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Oct 26, 2016
@jreback jreback added this to the Next Major Release milestone Oct 26, 2016
@jreback
Copy link
Contributor

jreback commented Oct 26, 2016

ok will mark this for error message improvement @bluenote10 if you'd like to submit a PR.

I suppose you could mention that this is label alignment would be enough.

@jreback jreback changed the title "Unalignable boolean Series key provided" with string index and boolean pandas.Series ERR: improve error message with unalignable label indexing Oct 26, 2016
Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Nov 29, 2016
Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Nov 29, 2016
@jreback jreback modified the milestones: 0.20.0, Next Major Release Dec 4, 2016
Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Dec 6, 2016
ERR: Change error message   pandas-dev#14491

ENH: pandas-dev#11897 make lint work.  ERR: pandas-dev#14491 change error message

ERR: pandas-dev#14491 fix test for error message

fixes based on jreback feedback

fix indent issue

Doc fixes

Fixes per jreback comments
Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Dec 7, 2016
Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Dec 9, 2016
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.

3 participants