简体   繁体   中英

Sum date with integer in Python 3.x

I have a dataframe, in which I want to create a new column which will sum two columns:

   index  Date          num  
0      0  2004-03-09      1  
1      1  2004-03-09      2
2      2  2004-03-09   -300
3      3  2004-03-09     -5
4      4  2004-03-09      3

Date is in date format and num is integer. The output should look like this:

    index Date          num  Date_2 
0      0  2004-03-09      1  2004-03-10
1      1  2004-03-09      2  2004-03-11
2      2  2004-03-09   -300  2003-05-14
3      3  2004-12-09     -5  2003-12-04
4      4  2004-02-09      3  2004-02-12
5      5  2004-05-09      3  2004-05-12

This was very straightforward to do in R, but since I have just started with Python I cannot figure it out. (Of course when I try to sum the columns I get an error that they are incompatible for operation)

[In]: df['Date_2'] = df['Date'] + df['num']

[Out]: incompatible type for a datetime/timedelta operation [__add__]

I tried converting the 'num' column in days, but I was unsuccessful.

In [277]: df['Date2'] = df.apply(lambda x: x.Date + np.timedelta64(x.num, 'D'), 
     ...: axis=1)

In [278]: df
Out[278]: 
   index       Date  num      Date2
0      0 2004-03-09    1 2004-03-10
1      1 2004-03-09    2 2004-03-11
2      2 2004-03-09 -300 2003-05-14
3      3 2004-03-09   -5 2004-03-04
4      4 2004-03-09    3 2004-03-12

incompatible type ... [__add__] means that the two objects you are trying to add do not implement the __add__ magic method in such a way that they handle each others object types.

You need a timedelta-related object that can be created from ints from your integer column that is compatible with the dates in your dataframe.

Assuming you are using Pandas dataframes, Pandas has a Timedelta scalar type that you can create from an int like so: Timedelta(df['num'], unit='d')

You can use datetime.timedelta(days= num ). Like below:

df['Date_2'] = df['Date'] + datetime.timedelta(days=df['num'])

Simple approach using pd.to_datetime and pd.to_timedelta .

df['date2']=pd.to_datetime(df['date'])+pd.to_timedelta(df['num'].astype(np.int),'D')

May this one help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM