Explore pandas.Series.dt.floor() method (4 examples)

Updated: February 19, 2024 By: Guest Contributor Post a comment


The pandas library in Python is a powerhouse for data manipulation and analysis. Specifically, when working with time series data, pandas offer a robust set of tools to make your analysis as straightforward as possible. One such tool is the dt.floor() method associated with Series objects that have datetime-like values. This method allows for the rounding down of datetime objects to a specified frequency, proving extremely useful in time series analysis. This tutorial explores the pandas.Series.dt.floor() method through four practical examples, demonstrating its utility and flexibility.

Understanding dt.floor() Method

Before diving into the examples, let’s have a quick overview of what dt.floor() actually does. When working with datetime data in pandas, it’s common to need to standardize the dates or times to a regular frequency—say, rounding down to the nearest hour, minute, or even second. The dt.floor() method accomplishes this by ‘flooring’ the datetime values—the equivalent of rounding down in numerical terms—to the specified ‘frequency’ level.

import pandas as pd
# Example datetime Series
dates = pd.date_range('2023-01-01', periods=4, freq='T')
series = pd.Series(dates)
# Applying dt.floor('H') to round down to nearest hour
floored = series.dt.floor('H')


0   2023-01-01
1   2023-01-01
2   2023-01-01
3   2023-01-01
dtype: datetime64[ns]

Example 1: Rounding Down to Hours

Let’s start with a simple example where datetime values are rounded down to the nearest hour. This can be particularly useful in scenarios where you want to group data by the hour, regardless of minutes and seconds.

import pandas as pd
# Sample data
dates = pd.date_range('2023-08-01 13:45', periods=3, freq='15T')
series = pd.Series(dates)
# Apply dt.floor('H')
result = series.dt.floor('H')
# Output


0   2023-08-01 13:00:00
1   2023-08-01 14:00:00
2   2023-08-01 14:00:00
dtype: datetime64[ns]

Example 2: Rounding Down to the Nearest Day

In many situations, especially when analyzing daily trends, you might want to normalize your time data to the start of each day. Here’s how you can employ dt.floor() to achieve this.

import pandas as pd
# Sample data for three different days varying in time
dates = pd.date_range('2023-09-15 18:30', periods=3, freq='6H')
series = pd.Series(dates)
# Apply dt.floor('D')
result = series.dt.floor('D')
# Output displays all times rounded down to the start of the day


0   2023-09-15
1   2023-09-16
2   2023-09-16
dtype: datetime64[ns]

Example 3: Customizing Frequencies

The power of dt.floor() extends beyond just hours and days. You can also round down to the nearest month, minute, or even second. Additionally, customizing the frequency to less common intervals like 15 minutes or 5 seconds is also possible. Here’s how:

import pandas as pd
# Sample data spaced 5 minutes apart
dates = pd.date_range('2023-02-02 14:52', periods=4, freq='5T')
series = pd.Series(dates)
# Apply dt.floor('15T') for 15-minute intervals
result = series.dt.floor('15T')
# Notice how the times are rounded to the nearest 15-minute mark


0   2023-02-02 14:45:00
1   2023-02-02 14:45:00
2   2023-02-02 15:00:00
3   2023-02-02 15:00:00
dtype: datetime64[ns]

Example 4: Handling Time Zones

Time zone management is a critical component of time series analysis. Thankfully, dt.floor() handles time zones gracefully. If your datetime Series has timezone-aware datetime objects, dt.floor() will respect these time zones when rounding. Here’s an example using Eastern Standard Time (EST).

import pandas as pd
# Make sure pandas is updated to handle time zones efficiently
pd.options.mode.chained_assignment = None  # default='warn'
# Time zone aware datetime range
utc_dates = pd.date_range('2023-03-10', periods=3, freq='4H', tz='UTC')
series = pd.Series(utc_dates).dt.tz_convert('US/Eastern')
# Apply dt.floor('H') to round down to the nearest hour, respecting EST
result = series.dt.floor('H')


0   2023-03-09 19:00:00-05:00
1   2023-03-09 23:00:00-05:00
2   2023-03-10 03:00:00-05:00
dtype: datetime64[ns, US/Eastern]


The pandas.Series.dt.floor() method is an indispensable tool in the data scientist’s toolkit for precise time series analysis. Through these four examples, it’s clear that whether you’re rounding times down to streamline data, aligning records to a standard frequency, dealing with custom intervals, or managing timezone-specific datasets, dt.floor() can significantly simplify these processes. By mastering this method, you can ensure that your time series data is as clean and organized as possible, ready for deeper analysis and insights.