Extract year from DateTime in Pandas

Extract year from DateTime in Pandas

Being able to extract the year from a DateTime object in Pandas can be extremely helpful for feature engineering. In this post, I will walk through how to do this simply in multiple variations.

Stephen Allwright
Stephen Allwright

Being able to extract the year from a DateTime object in Pandas can be extremely helpful for feature engineering. In this post, I will walk through how to do this simply in multiple variations.

How to get the year from a DateTime object in Pandas

There are several methods of getting the year from a DateTime in Pandas, which you choose will depend upon the format that you wish to return in the year in. The simplest, and also most common, way of doing this is to use .dt.year on the DateTime object which returns the year as an integer.

get year from pandas datetime column

Extract year from DateTime in Pandas

Let’s look at an example of using the most common method of year extraction in Pandas, which is to use .dt.year. This will return the year as an integer.

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("31/12/2022 09:00:00", periods=4, freq="Y"))

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

print(df)
print(df.dtypes)

"""
Output:

datetime                year
0 2022-12-31 09:00:00   2022
1 2023-12-31 09:00:00   2023
2 2024-12-31 09:00:00   2024
3 2025-12-31 09:00:00   2025

datetime    datetime64[ns]
year                 int64
dtype: object
"""

Extract year as a string from DateTime in Pandas

It's also possible to return the year of a DateTime as a string, instead of an integer. This is done using the strftime method in Pandas.

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("31/12/2022 09:00:00", periods=4, freq="Y"))

df["year"] = df["datetime"].dt.strftime('%Y')

print(df)
print(df.dtypes)

"""
Output:

datetime                year
0 2022-12-31 09:00:00   2022
1 2023-12-31 09:00:00   2023
2 2024-12-31 09:00:00   2024
3 2025-12-31 09:00:00   2025

datetime    datetime64[ns]
year                object
dtype: object
"""

Get first date of year from DateTime in Pandas

Instead of returning the year from a DateTime, you may way to round the dates to the first date of their respective years. This is possible using the tseries.offsets method from Pandas.

import pandas as pd
from pandas.tseries import offsets

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("31/12/2022 09:00:00", periods=4, freq="Y"))

df["year_start"] = df["datetime"] - offsets.YearBegin()

print(df)
print(df.dtypes)

"""
Output:

datetime               year_start
0 2022-12-31 09:00:00  2022-01-01 09:00:00
1 2023-12-31 09:00:00  2023-01-01 09:00:00
2 2024-12-31 09:00:00  2024-01-01 09:00:00
3 2025-12-31 09:00:00. 2025-01-01 09:00:00

datetime      datetime64[ns]
year_start    datetime64[ns]
dtype: object
"""

Get last date of year from DateTime in Pandas

Likewise, if you wanted to get the last date of the year from a DateTime in Pandas then you can use the same tseries.offsets method, just using the year-end instead of the beginning.

import pandas as pd
from pandas.tseries import offsets

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("31/12/2022 09:00:00", periods=4, freq="Y"))

df["year_end"] = df["datetime"] - offsets.YearEnd()

print(df)
print(df.dtypes)

"""
Output

datetime               year_end
0 2022-12-31 09:00:00  2021-12-31 09:00:00
1 2023-12-31 09:00:00  2022-12-31 09:00:00
2 2024-12-31 09:00:00  2023-12-31 09:00:00
3 2025-12-31 09:00:00  2024-12-31 09:00:00

datetime    datetime64[ns]
year_end    datetime64[ns]
dtype: object
"""

Get year from DateTime in Python
Extract week from date in Pandas
Extract month from date in Pandas
Extract day from date in Pandas

References

Pandas year documentation

Pandas

Stephen Allwright Twitter

I'm a Data Scientist currently working for Oda, an online grocery retailer, in Oslo, Norway. These posts are my way of sharing some of the tips and tricks I've picked up along the way.