Time deltas

Timedeltas are differences in times, expressed in difference units, e.g. days, hours, minutes,seconds. They can be both positive and negative.

Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner,but allows compatibility with np.timedelta64 types as well as a host of custom representation,parsing, and attributes.

Parsing

You can construct a Timedelta scalar through various arguments:

  1. In [1]: import datetime
  2.  
  3. # strings
  4. In [2]: pd.Timedelta('1 days')
  5. Out[2]: Timedelta('1 days 00:00:00')
  6.  
  7. In [3]: pd.Timedelta('1 days 00:00:00')
  8. Out[3]: Timedelta('1 days 00:00:00')
  9.  
  10. In [4]: pd.Timedelta('1 days 2 hours')
  11. Out[4]: Timedelta('1 days 02:00:00')
  12.  
  13. In [5]: pd.Timedelta('-1 days 2 min 3us')
  14. Out[5]: Timedelta('-2 days +23:57:59.999997')
  15.  
  16. # like datetime.timedelta
  17. # note: these MUST be specified as keyword arguments
  18. In [6]: pd.Timedelta(days=1, seconds=1)
  19. Out[6]: Timedelta('1 days 00:00:01')
  20.  
  21. # integers with a unit
  22. In [7]: pd.Timedelta(1, unit='d')
  23. Out[7]: Timedelta('1 days 00:00:00')
  24.  
  25. # from a datetime.timedelta/np.timedelta64
  26. In [8]: pd.Timedelta(datetime.timedelta(days=1, seconds=1))
  27. Out[8]: Timedelta('1 days 00:00:01')
  28.  
  29. In [9]: pd.Timedelta(np.timedelta64(1, 'ms'))
  30. Out[9]: Timedelta('0 days 00:00:00.001000')
  31.  
  32. # negative Timedeltas have this string repr
  33. # to be more consistent with datetime.timedelta conventions
  34. In [10]: pd.Timedelta('-1us')
  35. Out[10]: Timedelta('-1 days +23:59:59.999999')
  36.  
  37. # a NaT
  38. In [11]: pd.Timedelta('nan')
  39. Out[11]: NaT
  40.  
  41. In [12]: pd.Timedelta('nat')
  42. Out[12]: NaT
  43.  
  44. # ISO 8601 Duration strings
  45. In [13]: pd.Timedelta('P0DT0H1M0S')
  46. Out[13]: Timedelta('0 days 00:01:00')
  47.  
  48. In [14]: pd.Timedelta('P0DT0H0M0.000000123S')
  49. Out[14]: Timedelta('0 days 00:00:00.000000')

New in version 0.23.0: Added constructor for ISO 8601 Duration strings

DateOffsets (Day, Hour, Minute, Second, Milli, Micro, Nano) can also be used in construction.

  1. In [15]: pd.Timedelta(pd.offsets.Second(2))
  2. Out[15]: Timedelta('0 days 00:00:02')

Further, operations among the scalars yield another scalar Timedelta.

  1. In [16]: pd.Timedelta(pd.offsets.Day(2)) + pd.Timedelta(pd.offsets.Second(2)) +\
  2. ....: pd.Timedelta('00:00:00.000123')
  3. ....:
  4. Out[16]: Timedelta('2 days 00:00:02.000123')

to_timedelta

Using the top-level pd.to_timedelta, you can convert a scalar, array, list,or Series from a recognized timedelta format / value into a Timedelta type.It will construct Series if the input is a Series, a scalar if the input isscalar-like, otherwise it will output a TimedeltaIndex.

You can parse a single string to a Timedelta:

  1. In [17]: pd.to_timedelta('1 days 06:05:01.00003')
  2. Out[17]: Timedelta('1 days 06:05:01.000030')
  3.  
  4. In [18]: pd.to_timedelta('15.5us')
  5. Out[18]: Timedelta('0 days 00:00:00.000015')

or a list/array of strings:

  1. In [19]: pd.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])
  2. Out[19]: TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015', NaT], dtype='timedelta64[ns]', freq=None)

The unit keyword argument specifies the unit of the Timedelta:

  1. In [20]: pd.to_timedelta(np.arange(5), unit='s')
  2. Out[20]: TimedeltaIndex(['00:00:00', '00:00:01', '00:00:02', '00:00:03', '00:00:04'], dtype='timedelta64[ns]', freq=None)
  3.  
  4. In [21]: pd.to_timedelta(np.arange(5), unit='d')
  5. Out[21]: TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)

Timedelta limitations

Pandas represents Timedeltas in nanosecond resolution using64 bit integers. As such, the 64 bit integer limits determinethe Timedelta limits.

  1. In [22]: pd.Timedelta.min
  2. Out[22]: Timedelta('-106752 days +00:12:43.145224')
  3.  
  4. In [23]: pd.Timedelta.max
  5. Out[23]: Timedelta('106751 days 23:47:16.854775')

Operations

You can operate on Series/DataFrames and construct timedelta64[ns] Series throughsubtraction operations on datetime64[ns] Series, or Timestamps.

  1. In [24]: s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D'))
  2.  
  3. In [25]: td = pd.Series([pd.Timedelta(days=i) for i in range(3)])
  4.  
  5. In [26]: df = pd.DataFrame({'A': s, 'B': td})
  6.  
  7. In [27]: df
  8. Out[27]:
  9. A B
  10. 0 2012-01-01 0 days
  11. 1 2012-01-02 1 days
  12. 2 2012-01-03 2 days
  13.  
  14. In [28]: df['C'] = df['A'] + df['B']
  15.  
  16. In [29]: df
  17. Out[29]:
  18. A B C
  19. 0 2012-01-01 0 days 2012-01-01
  20. 1 2012-01-02 1 days 2012-01-03
  21. 2 2012-01-03 2 days 2012-01-05
  22.  
  23. In [30]: df.dtypes
  24. Out[30]:
  25. A datetime64[ns]
  26. B timedelta64[ns]
  27. C datetime64[ns]
  28. dtype: object
  29.  
  30. In [31]: s - s.max()
  31. Out[31]:
  32. 0 -2 days
  33. 1 -1 days
  34. 2 0 days
  35. dtype: timedelta64[ns]
  36.  
  37. In [32]: s - datetime.datetime(2011, 1, 1, 3, 5)
  38. Out[32]:
  39. 0 364 days 20:55:00
  40. 1 365 days 20:55:00
  41. 2 366 days 20:55:00
  42. dtype: timedelta64[ns]
  43.  
  44. In [33]: s + datetime.timedelta(minutes=5)
  45. Out[33]:
  46. 0 2012-01-01 00:05:00
  47. 1 2012-01-02 00:05:00
  48. 2 2012-01-03 00:05:00
  49. dtype: datetime64[ns]
  50.  
  51. In [34]: s + pd.offsets.Minute(5)
  52. Out[34]:
  53. 0 2012-01-01 00:05:00
  54. 1 2012-01-02 00:05:00
  55. 2 2012-01-03 00:05:00
  56. dtype: datetime64[ns]
  57.  
  58. In [35]: s + pd.offsets.Minute(5) + pd.offsets.Milli(5)
  59. Out[35]:
  60. 0 2012-01-01 00:05:00.005
  61. 1 2012-01-02 00:05:00.005
  62. 2 2012-01-03 00:05:00.005
  63. dtype: datetime64[ns]

Operations with scalars from a timedelta64[ns] series:

  1. In [36]: y = s - s[0]
  2.  
  3. In [37]: y
  4. Out[37]:
  5. 0 0 days
  6. 1 1 days
  7. 2 2 days
  8. dtype: timedelta64[ns]

Series of timedeltas with NaT values are supported:

  1. In [38]: y = s - s.shift()
  2.  
  3. In [39]: y
  4. Out[39]:
  5. 0 NaT
  6. 1 1 days
  7. 2 1 days
  8. dtype: timedelta64[ns]

Elements can be set to NaT using np.nan analogously to datetimes:

  1. In [40]: y[1] = np.nan
  2.  
  3. In [41]: y
  4. Out[41]:
  5. 0 NaT
  6. 1 NaT
  7. 2 1 days
  8. dtype: timedelta64[ns]

Operands can also appear in a reversed order (a singular object operated with a Series):

  1. In [42]: s.max() - s
  2. Out[42]:
  3. 0 2 days
  4. 1 1 days
  5. 2 0 days
  6. dtype: timedelta64[ns]
  7.  
  8. In [43]: datetime.datetime(2011, 1, 1, 3, 5) - s
  9. Out[43]:
  10. 0 -365 days +03:05:00
  11. 1 -366 days +03:05:00
  12. 2 -367 days +03:05:00
  13. dtype: timedelta64[ns]
  14.  
  15. In [44]: datetime.timedelta(minutes=5) + s
  16. Out[44]:
  17. 0 2012-01-01 00:05:00
  18. 1 2012-01-02 00:05:00
  19. 2 2012-01-03 00:05:00
  20. dtype: datetime64[ns]

