Datetime does not work in my index series
<p>I have a hf5 filled with some data. </p>

<p>when I open it in python I have this output</p>

<pre><code>hf['SB'].head()

0_1                     price   cng     
2015-07-15 07:30:00.087 12.61   4
2015-07-15 07:30:00.087 12.61   1
2015-07-15 07:30:00.087 12.61   1
2015-07-15 07:30:00.087 12.61   2
2015-07-15 07:30:00.087 12.61   19
</code></pre>

<p>This file ranges from 2015 to 2016.</p>

<pre><code>hf['SB'].index

DatetimeIndex(['2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               ...
               '2016-07-14 16:59:57.670000', '2016-07-14 16:59:58.047000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.957000'],
              dtype='datetime64[ns]', name=u'0_1', length=3961015, freq=None)
</code></pre>

<p>Well... My problem is:</p>

<p>when I want a slice, for example, august 20 2015:</p>

<pre><code>hf['SB'][datetime(2015,8,20)]
</code></pre>

<p>I got this error: <code>KeyError: datetime.datetime(2015, 8, 20, 0, 0)</code></p>

<p>But if I use:</p>

<pre><code>hf['SB']['2015-08-20']
</code></pre>

<p>its works!!!</p>

<p>There is something wrong in my index file or I'm using <code>datetime</code> function wrong?</p>

<p>I believe, that you are truing to get value for a key which is not present in index at all. <code>hf['SB']['2015-08-20']</code> will give you all records for that particular date.
See example below:</p>

<pre><code>&gt;&gt;&gt; rng = pd.date_range('1/1/2016', periods=10, freq='S')
&gt;&gt;&gt; ts = pd.Series(np.random.randn(len(rng)), index=rng)
&gt;&gt;&gt; ts = ts[1:]

&gt;&gt;&gt; ts
2016-01-01 00:00:01    0.133551
2016-01-01 00:00:02    1.067772
2016-01-01 00:00:03    0.591676
2016-01-01 00:00:04   -2.445586
2016-01-01 00:00:05    0.700155
2016-01-01 00:00:06   -0.127861
2016-01-01 00:00:07    1.116494
2016-01-01 00:00:08   -0.427959
2016-01-01 00:00:09    2.115352
Freq: S, dtype: float64

&gt;&gt;&gt; ts[datetime.date(2016,1,1)]
....
KeyError: datetime.date(2016, 1, 1)

&gt;&gt;&gt;  ts['2016-01-01']

2016-01-01 00:00:01    0.133551
2016-01-01 00:00:02    1.067772
2016-01-01 00:00:03    0.591676
2016-01-01 00:00:04   -2.445586
2016-01-01 00:00:05    0.700155
2016-01-01 00:00:06   -0.127861
2016-01-01 00:00:07    1.116494
2016-01-01 00:00:08   -0.427959
2016-01-01 00:00:09    2.115352
Freq: S, dtype: float64
</code></pre>

<p>so with <code>hf['SB']['2015-08-20']</code> you are getting all records for '2015-08-20', but none for <code>datetime.datetime(2015,8,20,0,0)</code></p>

<p>if you want a slice using datetime, try following:</p>

<pre><code>&gt;&gt;&gt; ts[datetime.datetime(2016,1,1,0,0,1):datetime.datetime(2016,1,1,0,0,3)]

2016-01-01 00:00:01    0.133551
2016-01-01 00:00:02    1.067772
2016-01-01 00:00:03    0.591676
Freq: S, dtype: float64
</code></pre>

python datetime pandas