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