Time Deltas

Parsing


In [1]:
import pandas as pd
import numpy as np

In [2]:
pd.Timedelta('-3 days'), pd.Timedelta('13 min'), pd.Timedelta(1, unit='M')


Out[2]:
(Timedelta('-3 days +00:00:00'),
 Timedelta('0 days 00:13:00'),
 Timedelta('30 days 10:29:06'))

In [3]:
pd.to_timedelta('2 days'), pd.to_timedelta(np.arange(10), unit='M')


Out[3]:
(Timedelta('2 days 00:00:00'), <class 'pandas.tseries.tdi.TimedeltaIndex'>
 ['0 days 00:00:00', ..., '273 days 22:21:54']
 Length: 10, Freq: None)

Operations


In [4]:
df = pd.DataFrame(dict(A = pd.Series(pd.date_range('2014/1/1/', periods=3)), B = [pd.Timedelta(days=i + 1) for i in range(3)]))
df['C'] = df['A'] + df['B']
df['D'] = df['C'] - max(df['B'])
df['E'] = df['D'] + pd.tseries.offsets.Day(10)
df


Out[4]:
A B C D E
0 2014-01-01 1 days 2014-01-02 2013-12-30 2014-01-09
1 2014-01-02 2 days 2014-01-04 2014-01-01 2014-01-11
2 2014-01-03 3 days 2014-01-06 2014-01-03 2014-01-13

Reductions


In [5]:
df['B'].mean()


Out[5]:
Timedelta('2 days 00:00:00')

In [6]:
df['B'].sum()


Out[6]:
Timedelta('6 days 00:00:00')

Frequency conversion


In [7]:
s = pd.Series([pd.Timedelta(days=np.random.randint(10)) for i in range(5)])
s


Out[7]:
0   3 days
1   7 days
2   8 days
3   9 days
4   7 days
dtype: timedelta64[ns]

In [8]:
s / np.timedelta64(1, 'D')


Out[8]:
0    3
1    7
2    8
3    9
4    7
dtype: float64

In [9]:
s.astype('timedelta64[D]')


Out[9]:
0    3
1    7
2    8
3    9
4    7
dtype: float64

Attributes


In [10]:
s.dt.days


Out[10]:
0    3
1    7
2    8
3    9
4    7
dtype: int64

In [11]:
s.dt.components


Out[11]:
days hours minutes seconds milliseconds microseconds nanoseconds
0 3 0 0 0 0 0 0
1 7 0 0 0 0 0 0
2 8 0 0 0 0 0 0
3 9 0 0 0 0 0 0
4 7 0 0 0 0 0 0

TimedeltaIndex


In [12]:
pd.TimedeltaIndex(['1 days', '1 days 00:00:03', np.timedelta64(2, 'M')])


Out[12]:
<class 'pandas.tseries.tdi.TimedeltaIndex'>
['1 days 00:00:00', ..., '60 days 20:58:12']
Length: 3, Freq: None

In [13]:
pd.timedelta_range(start='1 hours', freq='5H', periods=10)


Out[13]:
<class 'pandas.tseries.tdi.TimedeltaIndex'>
['0 days 01:00:00', ..., '1 days 22:00:00']
Length: 10, Freq: <5 * Hours>

Using TimedeltaIndex


In [14]:
s = pd.Series(np.arange(100), index=pd.timedelta_range('1 days', periods=100, freq='H'))
s['1 days': '2 days']


Out[14]:
1 days 00:00:00     0
1 days 01:00:00     1
1 days 02:00:00     2
1 days 03:00:00     3
1 days 04:00:00     4
1 days 05:00:00     5
1 days 06:00:00     6
1 days 07:00:00     7
1 days 08:00:00     8
1 days 09:00:00     9
1 days 10:00:00    10
1 days 11:00:00    11
1 days 12:00:00    12
1 days 13:00:00    13
1 days 14:00:00    14
1 days 15:00:00    15
1 days 16:00:00    16
1 days 17:00:00    17
1 days 18:00:00    18
1 days 19:00:00    19
1 days 20:00:00    20
1 days 21:00:00    21
1 days 22:00:00    22
1 days 23:00:00    23
2 days 00:00:00    24
2 days 01:00:00    25
2 days 02:00:00    26
2 days 03:00:00    27
2 days 04:00:00    28
2 days 05:00:00    29
2 days 06:00:00    30
2 days 07:00:00    31
2 days 08:00:00    32
2 days 09:00:00    33
2 days 10:00:00    34
2 days 11:00:00    35
2 days 12:00:00    36
2 days 13:00:00    37
2 days 14:00:00    38
2 days 15:00:00    39
2 days 16:00:00    40
2 days 17:00:00    41
2 days 18:00:00    42
2 days 19:00:00    43
2 days 20:00:00    44
2 days 21:00:00    45
2 days 22:00:00    46
2 days 23:00:00    47
dtype: int64

In [15]:
s['1 days 23 hours']


Out[15]:
23

In [16]:
s[pd.Timedelta('3 days 2 hours')]


Out[16]:
50

Operations

Conversions

Resampling


In [17]:
s.resample('D', how='sum')


Out[17]:
1 days     276
2 days     852
3 days    1428
4 days    2004
5 days     390
dtype: int64