Skip to content

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

Closed
@burk

Description

@burk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesUsage Question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions