In this tutorial, we're going to extract hours and minutes in Pandas. We will extract information from time stored as string or datetime.
Here are several ways to extract hours and minutes in Pandas:
(1) Use accessor on datetime column
df['date'].dt.hour
(2) Apply lambda and datetime
df['date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M:%S %p').hour)
Setup
Let's work with the following DataFrame which has time information stored as a string:
import pandas as pd
dict = {'date': {0: '28-01-2022 5:25:00 PM',
1: '27-02-2022 6:25:00 PM',
2: '30-03-2022 7:25:00 PM',
3: '29-04-2022 8:25:00 PM',
4: '31-05-2022 9:25:00 PM'},
'date_short': {0: 'Jan-2022', 1: 'Feb-2022', 2: 'Mar-2022', 3: 'Apr-2022', 4: 'May-2022'}}
df = pd.DataFrame(dict)
data:
date | date_short | |
---|---|---|
0 | 28-01-2022 5:25:00 PM | Jan-2022 |
1 | 27-02-2022 6:25:00 PM | Feb-2022 |
2 | 30-03-2022 7:25:00 PM | Mar-2022 |
3 | 29-04-2022 8:25:00 PM | Apr-2022 |
4 | 31-05-2022 9:25:00 PM | May-2022 |
We can't extract hour and minutes reliably just by using string operations. So in the next steps we will see how to extract time info in a reliable way.
Step 1: Convert string to time
First we will convert the string column to datetime. To do so in Pandas we can use method to_datetime(df['date'])
:
pd.to_datetime(df['date'])
result:
0 2022-01-28 17:25:00
1 2022-02-27 18:25:00
2 2022-03-30 19:25:00
3 2022-04-29 20:25:00
4 2022-05-31 21:25:00
Name: date, dtype: datetime64[ns]
Step 2: Extract hours from datetime
To extract hours from datetime or timestamp in Pandas we can use accessors: .dt.hour
. So working with datetime column we get:
df['date'].dt.hour
Hours represented in 24 hour format:
0 17
1 18
2 19
3 20
4 21
Name: date, dtype: int64
Because we had initial information stored as PM
The image below shows the extract hours:
Step 3: Extract minutes from datetime
Extracting minutes in Pandas from datetime is available by accessor - .dt.minute
:
df['date'].dt.minute
We get minutes from the datetime:
0 25
1 25
2 25
3 25
4 25
Name: date, dtype: int64
Step 4: Extract seconds from datetime
There is one last option - for seconds - .dt.second
:
df['date'].dt.second
We extracted seconds for all rows:
0 0
1 0
2 0
3 0
4 0
Name: date, dtype: int64
Step 5: Extract hour in Pandas with lambda
We can use Python's datetime to extract hours directly from Pandas columns.
To extract hour or minutes we can:
- apply
lambda
- select date and time format:
from datetime import datetime
df['date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M:%S %p').hour)
Extract hours and minutes:
0 5
1 6
2 7
3 8
4 9
Name: date, dtype: int64
Refer to: Infer date format from string to find how to extract time format from string in Pandas
If you need to find more about datetime errors in Pandas check: How to Fix Pandas to_datetime: Wrong Date and Errors
Conclusion
In this article, we saw several ways to extract time information in Pandas. We saw how to extract hour and minute from datetime and timestamp in Pandas.