HOWTO · Pandas

How to Extract Month and Year Separately From Datetime Column in Pandas

Extract numeric year and month values from a Pandas datetime column with the .dt accessor, or format them as strings with strftime().

On this page

Use a Series datetime accessor to add separate year and month columns: dates.dt.year and dates.dt.month. These properties return numeric components. If the source column contains date strings, convert it once with pandas.to_datetime before using .dt. Choose strftime() when the result should be text, such as a month formatted as 01.

Extract Month and Year with Series.dt.year and Series.dt.month

The .dt accessor exposes datetime properties on a Pandas Series. In particular, pandas.Series.dt.year and pandas.Series.dt.month return the year and month as numeric values; they are properties, so do not add parentheses to them. The Series year reference documents the year property.

Convert the input column with pandas.to_datetime when its values are strings. The example below starts with ISO date strings, converts the column, and assigns the two extracted components. The example converts the date strings, assigns the numeric year and month values, and prints the resulting DataFrame.

import pandas as pd

df = pd.DataFrame({"joined": ["2019-11-20", "2020-01-02", "2020-02-05"]})
df["joined"] = pd.to_datetime(df["joined"])
df["year"] = df["joined"].dt.year
df["month"] = df["joined"].dt.month
print(df)
      joined  year  month
0 2019-11-20  2019     11
1 2020-01-02  2020      1
2 2020-02-05  2020      2

If a column is already datetime typed, skip the conversion line and apply the two properties directly. Parsing depends on the values representing dates in a format Pandas can interpret; normalize or explicitly parse differently formatted source strings before extracting components. The month property is numeric, so January is represented by 1, which is useful for sorting or numeric comparisons.

Format Year and Month with strftime()

Use strftime() when downstream code needs formatted strings rather than numeric components. With the Series .dt accessor, %Y formats a four-digit year and %m formats a two-digit month. For the date 2020-01-02, those formats correspond to the strings 2020 and 01.

This version uses the same input conversion and adds text columns. The month string remains zero-padded, which is useful for labels or year-month keys. Because these values are strings, convert them to numbers if later calculations or numeric ordering are required.

import pandas as pd

df = pd.DataFrame({"joined": ["2019-11-20", "2020-01-02", "2020-02-05"]})
df["joined"] = pd.to_datetime(df["joined"])
df["year"] = df["joined"].dt.strftime("%Y")
df["month"] = df["joined"].dt.strftime("%m")
print(df)
      joined  year month
0 2019-11-20  2019    11
1 2020-01-02  2020    01
2 2020-02-05  2020    02

Use the numeric .dt.year and .dt.month properties when you want values for arithmetic or numeric comparisons. Use strftime() when the presentation format matters or when a fixed-width month such as 01 is needed. Both approaches operate on values that have datetime dtype.

Extract Month and Year with DatetimeIndex.year and DatetimeIndex.month

You can also wrap a datetime Series in pandas.DatetimeIndex, then read its year and month attributes. The DatetimeIndex reference describes this index type. These attributes give numeric components, like the Series accessor, and can be assigned to DataFrame columns in one step.

import pandas as pd

df = pd.DataFrame({"joined": ["2019-11-20", "2020-01-02", "2020-02-05"]})
df["joined"] = pd.to_datetime(df["joined"])
dates = pd.DatetimeIndex(df["joined"])
df["year"] = dates.year
df["month"] = dates.month
print(df)
      joined  year  month
0 2019-11-20  2019     11
1 2020-01-02  2020      1
2 2020-02-05  2020      2

Use DatetimeIndex when an index object fits the rest of your workflow; for a regular Series column, the .dt properties are usually the most direct expression. The two approaches produce numeric year and month values, while strftime() produces strings. In each case, make sure the source is datetime typed first. These examples use date-only strings; when timestamps include time zones, keep the intended timezone in mind because the calendar year and month depend on the timestamp’s local date.