0
 #Under d.types i can confirm they are both datetime objects
 date1      datetime64[ns]
 date2      datetime64[ns]

enter image description here

 df_test['snaptoexpectedStart'] = df['date1'] - df['date2']
 TypeError: '<' not supported between instances of 'str' and 'int'

I dont understand why I'm getting that error when both the columns im trying to subtract are in the correct format.

Zee
  • 81
  • 1
  • 8
  • Does this answer your question? [Pandas: Subtracting two date columns and the result being an integer](https://stackoverflow.com/questions/37840812/pandas-subtracting-two-date-columns-and-the-result-being-an-integer) – PacketLoss May 14 '20 at 21:57
  • That is what im currently trying :( – Zee May 14 '20 at 22:03

2 Answers2

0

I guess it has something to do with the datetime format I suppose, try casting this way to see if it works :

from datetime import datetime
df_test['snaptoexpectedStart'] = datetime(df['date1']) - datetime(df['date2'])

If you are looking to get the number of days only than try this :

df_test['snaptoexpectedStart'] = (df_test['date1'] - df_test['date2Date']).dt.days
Umar Aftab
  • 527
  • 4
  • 24
0

You might want to look into the timedelta class: According to the API, subtracting two datetimes (assuming they are datetime.datetimes) results in a timedelta object. You can then use the .day attribute of the timedelta to get the difference in days.

BaconSodas
  • 74
  • 3