Extracting day, month, and year from Datetime in Pandas: A practical guide

Python Pandas @ Freshers.in

Working with datetime data is a common requirement in data analysis. In Pandas, Python’s powerful data manipulation library, extracting specific components like day, month, and year from datetime objects is a frequent task. This article provides a comprehensive guide on how to perform this extraction effectively. Datetime in Pandas is a versatile data type that represents dates and times in a human-readable format. Pandas provides robust tools for working with datetime objects, making it easier to conduct time-based analysis and manipulations.

Creating a Datetime series

For demonstration, let’s create a Pandas Series with datetime objects:

import pandas as pd
# Sample datetime data
datetime_data = pd.Series(pd.date_range('2023-01-01', periods=6, freq='M'))
print(datetime_data)
0   2023-01-31
1   2023-02-28
2   2023-03-31
3   2023-04-30
4   2023-05-31
5   2023-06-30
dtype: datetime64[ns]

Extracting components

Pandas simplifies the extraction of day, month, and year through its dt accessor.

Extracting Day

days = datetime_data.dt.day
print("Days:", days)
Days: 0    31
1    28
2    31
3    30
4    31
5    30
dtype: int64

Extracting Month

months = datetime_data.dt.month
print("Months:", months)
Months: 0    1
1    2
2    3
3    4
4    5
5    6
dtype: int64

Extracting Year

years = datetime_data.dt.year
print("Years:", years)
Years: 0    2023
1    2023
2    2023
3    2023
4    2023
5    2023
dtype: int64

Example

For a more customized example, let’s consider a DataFrame containing various dates:

data = {'Dates': ['2023-03-15', '2023-07-21', '2023-11-05']}
df = pd.DataFrame(data)
df['Dates'] = pd.to_datetime(df['Dates'])
# Extracting day, month, and year
df['Day'] = df['Dates'].dt.day
df['Month'] = df['Dates'].dt.month
df['Year'] = df['Dates'].dt.year
print(df)
Output
       Dates  Day  Month  Year
0 2023-03-15   15      3  2023
1 2023-07-21   21      7  2023
2 2023-11-05    5     11  2023

Applications and benefits

Extracting day, month, and year from datetime objects in Pandas is particularly useful for:

  • Time series analysis.
  • Grouping data based on time components.
  • Simplifying date-related calculations.
Author: user