4

I have a dataframe with a date column and then a number of days that I want to add to that column. I want to create a new column, 'Recency_Date', with the resulting value.

df:

 fan Community Name Count Mean_Days Date_Min 0 855 AAA Games 6 353 2013-04-16 1 855 First Person Shooters 2 420 2012-10-16 2 855 Playstation 3 108 2014-06-12 3 3148 AAA Games 1 0 2015-04-17 4 3148 Mobile Gaming 1 0 2013-01-19 

df info:

merged.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 4627415 entries, 0 to 4627414 Data columns (total 5 columns): fan int64 Community Name object Count int64 Mean_Days int32 Date_Min datetime64[ns] dtypes: datetime64[ns](1), int32(1), int64(2), object(1) memory usage: 194.2+ MB 

Sample data as csv:

fan,Community Name,Count,Mean_Days,Date_Min 855,AAA Games,6,353,2013-04-16 00:00:00 855,First Person Shooters,2,420,2012-10-16 00:00:00 855,Playstation,3,108,2014-06-12 00:00:00 3148,AAA Games,1,0,2015-04-17 00:00:00 3148,Mobile Gaming,1,0,2013-01-19 00:00:00 3148,Power PCs,2,0,2014-06-17 00:00:00 3148,XBOX,1,0,2009-11-12 00:00:00 3860,AAA Games,1,0,2012-11-28 00:00:00 3860,Minecraft,3,393,2011-09-07 00:00:00 4044,AAA Games,5,338,2010-11-15 00:00:00 4044,Blizzard Games,1,0,2013-07-12 00:00:00 4044,Geek Culture,1,0,2011-06-03 00:00:00 4044,Indie Games,2,112,2013-01-09 00:00:00 4044,Minecraft,1,0,2014-01-02 00:00:00 4044,Professional Gaming,1,0,2014-01-02 00:00:00 4044,XBOX,2,785,2010-11-15 00:00:00 4827,AAA Games,1,0,2010-08-24 00:00:00 4827,Gaming Humour,1,0,2012-05-05 00:00:00 4827,Minecraft,2,10,2012-03-21 00:00:00 5260,AAA Games,4,27,2013-09-17 00:00:00 5260,Indie Games,8,844,2011-06-08 00:00:00 5260,MOBA,2,0,2012-10-27 00:00:00 5260,Minecraft,5,106,2012-02-17 00:00:00 5260,XBOX,1,0,2011-06-15 00:00:00 5484,AAA Games,21,1296,2009-08-01 00:00:00 5484,Free to Play,1,0,2014-12-08 00:00:00 5484,Indie Games,1,0,2014-05-28 00:00:00 5484,Music Games,1,0,2012-09-12 00:00:00 5484,Playstation,1,0,2012-02-22 00:00:00 

I've tried:

merged['Recency_Date'] = merged['Date_Min'] + timedelta(days=merged['Mean_Days']) 

and:

merged['Recency_Date'] = pd.DatetimeIndex(merged['Date_Min']) + pd.DateOffset(merged['Mean_Days']) 

But am having trouble finding something that will work for a Series rather than an individual int value. Any and all help would be very much appreciated with this.

1
  • You'll need to post more info, preferable raw input data, code to reproduce your df and desired output, also can you post the output from df.info() if column 'Date_Min' is already datetime dtype then pd.TimedeltaIndex(merged['Mean_Days'], unit='D') will construct a time delta index that you can use to offset 'Date_Min' column Commented Sep 15, 2015 at 12:58

1 Answer 1

2

If 'Date_Min' dtype is already datetime then you can construct a Timedeltaindex from your 'Mean_Days' column and add these:

In [174]: df = pd.DataFrame({'Date_Min':[dt.datetime.now(), dt.datetime(2015,3,4), dt.datetime(2011,6,9)], 'Mean_Days':[1,2,3]}) df Out[174]: Date_Min Mean_Days 0 2015-09-15 14:02:37.452369 1 1 2015-03-04 00:00:00.000000 2 2 2011-06-09 00:00:00.000000 3 In [175]: df['Date_Min'] + pd.TimedeltaIndex(df['Mean_Days'], unit='D') Out[175]: 0 2015-09-16 14:02:37.452369 1 2015-03-06 00:00:00.000000 2 2011-06-12 00:00:00.000000 Name: Date_Min, dtype: datetime64[ns] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked perfectly, exactly what I was looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.