+

Python | Pandas Timedelta.floor ()

Timedelta is a subclass of datetime.timedelta and behaves in a similar manner. It is the panda equivalent of datetime.timedelta and is interchangeable with it in most cases. Timedelta.floor() in pandas.Timedelta is used to return a new Timedelta with this resolution.

Syntax: Timedelta.floor ()

Parameters:
freq: a freq string indicating the flooring resolution

Returns: new floored Timedelta.

Code # 1:

# import pandas as pd

import pandas as pd 

import datetime

 
# Create a Timedelta object

td = pd.Timedelta ( 5.05 , unit = ` s` )

 
# Print Timedelta object

 

print (td.floor ( `S` ))

Exit :

 0 days 00:00:05 

Code # 2:

# import pandas as pd

import pandas as pd 

import datetime

 
# Create Timedelta object

 td = pd.Timedelta ( 13.25 , unit = `h` )

 
# Print the Timedelta object

 

print (td.floor ( `H` ))

Exit :

 0 days 13:00:00 

Code # 3:

# import pandas as pd

import pandas as pd 

from datetime import datetime

 
# Create a Timedelta object

td = pd.Timedelta ( ` 7 days 15 hours` )

 
# Print the Timedelta object

 

print (td.floor ( `D` ))

Exit :

 7 days 00:00:00 
Get Solution for free from DataCamp guru