0

Datatype of two objects

df_patients['date_of_diagnosis_new'] = pd.to_datetime(df_patients['date_of_diagnosis'])

Tried following code but got an error this error "AttributeError: type object 'datetime.datetime' has no attribute 'datetime'"

df_patients['date'] = df_patients['date_of_diagnosis'].apply(lambda x: datetime.datetime.strptime(x, '%Y%m%d'))

Imported libraries: import datetime from datetime import datetime, timedelta

Original Format  Desired Format
3/27/2010        2010-03-27
3/2/2011         2011-03-02
7/20/2009        2009-07-20
Anita
  • 1
  • use `df_patients['date_of_diagnosis_new'] = pd.to_datetime(df_patients['date_of_diagnosis']).dt.date` – jezrael Apr 08 '18 at 07:10
  • Now the output is in 'datetime.date(2012, 1, 1)' format. I want it in '2010-03-27' so that I can find out the difference between two dates in terms of days. There are two date columns and I need to find the difference between them. I'm able to calculate the difference with the timestamp '196 days 00:00:00' I just need to remove the timestamp now – Anita Apr 08 '18 at 07:28
  • then use [this](https://stackoverflow.com/a/41718815/2901002) – jezrael Apr 08 '18 at 07:30
  • Still not able to get the difference 'df_patients['date_of_diagnosis_new'] = pd.to_datetime(df_patients['date_of_diagnosis']).dt.date df_patients['date_of_final_obs_new'] = pd.to_datetime(df_patients['date_of_final_obs']).dt.date df_time['time_to_death'] = (df_time['date_of_final_obs_new'] - df_time['date_of_diagnosis_new'])' – Anita Apr 08 '18 at 07:40
  • what is your code which does not work? – jezrael Apr 08 '18 at 07:41
  • `df_time = df_patients.copy() df_time['date_of_diagnosis'] = pd.to_datetime(df_patients['date_of_diagnosis']) df_time['date_of_final_obs'] = pd.to_datetime(df_patients['date_of_final_obs']) df_time['time_to_death'] = (df_time['date_of_final_obs'] - df_time['date_of_diagnosis']) print("Median time of death:",df_time['time_to_death'].median())` – Anita Apr 08 '18 at 07:46
  • The output is **Median time of death: 196 days 00:00:00** I want it to be just **196** days in integer – Anita Apr 08 '18 at 07:47
  • Use `print("Median time of death:",df_time['time_to_death'].median().days) ` – jezrael Apr 08 '18 at 07:48
  • Thanks! that worked. And what if I want to get the number of days before calculating the median. That is the difference between two dates in term of days in an integer type? – Anita Apr 08 '18 at 07:56

0 Answers0