How to access pandas DataFrame datetime index using strings
<p>This is a very simple and practical question. I have the feeling that it must be a silly detail and that there should be similar questions. I wasn't able to find them tho. If someone does I'll happily delete this one.</p>

<p>The closest I found were these:
<a href="https://stackoverflow.com/questions/27501694/pandas-iterating-over-dataframe-index-with-loc/27527628#27527628">pandas: iterating over DataFrame index with loc</a></p>

<p><a href="https://stackoverflow.com/questions/13221218/how-to-select-rows-within-a-pandas-dataframe-based-on-time-only-when-index-is-da">How to select rows within a pandas dataframe based on time only when index is date and time</a></p>

<p>anyway, the thing is, I have a datetime indexed panda dataframe as follows:</p>

<pre><code>In[81]: y
Out[81]: 
            PETR4  CSNA3  VALE5
2008-01-01    0.0    0.0    0.0
2008-01-02    1.0    1.0    1.0
2008-01-03    7.0    7.0    7.0

In[82]: y.index
Out[82]: DatetimeIndex(['2008-01-01', '2008-01-02', '2008-01-03'], dtype='datetime64[ns]', freq=None)
</code></pre>

<p>Oddly enough, I can't access its values using none of the following methods:</p>

<pre><code>In[83]: y[datetime.datetime(2008,1,1)]
In[84]: y['2008-1-1']
In[85]: y['1/1/2008']
</code></pre>

<p>I get the <code>KeyError</code> error.</p>

<p>Even more weird is that the following methods DO work:</p>

<pre><code>In[86]: y['2008']
Out[86]: 
            PETR4  CSNA3  VALE5
2008-01-01    0.0    0.0    0.0
2008-01-02    1.0    1.0    1.0
2008-01-03    7.0    7.0    7.0
In[87]: y['2008-1']
Out[87]: 
            PETR4  CSNA3  VALE5
2008-01-01    0.0    0.0    0.0
2008-01-02    1.0    1.0    1.0
2008-01-03    7.0    7.0    7.0
</code></pre>

<p>I'm fairly new to pandas so maybe I'm missing something here?</p>