Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift dates in a pandas dataframe (add x months)?

I have a dataframe with columns of dates.

I know how to shift dates by a fixed number of months (eg add 3 months to all the dates in column x); however, I cannot figure out how to shift dates by a number of months which is not fixed, but is another column of the dataframe.

Any ideas?

I have copied a minimal example below. The error I get is:

The truth value of a Series is ambiguous

Thanks a lot!

import pandas as pd
import numpy as np
import datetime

df = pd.DataFrame()
df['year'] = np.arange(2000,2010)
df['month'] = 3

df['mydate'] = pd.to_datetime(  (df.year * 10000 + df.month * 100 +1).apply(str), format='%Y%m%d')
df['month shift'] = np.arange(0,10)

# if I want to shift mydate by 3 months, I can convert it to DatetimeIndex and use dateOffset:
df['my date shifted by 3 months'] = pd.DatetimeIndex( df['mydate'] ) + pd.DateOffset(months = 3)

# however, how do I shift mydate by the number of months in the column 'month shift'?
#This does NOT work:
df['my date shifted'] = pd.DatetimeIndex( df['mydate'] ) + pd.DateOffset(months = df['month shift'])
print df
like image 833
Pythonista anonymous Avatar asked Mar 13 '23 07:03

Pythonista anonymous


2 Answers

IIUC you could use apply with axis=1:

In [23]: df.apply(lambda x: x['mydate'] + pd.DateOffset(months = x['month shift']), axis=1)
Out[23]:
0   2000-03-01
1   2001-04-01
2   2002-05-01
3   2003-06-01
4   2004-07-01
5   2005-08-01
6   2006-09-01
7   2007-10-01
8   2008-11-01
9   2009-12-01
dtype: datetime64[ns]
like image 137
Anton Protopopov Avatar answered Apr 01 '23 20:04

Anton Protopopov


"one"-liner using the underlying numpy functionality:

df['my date shifted'] = (
    df["mydate"].values.astype("datetime64[M]") 
    + df["month shift"].values.astype("timedelta64[M]")
)
like image 38
MikeGM Avatar answered Apr 01 '23 21:04

MikeGM