extract hour from timestamp with python
<p>I have a dataframe  df_energy2</p>

<pre><code>df_energy2.info()
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 29974 entries, 0 to 29973
Data columns (total 4 columns):
TIMESTAMP        29974 non-null datetime64[ns]
P_ACT_KW         29974 non-null int64
PERIODE_TARIF    29974 non-null object
P_SOUSCR         29974 non-null int64
dtypes: datetime64[ns](1), int64(2), object(1)
memory usage: 936.8+ KB
</code></pre>

<p>with this structure :</p>

<pre><code>df_energy2.head()

TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR
2016-01-01 00:00:00 116 HC 250 
2016-01-01 00:10:00 121 HC 250
</code></pre>

<p>Is there any python fucntion which can extract hour from <code>TIMESTAMP</code>?</p>

<p>Kind regards</p>

<p>Given your data:</p>

<blockquote>
<pre><code>df_energy2.head()

TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR
2016-01-01 00:00:00 116 HC 250 
2016-01-01 00:10:00 121 HC 250
</code></pre>
</blockquote>

<p>You have timestamp as the index. For extracting hours from timestamp where you have it as the index of the dataframe: </p>

<pre><code>  hours = df_energy2.index.hour
</code></pre>

<hr>

<p><strong>Edit</strong>: Yes, jezrael you're right. Putting what he has stated: pandas dataframe has a property for this i.e. <code>dt</code> :</p>

<pre><code>&lt;dataframe&gt;.&lt;ts_column&gt;.dt.hour
</code></pre>

<p>Example in your context - the column with date is <code>TIMESTAMP</code></p>

<pre><code>df.TIMESTAMP.dt.hour
</code></pre>

<hr>

<p>A similar question - <a href="http://stackoverflow.com/questions/21624217/pandas-dataframe-with-a-datetime64-column-querying-by-hour">Pandas, dataframe with a datetime64 column, querying by hour</a></p>

python datetime pandas extract hour