Iteration through a range of timestamp in Python
<p>I have a dataframe df :</p>

<pre><code>TIMESTAMP equipement1 equipement2
2016-05-10 13:20:00 0.000000 0.000000
2016-05-10 14:40:00 0.400000 0.500000
2016-05-10 15:20:00 0.500000 0.500000
</code></pre>

<p>Iam trying to iterate through timestamp by step of 5 minutes .
I try : <code>pd.date_range(start, end, freq='5 minutes')</code></p>

<p>But I get a problem with timestamp format.</p>

<blockquote>
  <p>" ValueError: Could not evaluate 5 minutes"</p>
</blockquote>

<p>Any idea to help me to resolve this problem?</p>

<p>Thank you</p>

<p>First, make sure your TIMESTAMP column is a datetime instead of a string (e.g. <code>df['TIMESTAMP'] = pd.to_datetime(df.TIMESTAMP)</code>).</p>

<p>Next, use this column as the index of the dataframe.  To make this permanent, <code>df.set_index('TIMESTAMP</code>, inplace=True)`.</p>

<p>Now you can <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html" rel="nofollow">resample</a> for any given frequency (e.g. <code>30min</code>) and use different methods of aggregation such as <code>sum</code>, <code>mean</code> (the default), a lambda function, etc).</p>

<p>Optionally, you can add <code>.fillna(0)</code> to replace the NaNs with zeros.</p>

<pre><code>&gt;&gt;&gt; df.set_index('TIMESTAMP').resample('30min', how='sum')

                     equipement1  equipement2
TIMESTAMP                                    
2016-05-10 13:00:00          0.0          0.0
2016-05-10 13:30:00          NaN          NaN
2016-05-10 14:00:00          NaN          NaN
2016-05-10 14:30:00          0.4          0.5
2016-05-10 15:00:00          0.5          0.5
</code></pre>

python date pandas