Consider a date-indexed DataFrame:
d0 = datetime.date(2024,5,5)
d1 = datetime.date(2024,5,10)
df0 = pd.DataFrame({"a":[1,2],"b":[10,None],"c":list("xy")}, index=[d0,d1])
df0.index
Index([2024-05-05, 2024-05-10], dtype='object')
Note that df0.index.dtype
is object
.
Now, lookup works for date
:
df0.loc[d0]
a 1
b 10.0
c x
Name: 2024-05-05, dtype: object
but both df0.loc[str(d0)]
and df0.loc[pd.Timestamp(d0)]
raise KeyError
.
This seems to be reasonable.
However, consider
df1 = df0.reindex(pd.date_range(d0,d1))
df1.index
DatetimeIndex(['2024-05-05', '2024-05-06', '2024-05-07', '2024-05-08',
'2024-05-09', '2024-05-10'],
dtype='datetime64[ns]', freq='D')
Note that df1.index.dtype
is datetime64
.
Now, lookup works for both df1.loc[pd.Timestamp(d0)]
(as expected) and df1.loc[str(d0)]
(why?!) but not for df1.loc[d0]
(if it works for a string, why not date
?!)
Is this the expected behavior (a bug with tenure)? Is this intentional?