In this tutorial we will learn how to cast datetime objects into months / years units. We’ll look into two use cases: date conversion in Python and in pandas DataFrames.
Casting datetime to months in Python
We’ll first create a Python date time object.
import datetime
my_dt = datetime.datetime(2023,11,30,20,45,55)
print(my_dt)
This will return the following:
2023-11-30 20:45:55
We can easily extract the month value:
my_dt.month
This will return the value 11.
A more interesting solution is to use the strftime formatter to extract the month, or month year:
print(my_dt.strftime('%m'))
# returns: 11
print(my_dt.strftime('%m-%Y'))
# returns: 11-2023
print(my_dt.strftime('%y-%m'))
# returns: 23-11
Convert datetime objects to months in pandas DataFrames
Create example data
We will start by importing the pandas library and create a DataFrame:
import pandas as pd
my_stamps = pd.date_range(start='1/28/2023', periods = 6, freq = 'B' )
num_interviews = [21, 20, 15, 16, 14, 22]
interviews = pd.DataFrame (dict (dates = my_stamps, num_interviews = num_interviews))
interviews.head()
This will return the following DataFrame rows:
dates | num_interviews | |
---|---|---|
0 | 2023-01-30 | 21 |
1 | 2023-01-31 | 20 |
2 | 2023-02-01 | 15 |
3 | 2023-02-02 | 16 |
4 | 2023-02-03 | 14 |
Convert dates to month name
We use the pd.Series dt accessor to invoke the month_name() method:
interviews ['dates'].dt.month_name()
This will return the following Series of objects:
0 January 1 January 2 February 3 February 4 February 5 February Name: dates, dtype: object
We can assign the Series to our dataframe:
interviews = interviews.assign (month_name = interviews ['dates'].dt.month_name())
Get the month – year value from datetime
IWe saw that we can use the strftime formatter to transform our dates to months and years. Here is an example of a different technique, that uses the to_period() method to cast datetimes to a specific period of times, in our case months.
interviews['month-year'] = interviews ['dates'].dt.to_period('m')
interviews['month-year'].head()
This will render the following Series:
0 2023-01 1 2023-01 2 2023-02 3 2023-02 4 2023-02 5 2023-02 Name: month_year, dtype: period[M]
For completeness, here’s code that helps achieve a similar Series, but made of string objects:
interviews['month_year'] = interviews ['dates'].dt.strftime('%Y-%m')
Group by months in pandas
If we would like to group our DataFrame rows by month, we can use the following snippet:
interviews.groupby(interviews ['dates'].dt.month_name())['num_interviews'].sum()
And this will return the following Series:
dates February 67 January 41 Name: num_interviews, dtype: int64
Follow up learning
How to turn a pandas DataFrame column to a date object with Python?