Description
-
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
single_index_no_duplicates = pd.Index([0, 1, 2])
single_index_with_duplicates = pd.Index([0, 1, 1])
multi_index_no_duplicates = pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 0)])
multi_index_with_duplicates = pd.MultiIndex.from_tuples([(0, 0), (1, 0), (1, 0)])
>>> pd.Series(range(3), single_index_no_duplicates).loc[0]
0
>>> pd.Series(range(3), single_index_with_duplicates).loc[0]
0
>>> pd.Series(range(3), multi_index_no_duplicates).loc[(0,0)]
0
>>> pd.Series(range(3), multi_index_with_duplicates).loc[(0,0)]
0 0 0
dtype: int64
Problem description
When indexing a unique index value with .loc
on a MultiIndex
containing duplicated values,
the return type is a Series
while it is a raw value for single Index
.
Expected Output
I think we should expect a raw value to keep consistent with Index
behavior.
>>> pd.Series(range(3), multi_index_with_duplicates).loc[(0,0)]
0
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