Consider the following df:
d = {'.KS200': {datetime.date(2016, 10, 3): nan, datetime.date(2016, 10, 4): 259.18, datetime.date(2016, 10, 5): 258.99, datetime.date(2016, 10, 6): 261.13, datetime.date(2016, 10, 7): 260.06}, '0001.HK': {datetime.date(2016, 10, 3): 99.45, datetime.date(2016, 10, 4): 99.45, datetime.date(2016, 10, 5): 99.25, datetime.date(2016, 10, 6): 98.7, datetime.date(2016, 10, 7): 98.0}}
df = pd.DataFrame.from_dict(d)
print(df)
.KS200 0001.HK
2016-10-03 7.00 99.45
2016-10-04 259.18 99.45
2016-10-05 258.99 99.25
2016-10-06 261.13 98.70
2016-10-07 260.06 98.00
Now if I try to index:
df.loc['2016-10-03']
I get a raise KeyError(key) from err
Oddly enough when I print:
print(df.index) I get dtype=object instead of datetime so is it a datetime or is it an object my index ???
to_dict(), and it printsdatetimebut I never specify before in my code, whenever a printdf.indexI getobjectasdtype. That's the confusiondatetimewhyprint(df.index)is equal toobject??????objectis probably the underlying use of NumPy. It should indeed bedatetime.date(or perhaps a Pandas datetime object). NumPy, however, doesn't know datetimes as standard types. If you look atdf.index.values, you'll see it's a NumPy array, anddf.index.values.dtypeis alsoobject. Possibly a shortcoming of Pandas (might be noted in an issue somewhere even, if you search around).