JamesHudson81
JamesHudson81

Reputation: 2273

Trying to subtract a column of dates to another date

I have the date :

dateTo=pd.to_datetime('today').strftime('%Y-%m-%d')

And I would like to subtract dateTo to the column of dates:

       number      dates      coord
AC      10      2018-07-10     11.54
AC      10      2018-07-11     11.19
AN       5      2018-07-12     69.40

The desired output would be a new column df['datepond'] with the output of the subtraction. I would like this output to be integers.

What I tried was a response given here Pandas: Subtracting two date columns and the result being an integer:

df['datepond']=(pd.Timestamp(dateTo)-pd.to_datetime(df['dates']))/ np.timedelta64(1, 'D')

In version numpy 1.12.1 it worked pretty good but im now at numpy 1.15.2 and it outputs

TypeError: data type "datetime" not understood

How could I obtain the desired output?

Upvotes: 2

Views: 231

Answers (1)

jezrael
jezrael

Reputation: 862406

You can try Timestamp.floor for remove times and for convert timedeltas to days Series.dt.days:

df['datepond']= (pd.to_datetime('today').floor('d') - pd.to_datetime(df['dates'])).dt.days
print (df)
    number       dates  coord  datepond
AC      10  2018-07-10  11.54        97
AC      10  2018-07-11  11.19        96
AN       5  2018-07-12  69.40        95

Upvotes: 4

Related Questions