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
Python Pandas - Return a new Timedelta with daily floored resolution
To return a new Timedelta floored to this resolution, use the timedelta.floor() method. For daily floored resolution, set the freq parameter to the value 'D'.
Syntax
timedelta.floor(freq)
Parameters
freq ? The frequency string representing the resolution. Use 'D' for daily flooring.
Creating a Timedelta Object
At first, import the required libraries ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('5 days 10 min 25 s')
# Display the Timedelta
print("Original Timedelta...")
print(timedelta)
Original Timedelta... 5 days 00:10:25
Applying Daily Floor Resolution
Return the floored Timedelta with daily floored resolution ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('5 days 10 min 25 s')
# Display the original Timedelta
print("Original Timedelta...")
print(timedelta)
# Return the floored Timedelta with daily floored resolution
floored_timedelta = timedelta.floor(freq='D')
# Display the floored Timedelta
print("\nTimedelta (daily floored)...")
print(floored_timedelta)
Original Timedelta... 5 days 00:10:25 Timedelta (daily floored)... 5 days 00:00:00
How It Works
The floor() method truncates the time components smaller than the specified frequency. With freq='D' (daily), it removes hours, minutes, and seconds, keeping only complete days.
Example with Different Frequencies
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('3 days 14 hours 30 min 45 s')
print("Original Timedelta:")
print(timedelta)
# Floor to different frequencies
daily_floor = timedelta.floor(freq='D')
hourly_floor = timedelta.floor(freq='H')
print(f"\nDaily floor (D): {daily_floor}")
print(f"Hourly floor (H): {hourly_floor}")
Original Timedelta: 3 days 14:30:45 Daily floor (D): 3 days 00:00:00 Hourly floor (H): 3 days 14:00:00
Conclusion
Use timedelta.floor(freq='D') to truncate a Timedelta to daily resolution. This method removes all time components smaller than days, returning only complete days with zero hours, minutes, and seconds.
