I'm trying to understand why I get this error. I already have a solution for this issue and it was actually solved here, just need to understand why it doesn't work as I was expecting.
I would like to understand why this throws a KeyError:
dates = pd.date_range('20130101', periods=4)
df = pd.DataFrame(np.identity(4), index=dates, columns=list('ABCD'))
df.loc[['20130102', '20130103'],:]
with the following feedback:
KeyError: "None of [['20130102', '20130103']] are in the [index]"
As explained here, the solution is just to do:
df.loc[pd.to_datetime(['20130102','20130104']),:]
So the problem is definitely with the way loc takes the string list as argument for selecting from a DateTimeIndex. However, I can see that the following calls are ok for this function:
df.loc['20130102':'20130104',:]
and
df.loc['20130102']
I would like to understand how this works and would appreciate any resources I can use to predict the behavior of this function depending of how it is being called. I read Indexing and Selecting Data and Time Series/Date functionality from pandas documentation but couldn't find an explanation for this.