Skip to content

BUG: DataFrame index name is missing after call ".loc[]" in some cases. #42188

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
2 of 3 tasks
PengSY opened this issue Jun 22, 2021 · 4 comments
Closed
2 of 3 tasks

BUG: DataFrame index name is missing after call ".loc[]" in some cases. #42188

PengSY opened this issue Jun 22, 2021 · 4 comments
Labels
Closing Candidate May be closeable, needs more eyeballs

Comments

@PengSY
Copy link

PengSY commented Jun 22, 2021

  • 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

@PengSY PengSY added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 22, 2021
@raghuabhishek
Copy link
Contributor

I reproduced your code and I got this output

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")
df1
col1 col2
col_index
a 1 2
b 2 3
b 3 4
df2
col1 col2
col_index
a 1 2
b 2 3
c 3 4
selected_index = df1.index.intersection(['a'])
print(df1.loc[selected_index])
col1 col2
col_index
a 1 2
print(df2.loc[selected_index])
col1 col2
col_index
a 1 2

I didnt get this issue which you are facing. I am currently using pandas : 1.4.0.dev0+58.g98e22297bb version

@attack68
Copy link
Contributor

  • I have confirmed this bug exists on the latest version of pandas.

latest version is 1.2.4 or 1.3.0 pre-release. Your post suggests you are using 1.1.5

@attack68 attack68 added Closing Candidate May be closeable, needs more eyeballs and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 22, 2021
@ankurtri
Copy link

I am not able to recreate this issue on pd.version == '1.2.4'.

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jun 25, 2021
@simonjayhawkins
Copy link
Member

fixed in commit: [7e2aa42] BUG: name retention in Index.intersection (#38111) which was released in pandas 1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

5 participants