swysell
swysell

Reputation: 107

Adding to date in pandas dataframe with other dataframe value

I want to increase the date in one dataframe column by an integer value in another.

I receive TypeError: unsupported type for timedelta days component: numpy.int64

My dataframes look like this:

import pandas as pd
import numpy as np
import datetime as dt

dfa = pd.DataFrame([
    ['5/15/17',1],
    ['5/15/17',1]],
    columns = ['Start','Days'])

dfb = pd.DataFrame([
    ['5/15/17',1],
    ['5/15/17',1]],
    columns = ['Start','Days'])

I format the 'Start' column to datetime with this code:

dfa['Start'] = dfa['Start'].apply(lambda x: 
                                    dt.datetime.strptime(x,'%m/%d/%y'))
dfb['Start'] = dfb['Start'].apply(lambda x: 
                                    dt.datetime.strptime(x,'%m/%d/%y'))

I try to change the values in the dfa dataframe. The dfb dataframe reference works for 'Days' but not for 'Start':

for i, row in dfb.iterrows():
    for j, row in dfa.iterrows():
        new = pd.DataFrame({"Start": dfa.loc[j,"Start"] + datetime.timedelta(days=dfb.loc[i,"Days"]), "Days": dfa.loc[j,"Days"] - dfb.loc[i,"Days"]}, index = [j+1])
        dfa = pd.concat([dfa.ix[:j], new, dfa.ix[j+1:]]).reset_index(drop=True)

This is the key component that raises the error:

"Start": dfa.loc[j,"Start"] + datetime.timedelta(days=dfb.loc[i,"Days"]

It works fine if I use:

"Start": dfa.loc[j,"Start"] + datetime.timedelta(days=1)

but I need it to be taking that value from dfb, not a static integer.

Upvotes: 1

Views: 593

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

IIUC (I changed the input values a bit to clarify what is going on):

import pandas as pd

dfa = pd.DataFrame([
    ['5/15/17',1],
    ['5/16/17',1]],
    columns = ['Start','Days'])

dfb = pd.DataFrame([
    ['5/15/17',3],
    ['5/16/17',4]],
    columns = ['Start','Days'])

dfa['Start'] = pd.to_datetime(dfa['Start'])

dfb['Start'] = pd.to_datetime(dfb['Start'])

dfa['Start'] = dfa['Start'] + dfb['Days'].apply(pd.Timedelta,unit='D')
print(dfa)

Output:

       Start  Days
0 2017-05-18     1
1 2017-05-20     1

Upvotes: 1

Related Questions