Assume you want to calculate the number of days between the dates, then this is one solution:
import datetime as dt
diff = (pd.to_datetime(df.finish_date) - pd.to_datetime(df.start_date)).dt.days
EDIT
Another alternative is
Start = pd.to_datetime(df.finish_date)
End = pd.to_datetime(df.start_date)
End.subtract(Start)
Example: Here I choose to compute the difference between dates in a df and now.
metric_id device_id timestamp cpu_5min vol_max
0 device_1 2020-12-04 05:15:00 116.0 734.0
1 device_1 2020-12-04 05:30:00 213.0 325.0
2 device_1 2020-12-04 05:35:00 427.0 668.0
3 device_2 2020-12-04 05:15:00 540.0 NaN
4 device_2 2020-12-04 05:30:00 127.0 NaN
5 device_2 2020-12-04 05:35:00 654.0 NaN
and
df['tDATE'] = pd.to_datetime(df['timestamp'])
df['DIFF1'] = (df['tDATE'] - dt.datetime.now()).dt.days #method 11
df['DIFF2'] = df['tDATE'].subtract(dt.datetime.now()) #method2
which returns
metric_id device_id timestamp cpu_5min vol_max \
0 device_1 2020-12-04 05:15:00 116.0 734.0
1 device_1 2020-12-04 05:30:00 213.0 325.0
2 device_1 2020-12-04 05:35:00 427.0 668.0
3 device_2 2020-12-04 05:15:00 540.0 NaN
4 device_2 2020-12-04 05:30:00 127.0 NaN
5 device_2 2020-12-04 05:35:00 654.0 NaN
metric_id tDATE difd DIFF1 DIFF2
0 2020-12-04 05:15:00 -14 -14 -14 days +22:13:26.627607
1 2020-12-04 05:30:00 -14 -14 -14 days +22:28:26.627607
2 2020-12-04 05:35:00 -14 -14 -14 days +22:33:26.627607
3 2020-12-04 05:15:00 -14 -14 -14 days +22:13:26.627607
4 2020-12-04 05:30:00 -14 -14 -14 days +22:28:26.627607
5 2020-12-04 05:35:00 -14 -14 -14 days +22:33:26.627607
EDIT: Working with Timestamps
From your comments below it is now evident that the examples above need preparation since you are working with this Timepstamps. As a note, this is why giving enough information when asking the question is important (e.g. what kind of data you are dealing with). This is even moe important when it comes to dates as there are many formats. Here is an example with the date format you gave in the comments:
import datetime as dt
Date = '2020-09-03T16:18:38.929863799Z'
Date2 = '2020-10-03T16:18:38.929863799Z'
What you have here are Timestamps
, so your first step is to convert them to datetime
and then use to_pydate
(It use to be called Timestamp.to_datetime()
but is now deprecated.
Date = pd.to_datetime(Date)
Date2 = pd.to_datetime(Date2)
DATE_1 = Date.to_pydatetime()
DATE_2 = Date2.to_pydatetime()
after which you can compute the difference
DIFF = (pd.to_datetime(DATE_1) -pd.to_datetime(DATE_2))
which is Timedelta('-30 days +00:00:00')