min, max and the corresponding idxmin, idxmax operations are supported on frames:

  1. In [45]: A = s - pd.Timestamp('20120101') - pd.Timedelta('00:05:05')
  2.  
  3. In [46]: B = s - pd.Series(pd.date_range('2012-1-2', periods=3, freq='D'))
  4.  
  5. In [47]: df = pd.DataFrame({'A': A, 'B': B})
  6.  
  7. In [48]: df
  8. Out[48]:
  9. A B
  10. 0 -1 days +23:54:55 -1 days
  11. 1 0 days 23:54:55 -1 days
  12. 2 1 days 23:54:55 -1 days
  13.  
  14. In [49]: df.min()
  15. Out[49]:
  16. A -1 days +23:54:55
  17. B -1 days +00:00:00
  18. dtype: timedelta64[ns]
  19.  
  20. In [50]: df.min(axis=1)
  21. Out[50]:
  22. 0 -1 days
  23. 1 -1 days
  24. 2 -1 days
  25. dtype: timedelta64[ns]
  26.  
  27. In [51]: df.idxmin()
  28. Out[51]:
  29. A 0
  30. B 0
  31. dtype: int64
  32.  
  33. In [52]: df.idxmax()
  34. Out[52]:
  35. A 2
  36. B 0
  37. dtype: int64

min, max, idxmin, idxmax operations are supported on Series as well. A scalar result will be a Timedelta.

  1. In [53]: df.min().max()
  2. Out[53]: Timedelta('-1 days +23:54:55')
  3.  
  4. In [54]: df.min(axis=1).min()
  5. Out[54]: Timedelta('-1 days +00:00:00')
  6.  
  7. In [55]: df.min().idxmax()
  8. Out[55]: 'A'
  9.  
  10. In [56]: df.min(axis=1).idxmin()
  11. Out[56]: 0

You can fillna on timedeltas, passing a timedelta to get a particular value.

  1. In [57]: y.fillna(pd.Timedelta(0))
  2. Out[57]:
  3. 0 0 days
  4. 1 0 days
  5. 2 1 days
  6. dtype: timedelta64[ns]
  7.  
  8. In [58]: y.fillna(pd.Timedelta(10, unit='s'))
  9. Out[58]:
  10. 0 0 days 00:00:10
  11. 1 0 days 00:00:10
  12. 2 1 days 00:00:00
  13. dtype: timedelta64[ns]
  14.  
  15. In [59]: y.fillna(pd.Timedelta('-1 days, 00:00:05'))
  16. Out[59]:
  17. 0 -1 days +00:00:05
  18. 1 -1 days +00:00:05
  19. 2 1 days 00:00:00
  20. dtype: timedelta64[ns]

You can also negate, multiply and use abs on Timedeltas:

  1. In [60]: td1 = pd.Timedelta('-1 days 2 hours 3 seconds')
  2.  
  3. In [61]: td1
  4. Out[61]: Timedelta('-2 days +21:59:57')
  5.  
  6. In [62]: -1 * td1
  7. Out[62]: Timedelta('1 days 02:00:03')
  8.  
  9. In [63]: - td1
  10. Out[63]: Timedelta('1 days 02:00:03')
  11.  
  12. In [64]: abs(td1)
  13. Out[64]: Timedelta('1 days 02:00:03')

Reductions

Numeric reduction operation for timedelta64[ns] will return Timedelta objects. As usualNaT are skipped during evaluation.

  1. In [65]: y2 = pd.Series(pd.to_timedelta(['-1 days +00:00:05', 'nat',
  2. ....: '-1 days +00:00:05', '1 days']))
  3. ....:
  4.  
  5. In [66]: y2
  6. Out[66]:
  7. 0 -1 days +00:00:05
  8. 1 NaT
  9. 2 -1 days +00:00:05
  10. 3 1 days 00:00:00
  11. dtype: timedelta64[ns]
  12.  
  13. In [67]: y2.mean()
  14. Out[67]: Timedelta('-1 days +16:00:03.333333')
  15.  
  16. In [68]: y2.median()
  17. Out[68]: Timedelta('-1 days +00:00:05')
  18.  
  19. In [69]: y2.quantile(.1)
  20. Out[69]: Timedelta('-1 days +00:00:05')
  21.  
  22. In [70]: y2.sum()
  23. Out[70]: Timedelta('-1 days +00:00:10')

