dt.hour is the inbuilt method to get hour from timestamp (date) in Pandas Python. Let’s see how to
- Get the hour from timestamp (date) in pandas python
First lets create the dataframe
import pandas as pd import numpy as np import datetime date1 = pd.Series(pd.date_range('2012-1-1 11:20:00', periods=7, freq='H')) df = pd.DataFrame(dict(date_given=date1)) print(df)
so the resultant dataframe will be
hour function gets hour value of the timestamp
df['hour_of_timestamp'] = df['date_given'].dt.hour print(df)
so the resultant dataframe will be