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
import pandas as pd
df1 = pd.DataFrame({"col1": [1, 2, 3], "col2": [2, 3, 4], "col_index": ['a', 'b', 'b']}).set_index("col_index")
df2 = pd.DataFrame({"col1": [1, 2, 3], "col2": [2, 3, 4], "col_index": ['a', 'b', 'c']}).set_index("col_index")
selected_index = df1.index.intersection(['a'])
print(df1.loc[selected_index])
print(df2.loc[selected_index])
Problem description
These two dataframes are almost the same, except the last index value. However, after we select the first row with index as "a" by .loc[]
on both dataframes, the returned dataframes are different. And the index name of the result for df2
is missing, which is unexpected.
>>> print(df1.loc[selected_index])
col1 col2
col_index
a 1 2
>>> print(df2.loc[selected_index])
col1 col2
a 1 2
Expected Output
The index name for the df.loc[selected_index])
should be col_index
as it is for df1
.
Output of pd.show_versions()
pd.show_versions()
INSTALLED VERSIONS
commit : b5958ee
python : 3.6.8.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.1.5
numpy : 1.18.5
pytz : 2019.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 52.0.0.post20210125
Cython : 0.29.14
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None