Frequency conversion

Timedelta Series, TimedeltaIndex, and Timedelta scalars can be converted to other ‘frequencies’ by dividing by another timedelta,or by astyping to a specific timedelta type. These operations yield Series and propagate NaT -> nan.Note that division by the NumPy scalar is true division, while astyping is equivalent of floor division.

  1. In [71]: december = pd.Series(pd.date_range('20121201', periods=4))
  2.  
  3. In [72]: january = pd.Series(pd.date_range('20130101', periods=4))
  4.  
  5. In [73]: td = january - december
  6.  
  7. In [74]: td[2] += datetime.timedelta(minutes=5, seconds=3)
  8.  
  9. In [75]: td[3] = np.nan
  10.  
  11. In [76]: td
  12. Out[76]:
  13. 0 31 days 00:00:00
  14. 1 31 days 00:00:00
  15. 2 31 days 00:05:03
  16. 3 NaT
  17. dtype: timedelta64[ns]
  18.  
  19. # to days
  20. In [77]: td / np.timedelta64(1, 'D')
  21. Out[77]:
  22. 0 31.000000
  23. 1 31.000000
  24. 2 31.003507
  25. 3 NaN
  26. dtype: float64
  27.  
  28. In [78]: td.astype('timedelta64[D]')
  29. Out[78]:
  30. 0 31.0
  31. 1 31.0
  32. 2 31.0
  33. 3 NaN
  34. dtype: float64
  35.  
  36. # to seconds
  37. In [79]: td / np.timedelta64(1, 's')
  38. Out[79]:
  39. 0 2678400.0
  40. 1 2678400.0
  41. 2 2678703.0
  42. 3 NaN
  43. dtype: float64
  44.  
  45. In [80]: td.astype('timedelta64[s]')
  46. Out[80]:
  47. 0 2678400.0
  48. 1 2678400.0
  49. 2 2678703.0
  50. 3 NaN
  51. dtype: float64
  52.  
  53. # to months (these are constant months)
  54. In [81]: td / np.timedelta64(1, 'M')
  55. Out[81]:
  56. 0 1.018501
  57. 1 1.018501
  58. 2 1.018617
  59. 3 NaN
  60. dtype: float64

Dividing or multiplying a timedelta64[ns] Series by an integer or integer Seriesyields another timedelta64[ns] dtypes Series.

  1. In [82]: td * -1
  2. Out[82]:
  3. 0 -31 days +00:00:00
  4. 1 -31 days +00:00:00
  5. 2 -32 days +23:54:57
  6. 3 NaT
  7. dtype: timedelta64[ns]
  8.  
  9. In [83]: td * pd.Series([1, 2, 3, 4])
  10. Out[83]:
  11. 0 31 days 00:00:00
  12. 1 62 days 00:00:00
  13. 2 93 days 00:15:09
  14. 3 NaT
  15. dtype: timedelta64[ns]

Rounded division (floor-division) of a timedelta64[ns] Series by a scalarTimedelta gives a series of integers.

  1. In [84]: td // pd.Timedelta(days=3, hours=4)
  2. Out[84]:
  3. 0 9.0
  4. 1 9.0
  5. 2 9.0
  6. 3 NaN
  7. dtype: float64
  8.  
  9. In [85]: pd.Timedelta(days=3, hours=4) // td
  10. Out[85]:
  11. 0 0.0
  12. 1 0.0
  13. 2 0.0
  14. 3 NaN
  15. dtype: float64

The mod (%) and divmod operations are defined for Timedelta when operating with another timedelta-like or with a numeric argument.

  1. In [86]: pd.Timedelta(hours=37) % datetime.timedelta(hours=2)
  2. Out[86]: Timedelta('0 days 01:00:00')
  3.  
  4. # divmod against a timedelta-like returns a pair (int, Timedelta)
  5. In [87]: divmod(datetime.timedelta(hours=2), pd.Timedelta(minutes=11))
  6. Out[87]: (10, Timedelta('0 days 00:10:00'))
  7.  
  8. # divmod against a numeric returns a pair (Timedelta, Timedelta)
  9. In [88]: divmod(pd.Timedelta(hours=25), 86400000000000)
  10. Out[88]: (Timedelta('0 days 00:00:00.000000'), Timedelta('0 days 01:00:00'))

