To extract the year from a datetime column in pandas, you can access the “year” property. The “year” property returns a 64-bit integer.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df["year"] = df["date"].dt.year

print(df)

#Output:
        date  sales  year
0 2021-09-30    100  2021
1 2021-12-31     30  2021
3 2022-03-31     50  2022
4 2022-06-30     60  2022
5 2022-09-30     10  2022
6 2022-12-31     80  2022

When working with data which contains information over time, the ability to extract certain pieces of information from our data easily is valuable.

One such piece of information is getting the year from a date when using pandas in Python.

To get the year from a datetime column in pandas, you can access the pandas datetime “year” property. The “year” property returns a 64-bit integer.

Below is a simple example which shows you how to get the year from a datetime variable in a pandas DataFrame.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df["year"] = df["date"].dt.year

print(df)

#Output:
        date  sales  year
0 2021-09-30    100  2021
1 2021-12-31     30  2021
3 2022-03-31     50  2022
4 2022-06-30     60  2022
5 2022-09-30     10  2022
6 2022-12-31     80  2022

Hopefully this article has been useful for you to learn how to get the year from a date variable in a pandas DataFrame in Python.

Categorized in:

Python,

Last Update: March 1, 2024