Issue
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 ???
Solution
Your DataFrame doesn't have a string index - it has a datetime.date index.
try
df.loc[datetime.date(2016, 10, 3)]
Better yet, create a datetime index:
df.index = pd.to_datetime(df.index)
Then, your original indexing will work.
Answered By - gshpychka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.