📜  Python| Pandas TimedeltaIndex.ceil(1)

📅  最后修改于: 2023-12-03 14:46:23.074000             🧑  作者: Mango

Python | Pandas TimedeltaIndex.ceil

Pandas TimedeltaIndex.ceil is a method in pandas library of python, used to round each value in a TimedeltaIndex to the specified resolution after the decimal point.

The method ceil() helps to ceil the TimedeltaIndex to the specific resolution provided by the user. It does this by creating a new index and rounding the values of the original index to the nearest multiple of the specified resolution.

Syntax:

TimedeltaIndex.ceil(freq=None, ambiguous=’raise’). 

Parameters:

  • freq: This is an optional parameter, the resolution to round the data to. By default, the resolution is in nanoseconds.
  • ambiguous: This is also an optional parameter, it accepts only ‘raise’, ‘NaT’, or ‘infer’.

Return:

Pandas Series which can be further operated to get the desired output.

Example:

import pandas as pd

tdi = pd.TimedeltaIndex(['1 days 05:00:00', '2 days 13:00:00', '3 days 15:30:00'])

print(tdi.ceil('D'))

Output:

TimedeltaIndex(['2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)

In the above example, a TimedeltaIndex object called tdi is created, which contains time intervals of 1 day and 5 hours, 2 days and 13 hours, and 3 days and 15 hours.

Then, the ceil() method is used to round each of these time intervals up to the nearest day (which is specified as ‘D’ as a parameter to the ceil() method).

The output of this operation is a new TimedeltaIndex object, which contains the rounded time intervals.