Attributes

You can access various components of the Timedelta or TimedeltaIndex directly using the attributes days,seconds,microseconds,nanoseconds. These are identical to the values returned by datetime.timedelta, in that, for example, the .seconds attribute represents the number of seconds >= 0 and < 1 day. These are signed according to whether the Timedelta is signed.

These operations can also be directly accessed via the .dt property of the Series as well.

Note

Note that the attributes are NOT the displayed values of the Timedelta. Use .components to retrieve the displayed values.

For a Series:

  1. In [89]: td.dt.days
  2. Out[89]:
  3. 0 31.0
  4. 1 31.0
  5. 2 31.0
  6. 3 NaN
  7. dtype: float64
  8.  
  9. In [90]: td.dt.seconds
  10. Out[90]:
  11. 0 0.0
  12. 1 0.0
  13. 2 303.0
  14. 3 NaN
  15. dtype: float64

You can access the value of the fields for a scalar Timedelta directly.

  1. In [91]: tds = pd.Timedelta('31 days 5 min 3 sec')
  2.  
  3. In [92]: tds.days
  4. Out[92]: 31
  5.  
  6. In [93]: tds.seconds
  7. Out[93]: 303
  8.  
  9. In [94]: (-tds).seconds
  10. Out[94]: 86097

You can use the .components property to access a reduced form of the timedelta. This returns a DataFrame indexedsimilarly to the Series. These are the displayed values of the Timedelta.

  1. In [95]: td.dt.components
  2. Out[95]:
  3. days hours minutes seconds milliseconds microseconds nanoseconds
  4. 0 31.0 0.0 0.0 0.0 0.0 0.0 0.0
  5. 1 31.0 0.0 0.0 0.0 0.0 0.0 0.0
  6. 2 31.0 0.0 5.0 3.0 0.0 0.0 0.0
  7. 3 NaN NaN NaN NaN NaN NaN NaN
  8.  
  9. In [96]: td.dt.components.seconds
  10. Out[96]:
  11. 0 0.0
  12. 1 0.0
  13. 2 3.0
  14. 3 NaN
  15. Name: seconds, dtype: float64

You can convert a Timedelta to an ISO 8601 Duration string with the.isoformat method

New in version 0.20.0.

  1. In [97]: pd.Timedelta(days=6, minutes=50, seconds=3,
  2. ....: milliseconds=10, microseconds=10,
  3. ....: nanoseconds=12).isoformat()
  4. ....:
  5. Out[97]: 'P6DT0H50M3.010010012S'

TimedeltaIndex

To generate an index with time delta, you can use either the TimedeltaIndex orthe timedelta_range() constructor.

Using TimedeltaIndex you can pass string-like, Timedelta, timedelta,or np.timedelta64 objects. Passing np.nan/pd.NaT/nat will represent missing values.

  1. In [98]: pd.TimedeltaIndex(['1 days', '1 days, 00:00:05', np.timedelta64(2, 'D'),
  2. ....: datetime.timedelta(days=2, seconds=2)])
  3. ....:
  4. Out[98]:
  5. TimedeltaIndex(['1 days 00:00:00', '1 days 00:00:05', '2 days 00:00:00',
  6. '2 days 00:00:02'],
  7. dtype='timedelta64[ns]', freq=None)

The string ‘infer’ can be passed in order to set the frequency of the index as theinferred frequency upon creation:

  1. In [99]: pd.TimedeltaIndex(['0 days', '10 days', '20 days'], freq='infer')
  2. Out[99]: TimedeltaIndex(['0 days', '10 days', '20 days'], dtype='timedelta64[ns]', freq='10D')

Generating ranges of time deltas

Similar to date_range(), you can construct regular ranges of a TimedeltaIndexusing timedelta_range(). The default frequency for timedelta_range iscalendar day:

  1. In [100]: pd.timedelta_range(start='1 days', periods=5)
  2. Out[100]: TimedeltaIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], dtype='timedelta64[ns]', freq='D')

