-1

I have got a data frame with diverse data but two columns contained dates (date of admission and date of discharge). The format of these dates is xxxx-xx-xx 00:00:00. I want to do some calculations such as subtraction. The code I used was

covid_19_admission['days_Hospitalised'] = (covid_19_admission['Discharge_date'] - covid_19_admission['Date']).dt.days 

# I expected a new column with the days.

But I got the error TypeError: unsupported operand type(s) for -: 'str' and 'str'.

I am not interested in hours, minutes or seconds so I have tried to remove these and understand the format but when I write


covid_19_admission.dtypes
Patient_admitted_id     int64
Date                   object
Hospital_ID             int64
Discharge_date         object
dtype: object

I am new working on dates so I dont know how to do this.

1 Answers1

1

Convert both columns to datetimes:

covid_19_admission['days_Hospitalised'] = (pd.to_datetime(covid_19_admission['Discharge_date']) - (pd.to_datetime(covid_19_admission['Date']))).dt.days
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252