• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing Pandas Extract Year from Datetime

You can extract the year from the DateTime (date) column in pandas in several ways. In this article, I will explain how to get a year from the Datetime column using pandas.Series.dt.year, pandas.DatetimeIndex properties and strftime() functions.

If the data is not in Datetime type, you need to convert it first to Datetime by using the pd.to_datetime() method.

1. Quick Examples of Extract Year from Datetime

If you are in a hurry, below are some quick examples of how to extract the year from the pandas DataFrame DateTime column.


# Below are the quick examples of get the year from the datetime column

# Example 1: Use Datetime.strftime() Method to extract year
df['Year'] = df['InsertedDate'].dt.strftime('%Y')

# Example 2: Using pandas.Series.dt.year()
df['Year'] = df['InsertedDate'].dt.year  

# Example 3: Using pandas.DatetimeIndex() to extract year
df['year'] = pd.DatetimeIndex(df['InsertedDate']).year

# Example 4: Use datetime.to_period() method to extract year
df['Month_Year'] = df['InsertedDate'].dt.to_period('y')

# Example 5: Use DataFrame.apply() with lambda function and strftime()
df['Year'] = df['InsertedDate'].apply(lambda x: x.strftime('%Y')) 

# Example 6: Use Pandas.to_datetime() and datetime.strftime() method
df['yyyy'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%Y')

2. Pandas Extract Year using Datetime.strftime()

Let’s create a Pandas DataFrame with the column of Datetime in the form of year, month, and day and use Pandas attributes and functions to extract the year from a given datetime column.


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame({'InsertedDate': pd.to_datetime(Dates)},index=Courses)
print("Create DataFrame:\n", df)

Yields below output. This example extracts the year and add as a new column to DataFrame.

Yields below output.

Pandas extract year

strftime() method takes the datetime format and returns a string representing the specific format. You can use %Y as format code to extract the year from the DataFrame. Here, pd.to_datetime() is used to convert String to Datetime.


# Use Datetime.strftime() Method to extract year
df['Year'] = df['InsertedDate'].dt.strftime('%Y')
print("Get the year from the datetime column:\n", df)

Yields below output.

Pandas extract year

3. Extract Year Using Series.dt.year()

We can use pandas.Series.dt.year() to extract year but, this function returns a series object. Assign these to a column to get a DataFrame with year columns.


# Using pandas.Series.dt.year()
df['Year'] = df['InsertedDate'].dt.year 
print("Get the year from the datetime column:\n", df)

Yields below output.


# Output:
# Get the year from the datetime column:
        InsertedDate  Year
Spark     2018-08-14  2018
PySpark   2019-10-17  2019
Hadoop    2020-11-14  2020
Python    2020-05-17  2020
Pandas    2021-09-15  2021
Hadoop    2021-12-14  2021

4. Use Pandas DatetimeIndex() to Extract Year

We can also extract the year from the Pandas Datetime column, using the DatetimeIndex.year attribute. Note that this method takes a date as an argument.


# Using pandas.DatetimeIndex() to extract year
df['year'] = pd.DatetimeIndex(df['InsertedDate']).year
print("Get the year from the datetime column:\n", df)

Yields the same output as above.

5. Use Datetime.to_period() Method to Extract Year

You can also use df['Year']=df['InsertedDate'].dt.to_period('Y') method. The df['date_column'] has to be in datetime format.


# Use datetime.to_period() method to year
df['Year'] = df['InsertedDate'].dt.to_period('Y')
print("Get the year from the datetime column:\n", df)

Yields below output.


# Output:
# Get the year from the datetime column:
        InsertedDate  Year
Spark     2018-08-14  2018
PySpark   2019-10-17  2019
Hadoop    2020-11-14  2020
Python    2020-05-17  2020
Pandas    2021-09-15  2021
Hadoop    2021-12-14  2021

6. Use DataFrame.apply() With Lambda Function and strftime()

Let’s see how to get the year by using Pandas DataFrame.apply() and lambda function.


# Use DataFrame.apply() with lambda function and strftime()
df['Year'] = df['InsertedDate'].apply(lambda x: x.strftime('%Y')) 
print("Get the year from the datetime column:\n", df)

Yields below output.


# Output:
# Get the year from the datetime column:
        InsertedDate  Year
Spark     2018-08-14  2018
PySpark   2019-10-17  2019
Hadoop    2020-11-14  2020
Python    2020-05-17  2020
Pandas    2021-09-15  2021
Hadoop    2021-12-14  2021

7. Use Pandas.to_datetime() and datetime.strftime() Method

Use Pandas.to_datetime() and datetime.strftime() to get the year.


# Use Pandas.to_datetime() and datetime.strftime() method
df['yyyy'] = pd.to_datetime(df['InsertedDate']).dt.strftime('%Y')
print("Get the year from the datetime column:\n", df)

Yields below output.


# Output:
# Get the year from the datetime column:
        InsertedDate  yyyy
Spark     2018-08-14  2018
PySpark   2019-10-17  2019
Hadoop    2020-11-14  2020
Python    2020-05-17  2020
Pandas    2021-09-15  2021
Hadoop    2021-12-14  2021

Frequently Asked Questions on Pandas Extract Year from Datetime

How can I extract the year from a datetime column in a Pandas DataFrame?

You can use the dt attribute in Pandas to extract the year from a datetime column. For example, df['year'] = df['datetime_column'].dt.year

How can I extract the year directly without creating a new column?

you can extract the year without creating a new column by simply accessing the dt.year attribute. For example, df['year'] = pd.to_datetime(df['datetime_column']).dt.year

How can I extract the year from a datetime index in a DataFrame?

If your DataFrame has a datetime index, you can use the year attribute directly on the index. For example, df.set_index('timestamp', inplace=True)
df['year'] = df.index.year

8. Conclusion

In this article, you have learned how to extract the year from the Pandas DateTime column by using pandas.Series.dt.strftime(), pandas.DatetimeIndex(), datetime.to_period() and DataFrame.apply() methods with examples.

Happy Learning !!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.