1

I need to calculate the difference between two columns of type datetime, and the result must be in days (integer format). However, what I am getting is the result in day / month / year hour and minute.

    id                  date_1      date_2              date_3                  date_result_2-1     date_result_3-1
0   C_ID_92a2005557     2017-06-01  2017-06-27 14:18:08 2018-04-29 11:23:05     26 days 14:18:08    332 days 11:23:05
1   C_ID_3d0044924f     2017-01-01  2017-01-06 16:29:42 2018-03-30 06:48:26     5 days 16:29:42     453 days 06:48:26
2   C_ID_d639edf6cd     2016-08-01  2017-01-11 08:21:22 2018-04-28 17:43:11     163 days 08:21:22   635 days 17:43:11
3   C_ID_186d6a6901     2017-09-01  2017-09-26 16:22:21 2018-04-18 11:00:11     25 days 16:22:21    229 days 11:00:11
4   C_ID_cdbd2c0db2     2017-11-01  2017-11-12 00:00:00 2018-04-28 18:50:25     11 days 00:00:00    178 days 18:50:25

The last two columns are the result that I obtained with the simple subtraction between two columns. I would like these columns to be in full format, containing only the number of days.

I tried to convert with astype (int) but I got a result that I could not understand.

Any suggestion? Thank you very much in advance.

Ângelo
  • 149
  • 1
  • 13

1 Answers1

2

if you need only days try this:

df = pd.DataFrame(data={"date":['2000-05-07','1965-01-30','NaT'],
                   "date_2":["2019-01-19 12:26:00","2019-03-21 02:23:12", "2018-11-02 18:30:10"]})


df['date'] = pd.to_datetime(df['date']).dt.date
df['date_2'] = pd.to_datetime(df['date_2']).dt.date

df['days'] = (df['date']-df['date_2']).dt.days

tawab_shakeel
  • 3,701
  • 10
  • 26