How do you extract the month from a date using Pandas, and can you provide some examples?

How do you extract the month from a date using Pandas, and can you provide some examples?

To extract the month from a date using Pandas, first import the Pandas library. Then, use the “.dt.month” function to extract the month from the desired date column. This will return a numerical representation of the month (1 for January, 2 for February, etc.). Some examples of this function in action would be using it on a column of dates in a DataFrame to create a new column specifically for the month, or using it to filter the DataFrame by a specific month.

Extract Month from Date in Pandas (With Examples)


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 DataFrameprint(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 DataFrameprint(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.

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

Cite this article

stats writer (2024). How do you extract the month from a date using Pandas, and can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-extract-the-month-from-a-date-using-pandas-and-can-you-provide-some-examples/

stats writer. "How do you extract the month from a date using Pandas, and can you provide some examples?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-do-you-extract-the-month-from-a-date-using-pandas-and-can-you-provide-some-examples/.

stats writer. "How do you extract the month from a date using Pandas, and can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-extract-the-month-from-a-date-using-pandas-and-can-you-provide-some-examples/.

stats writer (2024) 'How do you extract the month from a date using Pandas, and can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-extract-the-month-from-a-date-using-pandas-and-can-you-provide-some-examples/.

[1] stats writer, "How do you extract the month from a date using Pandas, and can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How do you extract the month from a date using Pandas, and can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top