Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Return a new Timedelta with minutely floored resolution
To return a new Timedelta floored to this resolution, use the timedelta.floor() method. For minutely floored resolution, set the freq parameter to 'T'.
Syntax
timedelta.floor(freq)
Parameters
freq: String representing the frequency. Use 'T' or 'min' for minutes.
Creating a Timedelta Object
First, import pandas and create a Timedelta object ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('8 days 11 hours 39 min 18 s')
print("Original Timedelta:")
print(timedelta)
Original Timedelta: 8 days 11:39:18
Applying Minutely Floor Resolution
Use the floor() method with freq='T' to floor to the nearest minute ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('8 days 11 hours 39 min 18 s')
print("Original Timedelta:")
print(timedelta)
# Apply minutely floor resolution
floored_timedelta = timedelta.floor(freq='T')
print("\nTimedelta (minutely floored):")
print(floored_timedelta)
Original Timedelta: 8 days 11:39:18 Timedelta (minutely floored): 8 days 11:39:00
Multiple Examples with Different Frequencies
Compare different floor frequencies to understand the behavior ?
import pandas as pd
# Create a Timedelta with seconds
td = pd.Timedelta('2 days 5 hours 23 min 45 s')
print("Original:", td)
# Different floor frequencies
print("Minutely floor (T):", td.floor('T'))
print("Hourly floor (H):", td.floor('H'))
print("Daily floor (D):", td.floor('D'))
Original: 2 days 05:23:45 Minutely floor (T): 2 days 05:23:00 Hourly floor (H): 2 days 05:00:00 Daily floor (D): 2 days 00:00:00
Key Points
- The
floor()method rounds down to the specified frequency - Use
'T'or'min'for minute-level flooring - Seconds and microseconds are removed when flooring to minutes
- The original Timedelta object remains unchanged
Conclusion
The floor() method with freq='T' efficiently floors Timedelta objects to minute precision. This is useful for time-based data analysis where you need consistent minute-level granularity.
Advertisements
