pandas.Timedelta#

class pandas.Timedelta(value=<object object>, unit=None, **kwargs)#

Represents a duration, the difference between two dates or times.

Timedelta is the pandas equivalent of python’s datetime.timedelta and is interchangeable with it in most cases.

Parameters
valueTimedelta, timedelta, np.timedelta64, str, or int
unitstr, default ‘ns’

Denote the unit of the input, if input is an integer.

Possible values:

  • ‘W’, ‘D’, ‘T’, ‘S’, ‘L’, ‘U’, or ‘N’

  • ‘days’ or ‘day’

  • ‘hours’, ‘hour’, ‘hr’, or ‘h’

  • ‘minutes’, ‘minute’, ‘min’, or ‘m’

  • ‘seconds’, ‘second’, or ‘sec’

  • ‘milliseconds’, ‘millisecond’, ‘millis’, or ‘milli’

  • ‘microseconds’, ‘microsecond’, ‘micros’, or ‘micro’

  • ‘nanoseconds’, ‘nanosecond’, ‘nanos’, ‘nano’, or ‘ns’.

**kwargs

Available kwargs: {days, seconds, microseconds, milliseconds, minutes, hours, weeks}. Values for construction in compat with datetime.timedelta. Numpy ints and floats will be coerced to python ints and floats.

Notes

The constructor may take in either both values of value and unit or kwargs as above. Either one of them must be used during initialization

The .value attribute is always in ns.

If the precision is higher than nanoseconds, the precision of the duration is truncated to nanoseconds.

Examples

Here we initialize Timedelta object with both value and unit

>>> td = pd.Timedelta(1, "d")
>>> td
Timedelta('1 days 00:00:00')

Here we initialize the Timedelta object with kwargs

>>> td2 = pd.Timedelta(days=1)
>>> td2
Timedelta('1 days 00:00:00')

We see that either way we get the same result

Attributes

asm8

Return a numpy timedelta64 array scalar view.

components

Return a components namedtuple-like.

days

Number of days.

delta

Return the timedelta in nanoseconds (ns), for internal compatibility.

microseconds

Number of microseconds (>= 0 and less than 1 second).

nanoseconds

Return the number of nanoseconds (n), where 0 <= n < 1 microsecond.

resolution_string

Return a string representing the lowest timedelta resolution.

seconds

Number of seconds (>= 0 and less than 1 day).

freq

is_populated

value

Methods

ceil(freq)

Return a new Timedelta ceiled to this resolution.

floor(freq)

Return a new Timedelta floored to this resolution.

isoformat

Format Timedelta as ISO 8601 Duration like P[n]Y[n]M[n]DT[n]H[n]M[n]S, where the [n] s are replaced by the values.

round(freq)

Round the Timedelta to the specified resolution.

to_numpy

Convert the Timedelta to a NumPy timedelta64.

to_pytimedelta

Convert a pandas Timedelta object into a python datetime.timedelta object.

to_timedelta64

Return a numpy.timedelta64 object with 'ns' precision.

total_seconds

Total seconds in the duration.

view

Array view compatibility.