Skip to content

BUG: Strange behavior when accessing datetime.date index with np.datetime64 #55969

@yavitzour

Description

@yavitzour

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
import numpy as np
import datetime as dt

dates = [dt.datetime(2023, 11, 1).date(), dt.datetime(2023, 11, 1).date(), dt.datetime(2023, 11, 2).date()]
t1 = ["A", "B", "C"]
t2 = ["C", "D", "E"]
vals = np.random.uniform(size=len(dates))

df = pd.DataFrame(data=np.array([dates, t1, t2, vals]).T, columns=["dates", "t1", "t2", "vals"])
df.set_index(["dates", "t1", "t2"], inplace=True)

date = np.datetime64("2023-11-01")

print("df:\n ", df)
print("\n df.loc[(date, 'A')] - Expected to get only the first row but getting two rows:\n\n", df.loc[(date, "A")])
print("\n df.loc[(date, 'B')] - Expected to get only the second row but getting two rows:\n\n", df.loc[(date, "B")])
print("\n df.loc[(date, 'C')] - Expected to get an error but still getting two rows:\n\n", df.loc[(date, "C")])

# Note - the bug doesn't exist when explicitly adding slice(None) to the third index level

Issue Description

There may be a simpler case where this occurs, but this is the simplest I could find.

When using a multiindex with 3 levels, where the type of the first level is datetime.date, and trying to access data matching the first two levels of the index, omitting the third level and using type np.datetime64 for the date, pandas returns all the rows matching the date, regardless of the value of the second level index.

The bug disappears when explicitly adding slice(None) for the third level, i.e. using df.loc[(date, "A", slice(None))] instead of df.loc[(date, "A")]

Expected Behavior

Expected behavior would be either to produce an error because the dates types are mismatched, or to produce the correct results, as described in my code example.

Installed Versions

INSTALLED VERSIONS

commit : 2a953cf
python : 3.9.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-213-generic
Version : #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.3
numpy : 1.23.4
pytz : 2021.3
dateutil : 2.8.2
setuptools : 56.0.0
pip : 23.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 7.30.1
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
sqlalchemy : 2.0.16
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions