Daniel Black Daniel Black - 6 months ago 137
Python Question

Pandas: Return Hour from Datetime Column Directly

Assume I have a DataFrame

of timestamp values:

timestamp sales_office
2014-01-01 09:01:00 Cincinnati
2014-01-01 09:11:00 San Francisco
2014-01-01 15:22:00 Chicago
2014-01-01 19:01:00 Chicago

I would like to create a new column
. I can create it by writing a short function as so and using
to apply it iteratively:

def hr_func(ts):
return ts.hour

sales['time_hour'] = sales['timestamp'].apply(hr_func)

I would then see this result:

timestamp sales_office time_hour
2014-01-01 09:01:00 Cincinnati 9
2014-01-01 09:11:00 San Francisco 9
2014-01-01 15:22:00 Chicago 15
2014-01-01 19:01:00 Chicago 19

What I'd like to achieve is some shorter transformation like this (which I know is erroneous but gets at the spirit):

sales['time_hour'] = sales['timestamp'].hour

Obviously the column is of type
and as such doesn't have those attributes, but it seems there's a simpler way to make use of matrix operations.

Is there a more-direct approach?


Assuming timestamp is the index of the dataframe, you can just do

    hours = sales.index.hour

If you want to add that to your sales dataframe, just do

    import pandas as pd
    pd.concat([sales, pd.DataFrame(hours, index=sales.index)], axis = 1)

Edit: If you have several columns of datetime objects, its the same process. If you have a column ['date'] in your dataframe, and assuming that 'date' has datetime values, you can access the hour from the 'date' as:

    hours = sales['date'].hour