Various combinations of start, end, and periods can be used withtimedelta_range:

  1. In [101]: pd.timedelta_range(start='1 days', end='5 days')
  2. Out[101]: TimedeltaIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], dtype='timedelta64[ns]', freq='D')
  3.  
  4. In [102]: pd.timedelta_range(end='10 days', periods=4)
  5. Out[102]: TimedeltaIndex(['7 days', '8 days', '9 days', '10 days'], dtype='timedelta64[ns]', freq='D')

The freq parameter can passed a variety of frequency aliases:

  1. In [103]: pd.timedelta_range(start='1 days', end='2 days', freq='30T')
  2. Out[103]:
  3. TimedeltaIndex(['1 days 00:00:00', '1 days 00:30:00', '1 days 01:00:00',
  4. '1 days 01:30:00', '1 days 02:00:00', '1 days 02:30:00',
  5. '1 days 03:00:00', '1 days 03:30:00', '1 days 04:00:00',
  6. '1 days 04:30:00', '1 days 05:00:00', '1 days 05:30:00',
  7. '1 days 06:00:00', '1 days 06:30:00', '1 days 07:00:00',
  8. '1 days 07:30:00', '1 days 08:00:00', '1 days 08:30:00',
  9. '1 days 09:00:00', '1 days 09:30:00', '1 days 10:00:00',
  10. '1 days 10:30:00', '1 days 11:00:00', '1 days 11:30:00',
  11. '1 days 12:00:00', '1 days 12:30:00', '1 days 13:00:00',
  12. '1 days 13:30:00', '1 days 14:00:00', '1 days 14:30:00',
  13. '1 days 15:00:00', '1 days 15:30:00', '1 days 16:00:00',
  14. '1 days 16:30:00', '1 days 17:00:00', '1 days 17:30:00',
  15. '1 days 18:00:00', '1 days 18:30:00', '1 days 19:00:00',
  16. '1 days 19:30:00', '1 days 20:00:00', '1 days 20:30:00',
  17. '1 days 21:00:00', '1 days 21:30:00', '1 days 22:00:00',
  18. '1 days 22:30:00', '1 days 23:00:00', '1 days 23:30:00',
  19. '2 days 00:00:00'],
  20. dtype='timedelta64[ns]', freq='30T')
  21.  
  22. In [104]: pd.timedelta_range(start='1 days', periods=5, freq='2D5H')
  23. Out[104]:
  24. TimedeltaIndex(['1 days 00:00:00', '3 days 05:00:00', '5 days 10:00:00',
  25. '7 days 15:00:00', '9 days 20:00:00'],
  26. dtype='timedelta64[ns]', freq='53H')

New in version 0.23.0.

Specifying start, end, and periods will generate a range of evenly spacedtimedeltas from start to end inclusively, with periods number of elementsin the resulting TimedeltaIndex:

  1. In [105]: pd.timedelta_range('0 days', '4 days', periods=5)
  2. Out[105]: TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)
  3.  
  4. In [106]: pd.timedelta_range('0 days', '4 days', periods=10)
  5. Out[106]:
  6. TimedeltaIndex(['0 days 00:00:00', '0 days 10:40:00', '0 days 21:20:00',
  7. '1 days 08:00:00', '1 days 18:40:00', '2 days 05:20:00',
  8. '2 days 16:00:00', '3 days 02:40:00', '3 days 13:20:00',
  9. '4 days 00:00:00'],
  10. dtype='timedelta64[ns]', freq=None)

Using the TimedeltaIndex

Similarly to other of the datetime-like indices, DatetimeIndex and PeriodIndex, you can useTimedeltaIndex as the index of pandas objects.

  1. In [107]: s = pd.Series(np.arange(100),
  2. .....: index=pd.timedelta_range('1 days', periods=100, freq='h'))
  3. .....:
  4.  
  5. In [108]: s
  6. Out[108]:
  7. 1 days 00:00:00 0
  8. 1 days 01:00:00 1
  9. 1 days 02:00:00 2
  10. 1 days 03:00:00 3
  11. 1 days 04:00:00 4
  12. ..
  13. 4 days 23:00:00 95
  14. 5 days 00:00:00 96
  15. 5 days 01:00:00 97
  16. 5 days 02:00:00 98
  17. 5 days 03:00:00 99
  18. Freq: H, Length: 100, dtype: int64

