Assume I have a DataFrame sales
of timestamp values:
timestamp sales_office
2014-01-01 09:01:00 Cincinnati
2014-01-01 09:11:00
Since the quickest, shortest answer is in a comment (from Jeff) and has a typo, here it is corrected and in full:
sales['time_hour'] = pd.DatetimeIndex(sales['timestamp']).hour
You can try this:
sales['time_hour'] = pd.to_datetime(sales['timestamp']).dt.hour
For posterity: as of 0.15.0, there is a handy .dt accessor you can use to pull such values from a datetime/period series (in the above case, just sales.timestamp.dt.hour
!
You can use a lambda expression, e.g:
sales['time_hour'] = sales.timestamp.apply(lambda x: x.hour)
Now we can use:
sales['time_hour'] = sales['timestamp'].apply(lambda x: x.hour)
Here is a simple solution:
import pandas as pd
# convert the timestamp column to datetime
df['timestamp'] = pd.to_datetime(df['timestamp'])
# extract hour from the timestamp column to create an time_hour column
df['time_hour'] = df['timestamp'].dt.hour