Pandas Extract Month and Year from Datetime

  • Post author:
  • Post category:Pandas
  • Post last modified:November 15, 2023

You can extract month and year from the DateTime (date) column in pandas in several ways. In this article, I will explain how to get a year and a month from the Datetime column using pandas.Series.dt.year and pandas.Series.dt.month attributes respectively.

If the data is not in Datetime type, you need to convert it first to Datetime by using the pd.to_datetime() method. Also, I will cover extracting the year and month from the DataFrame column using pandas.DatetimeIndex property and strftime() method.

1. Quick Examples of Extracting Month and Year from Datetime

If you are in a hurry, below are some quick examples of how to extract month and year separately from the pandas DataFrame DateTime column.


# Below are some quick examples

# Example 1: Use Datetime.strftime() Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime('%Y')
df['Month'] = df['InsertedDate'].dt.strftime('%m')

# Example 2: Using pandas.Series.dt.year() & pandas.Series.dt.month() method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 

# Example 3: Using pandas.DatetimeIndex() to extract month and year
df['year'] = pd.DatetimeIndex(df['InsertedDate']).year
df['month'] = pd.DatetimeIndex(df['InsertedDate']).month

# Example 4: Use datetime.to_period() method to extract month and year
df['Month_Year'] = df['InsertedDate'].dt.to_period('M')

# Example 5: Use DataFrame.apply() with lambda function and strftime()
df['Month_Year'] = df['InsertedDate'].apply(lambda x: x.strftime('%B-%Y')) 

# Example 6: Use Pandas.to_datetime() and datetime.strftime() method
df['yyyy-mm'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%Y-%m')

2. Pandas Extract Month and Year using Datetime.strftime()

Let’s create a Pandas DataFrame with the column of Datetime values and use the pd.to_datetime() function to convert this column into a datetime column and finally, use the strftime() method to extract the month and year from a datetime column.


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame({'InsertedDate': pd.to_datetime(Dates)},index=Courses)
print("DataFrame:\n", df)

Yields below output.

pandas datetime month year

strftime() method takes a datetime format and returns a string representing the specific format. You can use %Y and %m as format codes to extract year and month respectively from the pandas DataFrame.


# Use Datetime.strftime() Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime('%Y')
df['Month'] = df['InsertedDate'].dt.strftime('%m')
print("Get month and year from datetime column:\n", df)

Yields below output.

pandas datetime month year

3. Extract Month and Year Using Pandas.Series.dt.year() & month()

You can also use pandas.Series.dt.year() and pandas.Series.dt.month() attributes to get year and month but, these return a series object. Assign these to a column to get a DataFrame with year and month columns.


# Using pandas.Series.dt.year() & pandas.Series.dt.month() method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 
print("Get month and year from datetime column:\n", df)

Yields below output.


# Output:
# Get month and year from datetime column:
        InsertedDate  Year  Month
Spark     2018-08-14  2018      8
PySpark   2019-10-17  2019     10
Hadoop    2020-11-14  2020     11
Python    2020-05-17  2020      5
Pandas    2021-09-15  2021      9
Hadoop    2021-12-14  2021     12

4. Use pandas DatetimeIndex() to Extract Month and Year

Also, to extract the month and year from the pandas Datetime column, use DatetimeIndex.month attribute to find the month and use DatetimeIndex.year attribute to find the year present in the date. Note that this method takes a date as an argument.


# Using pandas.DatetimeIndex() to extract month and year
df['year'] = pd.DatetimeIndex(df['InsertedDate']).year
df['month'] = pd.DatetimeIndex(df['InsertedDate']).month
print("Get month and year from datetime column:\n", df)

Yields the same output as above.

5. Use Datetime.to_period() Method to Extract Month and Year

You can also use df['Month_Year']=df['InsertedDate'].dt.to_period('M') method. The df['date_column'] has to be in datetime format.


# Use datetime.to_period() method to extract month and year
df['Month_Year'] = df['InsertedDate'].dt.to_period('M')
print("Get month and year from datetime column:\n", df)

Yields below output.


# Output:
# Get month and year from datetime column:
        InsertedDate   Month_Year
Spark     2018-08-14    2018-08
PySpark   2019-10-17    2019-10
Hadoop    2020-11-14    2020-11
Python    2020-05-17    2020-05
Pandas    2021-09-15    2021-09
Hadoop    2021-12-14    2021-12

6. Use DataFrame.apply() With Lambda Function and strftime()

Let’s see how to get the month and year by using Pandas DataFrame.apply() and lambda function.


# Use DataFrame.apply() with lambda function and strftime()
df['Month_Year'] = df['InsertedDate'].apply(lambda x: x.strftime('%B-%Y')) 
print("Get month and year from datetime column:\n", df)

Yields below output.


# Output:
# Get month and year from datetime column:
      InsertedDate      Month_Year
Spark     2018-08-14     August-2018
PySpark   2019-10-17    October-2019
Hadoop    2020-11-14   November-2020
Python    2020-05-17        May-2020
Pandas    2021-09-15  September-2021
Hadoop    2021-12-14   December-2021

7. Use Pandas.to_datetime() and datetime.strftime() Method

To add a column with ‘year-month’ pairs.


# Use Pandas.to_datetime() and datetime.strftime() method
df['yyyy-mm'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%Y-%m')
print("Get month and year from datetime column:\n", df) 

Yields below output.


# Output:
# Get month and year from datetime column: 
        InsertedDate  yyyy-mm
Spark     2018-08-14  2018-08
PySpark   2019-10-17  2019-10
Hadoop    2020-11-14  2020-11
Python    2020-05-17  2020-05
Pandas    2021-09-15  2021-09
Hadoop    2021-12-14  2021-12

8. Complete the Example to Get Month and Year from Panda Datetime


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame({'InsertedDate': pd.to_datetime(Dates)},index=Courses)

# Use Datetime.strftime() Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime('%Y')
df['Month'] = df['InsertedDate'].dt.strftime('%m')
print(df)

# Using pandas.Series.dt.year() & pandas.Series.dt.month() method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 
print(df)

# Using pandas.DatetimeIndex() to extract month and year
df['year'] = pd.DatetimeIndex(df['InsertedDate']).year
df['month'] = pd.DatetimeIndex(df['InsertedDate']).month
print(df)

# Use datetime.to_period() method to extract month and year
df['Month_Year'] = df['InsertedDate'].dt.to_period('M')
print(df)

# Use DataFrame.apply() with lambda function and strftime()
df['Month_Year'] = df['InsertedDate'].apply(lambda x: x.strftime('%B-%Y')) 
print(df)

# Use Pandas.to_datetime() and datetime.strftime() method
df['yyyy-mm'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%Y-%m')
print(df)

# Use datetime.strftime() method
df['yyyy'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%Y')
df['mm'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%m')
print(df)

Frequently Asked Questions on Get Month and Year from Panda Datetime

How do I convert a column to a datetime type in Pandas?

You can use the pd.to_datetime() function to convert a column to a datetime type. For example, df['datetime_column'] = pd.to_datetime(df['datetime_column'])

How can I extract the month and year from a Pandas datetime column?

You can use the dt.month and dt.year attributes to get the month and year from the datetime column For example, df['month'] = df['datetime_column'].dt.month and df['year'] = df['datetime_column'].dt.year

How can I extract the day or other components from a datetime column?

You can extract various components like day, hour, minute, etc., using the dt attribute. For example, df['datetime_column'].dt.day for a day and df['datetime_column'].dt.hour for an hour.

How do I handle missing or null values in the datetime column?

You can use the pd.to_datetime() function with the errors='coerce' parameter to handle missing or invalid values by converting them to NaT (Not a Time). For example, df['datetime_column'] = pd.to_datetime(df['datetime_column'], errors='coerce')

How can I filter the DataFrame based on a specific month or year?

You can filter the DataFrame based on a specific month or year. For example, to filter for January. For example, january_data = df[df['datetime_column'].dt.month == 1]

Conclusion

In this article, you have learned how to extract month and year separately from the Pandas DateTime column by using pandas.Series.dt.strftime(), pandas.DatetimeIndex(), datetime.to_period(), and DataFrame.apply() methods with examples.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply

This Post Has One Comment

  1. Sait

    Really good!

You are currently viewing Pandas Extract Month and Year from Datetime