Selections work similarly, with coercion on string-likes and slices:

  1. In [109]: s['1 day':'2 day']
  2. Out[109]:
  3. 1 days 00:00:00 0
  4. 1 days 01:00:00 1
  5. 1 days 02:00:00 2
  6. 1 days 03:00:00 3
  7. 1 days 04:00:00 4
  8. ..
  9. 2 days 19:00:00 43
  10. 2 days 20:00:00 44
  11. 2 days 21:00:00 45
  12. 2 days 22:00:00 46
  13. 2 days 23:00:00 47
  14. Freq: H, Length: 48, dtype: int64
  15.  
  16. In [110]: s['1 day 01:00:00']
  17. Out[110]: 1
  18.  
  19. In [111]: s[pd.Timedelta('1 day 1h')]
  20. Out[111]: 1

Furthermore you can use partial string selection and the range will be inferred:

  1. In [112]: s['1 day':'1 day 5 hours']
  2. Out[112]:
  3. 1 days 00:00:00 0
  4. 1 days 01:00:00 1
  5. 1 days 02:00:00 2
  6. 1 days 03:00:00 3
  7. 1 days 04:00:00 4
  8. 1 days 05:00:00 5
  9. Freq: H, dtype: int64

Operations

Finally, the combination of TimedeltaIndex with DatetimeIndex allow certain combination operations that are NaT preserving:

  1. In [113]: tdi = pd.TimedeltaIndex(['1 days', pd.NaT, '2 days'])
  2.  
  3. In [114]: tdi.to_list()
  4. Out[114]: [Timedelta('1 days 00:00:00'), NaT, Timedelta('2 days 00:00:00')]
  5.  
  6. In [115]: dti = pd.date_range('20130101', periods=3)
  7.  
  8. In [116]: dti.to_list()
  9. Out[116]:
  10. [Timestamp('2013-01-01 00:00:00', freq='D'),
  11. Timestamp('2013-01-02 00:00:00', freq='D'),
  12. Timestamp('2013-01-03 00:00:00', freq='D')]
  13.  
  14. In [117]: (dti + tdi).to_list()
  15. Out[117]: [Timestamp('2013-01-02 00:00:00'), NaT, Timestamp('2013-01-05 00:00:00')]
  16.  
  17. In [118]: (dti - tdi).to_list()
  18. Out[118]: [Timestamp('2012-12-31 00:00:00'), NaT, Timestamp('2013-01-01 00:00:00')]

Conversions

Similarly to frequency conversion on a Series above, you can convert these indices to yield another Index.

  1. In [119]: tdi / np.timedelta64(1, 's')
  2. Out[119]: Float64Index([86400.0, nan, 172800.0], dtype='float64')
  3.  
  4. In [120]: tdi.astype('timedelta64[s]')
  5. Out[120]: Float64Index([86400.0, nan, 172800.0], dtype='float64')

Scalars type ops work as well. These can potentially return a different type of index.

  1. # adding or timedelta and date -> datelike
  2. In [121]: tdi + pd.Timestamp('20130101')
  3. Out[121]: DatetimeIndex(['2013-01-02', 'NaT', '2013-01-03'], dtype='datetime64[ns]', freq=None)
  4.  
  5. # subtraction of a date and a timedelta -> datelike
  6. # note that trying to subtract a date from a Timedelta will raise an exception
  7. In [122]: (pd.Timestamp('20130101') - tdi).to_list()
  8. Out[122]: [Timestamp('2012-12-31 00:00:00'), NaT, Timestamp('2012-12-30 00:00:00')]
  9.  
  10. # timedelta + timedelta -> timedelta
  11. In [123]: tdi + pd.Timedelta('10 days')
  12. Out[123]: TimedeltaIndex(['11 days', NaT, '12 days'], dtype='timedelta64[ns]', freq=None)
  13.  
  14. # division can result in a Timedelta if the divisor is an integer
  15. In [124]: tdi / 2
  16. Out[124]: TimedeltaIndex(['0 days 12:00:00', NaT, '1 days 00:00:00'], dtype='timedelta64[ns]', freq=None)
  17.  
  18. # or a Float64Index if the divisor is a Timedelta
  19. In [125]: tdi / tdi[0]
  20. Out[125]: Float64Index([1.0, nan, 2.0], dtype='float64')

Resampling

Similar to timeseries resampling, we can resample with a TimedeltaIndex.

  1. In [126]: s.resample('D').mean()
  2. Out[126]:
  3. 1 days 11.5
  4. 2 days 35.5
  5. 3 days 59.5
  6. 4 days 83.5
  7. 5 days 97.5
  8. Freq: D, dtype: float64