2.11 Timedeltas
The Timedeltas docs.
In [1]: s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D'))
In [2]: s - s.max()
Out[2]:
0 -2 days
1 -1 days
2 0 days
dtype: timedelta64[ns]
In [3]: s.max() - s
Out[3]:
0 2 days
1 1 days
2 0 days
dtype: timedelta64[ns]
In [4]: s - datetime.datetime(2011,1,1,3,5)
Out[4]:
0 364 days 20:55:00
1 365 days 20:55:00
2 366 days 20:55:00
dtype: timedelta64[ns]
In [5]: s + datetime.timedelta(minutes=5)
Out[5]:
0 2012-01-01 00:05:00
1 2012-01-02 00:05:00
2 2012-01-03 00:05:00
dtype: datetime64[ns]
In [6]: datetime.datetime(2011,1,1,3,5) - s
Out[6]:
0 -365 days +03:05:00
1 -366 days +03:05:00
2 -367 days +03:05:00
dtype: timedelta64[ns]
In [7]: datetime.timedelta(minutes=5) + s
Out[7]:
0 2012-01-01 00:05:00
1 2012-01-02 00:05:00
2 2012-01-03 00:05:00
dtype: datetime64[ns]
Adding and subtracting deltas and dates
In [8]: deltas = pd.Series([ datetime.timedelta(days=i) for i in range(3) ])
In [9]: df = pd.DataFrame(dict(A = s, B = deltas)); df
Out[9]:
A B
0 2012-01-01 0 days
1 2012-01-02 1 days
2 2012-01-03 2 days
In [10]: df['New Dates'] = df['A'] + df['B'];
In [11]: df['Delta'] = df['A'] - df['New Dates']; df
Out[11]:
A B New Dates Delta
0 2012-01-01 0 days 2012-01-01 0 days
1 2012-01-02 1 days 2012-01-03 -1 days
2 2012-01-03 2 days 2012-01-05 -2 days
In [12]: df.dtypes
Out[12]:
A datetime64[ns]
B timedelta64[ns]
New Dates datetime64[ns]
Delta timedelta64[ns]
dtype: object
Values can be set to NaT using np.nan, similar to datetime
In [13]: y = s - s.shift(); y
Out[13]:
0 NaT
1 1 days
2 1 days
dtype: timedelta64[ns]
In [14]: y[1] = np.nan; y
Out[14]:
0 NaT
1 NaT
2 1 days
dtype: timedelta64[ns]