2.4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Extract Month from Date in Pandas (With Examples)

How to Extract Month from Date in Pandas (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to extract the month from a date in pandas:

df['month'] = pd.DatetimeIndex(df['date_column']).month

The following example shows how to use this function in practice.

Example: Extract Month from Date in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'sales_date': ['2020-01-18', '2020-02-20', '2020-03-21'],
                   'total_sales': [675, 500, 575]})

#view DataFrame
print(df)

   sales_date  total_sales
0  2020-01-18          675
1  2020-02-20          500
2  2020-03-21          575

We can use the following syntax to create a new column that contains the month of the ‘sales_date’ column:

#extract month as new column
df['month'] = pd.DatetimeIndex(df['sales_date']).month

#view updated DataFrame
print(df)

	sales_date	total_sales	month
0	2020-01-18	675	        1
1	2020-02-20	500	        2
2	2020-03-21	575	        3

We can also use the following syntax to create a new column that contains the year of the ‘sales_date’ column:

#extract year as new column
df['year'] = pd.DatetimeIndex(df['sales_date']).year

#view updated DataFrame
print(df)

        sales_date	total_sales	month	year
0	2020-01-18	675	        1	2020
1	2020-02-20	500	        2	2020
2	2020-03-21	575	        3	2020

Note that if there are any NaN values in the DataFrame, this function will automatically produce NaN values for the corresponding values in the new month and year columns.

Related: How to Sort a Pandas DataFrame by Date

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Pandas: How to Count Occurrences of Specific Value in Column
Pandas: Get Index of Rows Whose Column Matches Value
Pandas: How to Count Missing Values in DataFrame

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories