-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
MultiIndex row indexing with .loc fail with tuple but work with list of indices #16943
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
Comments
I suspect this would break other tests, were tuples have different meanings that lists for slicing. I may be wrong though, if you want to give it a shot. |
this technically is not covered by the doc-string
but we almost always accept array-like (which includes tuples). The reason this is confusing slightly is that a non-nested tuple is also valid as a single indexer. |
I came across a slightly related issue: using a multi-index dataframe, why can I only use a tuple as an indexer and not a list (i.e. why do they give different results)? Using the example data, if I want to pull out rows where
This returns the desired slice:
I can't use a list:
This seems to imply that I want values
It took me quite some time to figure this out. Is this intended behavior? If yes, is this documented (I thought it should be mentioned here but didn't find anything)? |
@cbrnr Yes, that is intended behaviour. For single "labels" of a MultiIndex (so one value for each level), we always use tuples and not a list, because it would otherwise be difficult to distinguish. I think for this case we are quite consistent within pandas. So your assessment is correct: it tries to look for those values of the list in the first index level. You could interpret the list as give me the combination of indexing the dataframe with each element of the list, so |
For the original issue: given the possible confusion between the two, I think it might be better in this case to not interpret the tuple as a list-like. cc @toobaz interesting case :-) |
Thanks @jorisvandenbossche, this makes sense! I usually don't distinguish between lists and tuples in plain Python since they are both list-like objects. So this Pandas behavior tripped me up a bit - is this documented clearly somewhere? |
Ah, no, I suppose this is wrong. It seems that it does interpret So it is indexing as:
To make it a bit more confusing: it is a bit strange however that the actual list of lists ( |
Yes, this is one of the gotcha's due to the complexity of MultiIndexing that we somehow need to distinguish between both. |
I could add a statement that tuples are needed in the case of multiple indexers on a multiindex: http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers. Not a warning box, but maybe there's a note or an info box? Or is there a better place to put such a note? Let me know and I can take care of that in a PR. |
Ah, so we are actually using tuples there in the docs :-) So I just might have had the wrong assumption that a list would work (regarding the last of my comment above #16943 (comment)). |
Great idea! I think such statement should actually go at the beginning of http://pandas.pydata.org/pandas-docs/stable/advanced.html#advanced-indexing-with-hierarchical-index You could maybe start by stating that You might want to show examples of the fact that lists of tuples in general refer to multiple complete ( In [2]: s = pd.Series(-1, index=pd.MultiIndex.from_product([[1, 2], [3, 4]]))
In [3]: s.loc[[(1, 3), (2, 4)]]
Out[3]:
1 3 -1
2 4 -1
dtype: int64
In [4]: s.loc[([1, 2], [3, 4])]
Out[4]:
1 3 -1
4 -1
2 3 -1
4 -1
dtype: int64 Asides from the possible docs improvements: yes, in some cases we interpret tuples as lists, but I think it should be seen as an undesired implementation legacy. Vice-versa, I see no harm (in general - caveats clearly can apply to specific cases) in interpreting generators, dicts or other list-likes that as lists. |
See #19507 |
I think this is fixed by #19507 . Anyone feel free to reopen if you disagree. |
This is almost bizarre how you helped me with getting a dataframe by multiindex. Thank you! |
Code Sample, a copy-pastable example if possible
Problem description
Now, extracting the desired rows with
loc
fails here while returning only the first row:Expected Output
One solution would be to convert the
tuple
to alist
internally because alist
of indices work correctly:Another solution is to raise an error if a
tuple
of indices is provided as the row indexer of theloc
in order to prevent unpredicted results.Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.8.0-58-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 36.0.1
Cython: 0.25.2
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: