Pandas: Return Hour from Datetime Column Directly

PythonDatetimePandas

Python Problem Overview


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     San Francisco
2014-01-01 15:22:00     Chicago
2014-01-01 19:01:00     Chicago

I would like to create a new column time_hour. I can create it by writing a short function as so and using apply() 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 Series 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?

Python Solutions


Solution 1 - Python

Assuming timestamp is the index of the data frame, you can just do the following:

hours = sales.index.hour

If you want to add that to your sales data frame, 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, it's the same process. If you have a column ['date'] in your data frame, and assuming that 'date' has datetime values, you can access the hour from the 'date' as:

hours = sales['date'].hour

Edit2: If you want to adjust a column in your data frame you have to include dt:

sales['datehour'] = sales['date'].dt.hour

Solution 2 - Python

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!

Solution 3 - Python

You can use a lambda expression, e.g:

sales['time_hour'] = sales.timestamp.apply(lambda x: x.hour)

Solution 4 - Python

You can try this:

sales['time_hour'] = pd.to_datetime(sales['timestamp']).dt.hour

Solution 5 - Python

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

Solution 6 - Python

Now we can use:

sales['time_hour'] = sales['timestamp'].apply(lambda x: x.hour)

Solution 7 - Python

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

Solution 8 - Python

You could also create a function where, if needed, you could also extract month, year, etc. but 'timestamp' must be the index.

for i in range(len(sales)):
  position = sales.index[i]
  hour = position.hour
  month = position.month
  sales.loc[position, 'hour'] = hour
  sales.loc[position, 'month'] = month

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel BlackView Question on Stackoverflow
Solution 1 - PythonSudipta BasakView Answer on Stackoverflow
Solution 2 - Pythoniff_orView Answer on Stackoverflow
Solution 3 - PythonBob HannonView Answer on Stackoverflow
Solution 4 - PythonTCOView Answer on Stackoverflow
Solution 5 - PythonIdiot TomView Answer on Stackoverflow
Solution 6 - PythonVICTOR ACOSTAView Answer on Stackoverflow
Solution 7 - PythonDINA TAKLITView Answer on Stackoverflow
Solution 8 - PythonTrrmisView Answer on Stackoverflow