> ## 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 Month and Year from DateTime column in Pandas
- URL: https://datascientyst.com/extract-month-and-year-datetime-column-in-pandas/
- Published: 2021-08-11T21:26:30.000Z
- Updated: 2022-10-28T06:20:48.000Z
- Author: John D K
- Tags: Time Series

In this short guide, I'll show you how to extract Month and Year from a DateTime column in Pandas DataFrame. You can also find how to convert string data to a DateTime. So at the end you will get:

`01/08/2021` \-> `2021-08`  
`DD/MM/YYYY` \-> `YYYY-MM`

or any other date format. We will also cover `MM/YYYY`.

To start, here is the syntax that you may apply in order extract concatenation of year and month:

```python
.dt.to_period('M')

```

In the next section, I'll review the steps to apply the above syntax in practice.

## Step 1: Create a DataFrame with Datetime values

Lets create a DataFrame which has a single column StartDate:

```python
dates = ['2021-08-01', '2021-08-02', '2021-08-03']
df = pd.DataFrame({'StartDate': dates})

```

result:

| StartDate  |
| ---------- |
| 2021-08-01 |
| 2021-08-02 |
| 2021-08-03 |

In order to convert string to Datetime column we are going to use:

```python
df['StartDate'] = pd.to_datetime(df['StartDate'])

```

## Step 2: Extract Year and Month with .dt.to\_period('M') - format YYYY-MM

In order to extract from a full date only the year plus the month: 2021-08-01 -> 2021-08 we need just this line:

```python
df['StartDate'].dt.to_period('M')

```

result:

```
0    2021-08
1    2021-08
2    2021-08

```

## Step 3: Extract Year and Month other formats MM/YYYY

What if you like to get the month first and then the year? In this case we will use `.dt.strftime` in order to produce a column with format: `MM/YYYY` or any other format.

```python
df['StartDate'].dt.strftime('%m/%Y')

```

```
0    08/2021
1    08/2021
2    08/2021

```

Note: have in mind that this solution might be really slow in case of a huge DataFrame.

## Step 4: Extracting Year and Month separately and combine them

A bit faster solution than step 3 plus a trace of the month and year info will be:

- extract month and date to separate columns
- combine both columns into a single one

```python
df['yyyy'] = pd.to_datetime(df['StartDate']).dt.year
df['mm'] = pd.to_datetime(df['StartDate']).dt.month

```

| StartDate  | yyyy | mm |
| ---------- | ---- | -- |
| 2021-08-01 | 2021 | 8  |
| 2021-08-02 | 2021 | 8  |
| 2021-08-03 | 2021 | 8  |

and then:

```python
df['yyyy'].astype(str) + '-'+ df['mm'].astype(str)

```

Note: If you don't need extra columns you can just do:

```python
df['StartDate'].dt.year.astype(str) + "-" + df['StartDate'].dt.month.astype(str)

```

Notebook with all examples: [Extract Month and Year from DateTime column](https://github.com/softhints/datascientyst/blob/master/datetime/1.extract-month-and-year-datetime-column-in-pandas.ipynb?ref=datascientyst.com)

![extract-month-year-datetime-column-pandas](https://datascientyst.com/content/images/2022/10/extract-month-year-datetime-column-pandas.webp)