BigHandsome BigHandsome - 1 month ago 11
Python Question

Add days to dates in dataframe

I am stymied at the moment. I am sure that I am missing something simple, but how do you move a series of dates forward by x units? In my more specific case I want to add 180 days to a date series within a dataframe.

Here is what I have so far:

import pandas, numpy, StringIO, datetime


txt = '''ID,DATE
002691c9cec109e64558848f1358ac16,2003-08-13 00:00:00
002691c9cec109e64558848f1358ac16,2003-08-13 00:00:00
0088f218a1f00e0fe1b94919dc68ec33,2006-05-07 00:00:00
0088f218a1f00e0fe1b94919dc68ec33,2006-06-03 00:00:00
00d34668025906d55ae2e529615f530a,2006-03-09 00:00:00
00d34668025906d55ae2e529615f530a,2006-03-09 00:00:00
0101d3286dfbd58642a7527ecbddb92e,2007-10-13 00:00:00
0101d3286dfbd58642a7527ecbddb92e,2007-10-27 00:00:00
0103bd73af66e5a44f7867c0bb2203cc,2001-02-01 00:00:00
0103bd73af66e5a44f7867c0bb2203cc,2008-01-20 00:00:00
'''
df = pandas.read_csv(StringIO.StringIO(txt))
df = df.sort('DATE')
df.DATE = pandas.to_datetime(df.DATE)
df['X_DATE'] = df['DATE'].shift(180, freq=pandas.datetools.Day)


This code generates a type error. For reference I am using:

Python 2.7.4
Pandas '0.12.0.dev-6e7c4d6'
Numpy '1.7.1'

DSM DSM
Answer

If I understand you, you don't actually want shift, you simply want to make a new column next to the existing DATE which is 180 days after. In that case, you can use timedelta:

>>> from datetime import timedelta
>>> df.head()
                                 ID                DATE
8  0103bd73af66e5a44f7867c0bb2203cc 2001-02-01 00:00:00
0  002691c9cec109e64558848f1358ac16 2003-08-13 00:00:00
1  002691c9cec109e64558848f1358ac16 2003-08-13 00:00:00
5  00d34668025906d55ae2e529615f530a 2006-03-09 00:00:00
4  00d34668025906d55ae2e529615f530a 2006-03-09 00:00:00
>>> df["X_DATE"] = df["DATE"] + timedelta(days=180)
>>> df.head()
                                 ID                DATE              X_DATE
8  0103bd73af66e5a44f7867c0bb2203cc 2001-02-01 00:00:00 2001-07-31 00:00:00
0  002691c9cec109e64558848f1358ac16 2003-08-13 00:00:00 2004-02-09 00:00:00
1  002691c9cec109e64558848f1358ac16 2003-08-13 00:00:00 2004-02-09 00:00:00
5  00d34668025906d55ae2e529615f530a 2006-03-09 00:00:00 2006-09-05 00:00:00
4  00d34668025906d55ae2e529615f530a 2006-03-09 00:00:00 2006-09-05 00:00:00

Does that help any?

Comments