-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Indexing MultiIndex with duplicates return Series instead of raw value #42102
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
slightly subjective what should happen here. see #39775 for many similar cases, this is possibly a duplicate. |
I think this is a bit different than #39775, which is more related to what happens if there are missing keys. Here, the issue is that if the Here's another example: import pandas as pd
df = pd.DataFrame(
[["a", 1, 10], ["a", 1, 20], ["b", 2, 30]], columns=["ab", "ot", "val"]
).set_index(["ab", "ot"])
print(df)
s2 = df["val"].loc[("b", 2)]
print("result")
print(s2)
print()
print("s2 type", type(s2))
print()
df2 = pd.DataFrame(
[["a", 1, 10], ["c", 1, 20], ["b", 2, 30]], columns=["ab", "ot", "val"]
).set_index(["ab", "ot"])
print(df2)
s3 = df2["val"].loc[("b", 2)]
print("result")
print(s3)
print()
print("s3 type", type(s3)) Here's the output:
In both cases, I am doing So now I have to check the type of the result to determine what computation to do next. This is very unfriendly! |
I can confirm this bug still exists for 2d DataFrame. Copy Paste Example Below:
Output:
|
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Problem description
When indexing a unique index value with
.loc
on aMultiIndex
containing duplicated values,the return type is a
Series
while it is a raw value for singleIndex
.Expected Output
I think we should expect a raw value to keep consistent with
Index
behavior.Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 2cb9652
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-55-generic
Version : #62~20.04.1-Ubuntu SMP Wed Jun 2 08:55:04 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8
pandas : 1.2.4
numpy : 1.20.3
pytz : 2019.3
dateutil : 2.7.3
pip : 20.0.2
setuptools : 45.2.0
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: