Pandas Timedelta in Days Pandas Timedelta in Days python python

Pandas Timedelta in Days


Using the Pandas type Timedelta available since v0.15.0 you also can do:

In[1]: import pandas as pdIn[2]: df = pd.DataFrame([ pd.Timestamp('20150111'),                            pd.Timestamp('20150301') ], columns=['date'])In[3]: df['today'] = pd.Timestamp('20150315')In[4]: dfOut[4]:         date      today0 2015-01-11 2015-03-151 2015-03-01 2015-03-15In[5]: (df['today'] - df['date']).dt.daysOut[5]: 0    631    14dtype: int64


You need 0.11 for this (0.11rc1 is out, final prob next week)

In [9]: df = DataFrame([ Timestamp('20010101'), Timestamp('20040601') ])In [10]: dfOut[10]:                     00 2001-01-01 00:00:001 2004-06-01 00:00:00In [11]: df = DataFrame([ Timestamp('20010101'),                           Timestamp('20040601') ],columns=['age'])In [12]: dfOut[12]:                   age0 2001-01-01 00:00:001 2004-06-01 00:00:00In [13]: df['today'] = Timestamp('20130419')In [14]: df['diff'] = df['today']-df['age']In [16]: df['years'] = df['diff'].apply(lambda x: float(x.item().days)/365)In [17]: dfOut[17]:                   age               today                diff      years0 2001-01-01 00:00:00 2013-04-19 00:00:00 4491 days, 00:00:00  12.3041101 2004-06-01 00:00:00 2013-04-19 00:00:00 3244 days, 00:00:00   8.887671

You need this odd apply at the end because not yet full support for timedelta64[ns] scalars (e.g. like how we use Timestamps now for datetime64[ns], coming in 0.12)


Not sure if you still need it, but in Pandas 0.14 i usually use .astype('timedelta64[X]') methodhttp://pandas.pydata.org/pandas-docs/stable/timeseries.html (frequency conversion)

df = pd.DataFrame([ pd.Timestamp('20010101'), pd.Timestamp('20040605') ])df.ix[0]-df.ix[1]

Returns:

0   -1251 daysdtype: timedelta64[ns]
(df.ix[0]-df.ix[1]).astype('timedelta64[Y]')

Returns:

  0   -4 dtype: float64

Hope that will help