Skip to content

BUG: Using .loc with DatetimeIndex drops index name #45818

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
burk opened this issue Feb 4, 2022 · 3 comments
Closed
2 of 3 tasks

BUG: Using .loc with DatetimeIndex drops index name #45818

burk opened this issue Feb 4, 2022 · 3 comments
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question

Comments

@burk
Copy link

burk commented Feb 4, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
x = pd.DataFrame({'y':[1,2,3]}, index=pd.DatetimeIndex(["2020-01-01","2020-01-02", "2020-01-03"]))
x.index.names = ["abc"]

print(x)
#             y
# abc          
# 2020-01-01  1
# 2020-01-02  2
# 2020-01-03  3

print(x.loc[pd.DatetimeIndex(["2020-01-01"])])
#             y
# 2020-01-01  1

Issue Description

When using .loc to select a subset of rows in a data frame with a DatetimeIndex, the index name is lost.

Expected Behavior

The index name is not lost.

Installed Versions

INSTALLED VERSIONS

commit : bb1f651
python : 3.9.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.0-10-amd64
Version : #1 SMP Debian 5.10.84-1 (2021-12-08)
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.0
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.2
setuptools : 57.4.0
Cython : 0.29.27
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : 3.0.2
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : 2.9.2
jinja2 : None
IPython : 8.0.1
pandas_datareader: 0.10.0
bs4 : None
bottleneck : 1.3.2
fastparquet : 0.8.0
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.5.1
numba : 0.55.1
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : 2022.01.0
scipy : 1.7.3
sqlalchemy : 1.4.28
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

@burk burk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 4, 2022
@phofl
Copy link
Member

phofl commented Feb 4, 2022

This is the expected behavior. The method keeps the name of the passed Index, which is None. Additionally: If you have a single index, use the name attribute, not names

see https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html

@phofl phofl closed this as completed Feb 4, 2022
@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Usage Question and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Feb 4, 2022
@phofl phofl added this to the No action milestone Feb 4, 2022
@burk
Copy link
Author

burk commented Feb 4, 2022

Ok, it did not work this way in pandas 1.3.5, which is what led me to believe it was a bug, but we'll update our code when we upgrade then. Thanks!

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 11, 2022
@simonjayhawkins
Copy link
Member

Ok, it did not work this way in pandas 1.3.5, which is what led me to believe it was a bug, but we'll update our code when we upgrade then. Thanks!

looks like an undocumented bug fix from commit: [4a2096d] CI: Fix doctests (#42790)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants