Home > database >  Subtracting the difference between 2 dates Pandas Python
Subtracting the difference between 2 dates Pandas Python

Time:02-21

I want to write piece of code that returns the number of days hours minutes that are differ between values of backtest_results and realtime_test. So I want to perform 2022-01-24 10:05:00 - 2022-01-30 14:09:03, 2022-01-27 01:54:00 - 2022-02-02 09:34:06, backtest_results[0] - realtime_test[0], backtest_results[1] - realtime_test[1] etc.How woudl i be able top code the wanted code below.

import pandas as pd 
import numpy

backtest_results = pd.to_datetime(['2022-01-24 10:05:00', '2022-01-27 01:54:00', 
                     '2022-01-30 19:08:00','2022-02-02 14:32:00',
                     '2022-02-10 02:58:00', '2022-02-10 14:01:00',
                     '2022-02-11 00:25:00' '2022-02-16 13:49:00'])
    
realtime_test = pd.to_datetime([
                '2022-01-30 14:09:03', '2022-02-02 09:34:06',
                '2022-02-08 07:37:03', '2022-02-09 22:07:02',
                '2022-02-10 09:02:03', '2022-02-10 19:32:25',
                '2022-02-12 16:42:03', '2022-02-15 23:19:03'])
result = backtest_results - realtime_test

CodePudding user response:

You're missing a comma in backtest_results in the last row:

backtest_results = pd.to_datetime(['2022-01-24 10:05:00', '2022-01-27 01:54:00', 
                     '2022-01-30 19:08:00','2022-02-02 14:32:00',
                     '2022-02-10 02:58:00', '2022-02-10 14:01:00',
                     '2022-02-11 00:25:00', '2022-02-16 13:49:00'])
                                          ^^ here

Then, you can simply subtract one from the other.

If you want the raw difference:

>>> backtest_results - realtime_test
TimedeltaIndex(['-7 days  19:55:57', '-7 days  16:19:54', '-9 days  11:30:57',
                '-8 days  16:24:58', '-1 days  17:55:57', '-1 days  18:28:35',
                '-2 days  07:42:57',   '0 days 14:29:57'],
               dtype='timedelta64[ns]', freq=None)

If you want to get the difference is days:

>>> (backtest_results - realtime_test).astype('timedelta64[D]')
Float64Index([-7.0, -7.0, -9.0, -8.0, -1.0, -1.0, -2.0, 0.0], dtype='float64')

If you want the difference in hours:

>>> (backtest_results - realtime_test).astype('timedelta64[h]')
Float64Index([-149.0, -152.0, -205.0, -176.0, -7.0, -6.0, -41.0, 14.0], dtype='float64')

If you want the difference in minutes:

>>> (backtest_results - realtime_test).astype('timedelta64[m]')
Float64Index([-8885.0, -9101.0, -12270.0, -10536.0, -365.0, -332.0, -2418.0, 869.0], dtype='float64')

CodePudding user response:

df['diff'] = df.EndDate - df.StartDtate
df.diff = df.diff / np.timedelta64(1. 'D')

‘D’ for day, ‘W’ for weeks, ‘M’ for month, ‘Y’ for year

  • Related