date_trunc in Python Pandas

Posted August 23, 2022 by Rohith ‐ 1 min read

date_trunc is the date function used to truncate a date or datetime value to the start of a given unit of duration. The function helps in truncating the date column to year, decade, century, quarter, month, week, day, hour, minute, second, or millisecond. In this article, we will truncate the date column using python pandas.


use dt.floor('<trunc-param>') or dt.to_period(<trunc-param>)

date_trunc In Pandas

for applying dt functions, we must make sure the column type is timestamp.

Convert column to datetime

We can convert the pandas column to datetime using, pd.to_datetime() function.


data['order_dt'] = pd.to_datetime(data['order_time'])

Apply Truncate Operation

Once we have pandas datetime column, we can apply truncate operation on datetime column using dt.floor('<trunc-param>') or dt.to_period(<trunc-param>)


data['order_hour'] = data['order_dt'].floor('h')

Truncate By Hour

Use h or H as param.


data['order_hour'] = data['order_dt'].floor('H')

Truncate By Month

Use M as param.


data['order_hour'] = data['order_dt'].floor('M')

Truncate By Minute

Use m as param.


data['order_hour'] = data['order_dt'].floor('m')
quick-references blog python pandas

Subscribe For More Content