> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Extract Hour and Minute in  Pandas
- URL: https://datascientyst.com/how-to-extract-hour-and-minute-in-pandas/
- Published: 2022-10-05T07:31:25.000Z
- Updated: 2022-10-05T07:31:25.000Z
- Author: John D K
- Tags: Time Series

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**

```python
df['date'].dt.hour

```

**(2) Apply lambda and datetime**

```python
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:

```python
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'])`:

```python
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:

```python
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:

![](https://datascientyst.com/content/images/2022/10/how-to-extract-hour-and-minute-in-pandas.png)

## Step 3: Extract minutes from datetime

Extracting minutes in Pandas from datetime is available by accessor - `.dt.minute`:

```python
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`:

```python
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:

```python
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](https://datascientyst.com/convert-string-to-datetime-pandas/#step-4-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](https://datascientyst.com/how-to-fix-pandas-to%5Fdatetime-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.**