python - Difference of a date and int column in Pandas -
i have dataframe looks this:
raw_data = {'seriesdate':['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],'test':['1','2','3','4']} import pandas pd df = pd.dataframe(raw_data,columns=['seriesdate','test']) df['seriesdate'] = pd.to_datetime(df['seriesdate'])
i want subtract number field date field arrive @ date, when this:
df['testdate'] = df['seriesdate'] - pd.series((dd) dd in df['test'])
i following typeerror:
typeerror: incompatible type [object] datetime/timedelta operation
any idea how can workaround this?
you can use to_timedelta
:
df['testdate'] = df['seriesdate'] - pd.to_timedelta(df['test'].astype(int), unit="d") print(df) seriesdate test testdate 0 2017-03-10 1 2017-03-09 1 2017-03-13 2 2017-03-11 2 2017-03-14 3 2017-03-11 3 2017-03-15 4 2017-03-11
Comments
Post a Comment