0

I am trying to subtract two timestamps using datetime library of python. Since I don't want date to be included, I removed the date part from below code. Everything comes perfect in the output but at some places output like "-1 day, 23:59:58.233000" is coming. Do anyone have idea how to fix it? I want only time difference not the date. Below attached is the code and its output.

Code:-

d1 = datetime.strptime(c1,"%H:%M:%S.%f")

d2 = datetime.strptime(c2,"%H:%M:%S.%f")

c=(d1-d2)
print(c)

Note: c1 and c2 contains timestamps. Total 37.

Output:-

0:00:03.733000
0:00:03.533000
0:00:03.666000
0:00:05.900000
0:00:04.334000
0:00:03.734000
0:00:04.734000
0:00:04.301000
0:00:10.667000
0:00:13.167000
0:00:11.234000
0:00:08.101000
0:00:10.966000
0:00:02.734000
0:00:03.134000
0:00:06.666000
0:00:06.733000
0:00:02.300000
0:00:00.700000
0:00:00.866000
0:00:02.367000
0:00:02.534000
0:00:01.168000
0:00:00.467000
-1 day, 23:59:55.600000
-1 day, 23:59:57.366000
-1 day, 23:59:58.434000
-1 day, 23:59:58.233000
0:00:00.334000
0:00:04.366000
0:00:01
-1 day, 23:59:58.034000
-1 day, 23:59:57.400000
-1 day, 23:59:55.267000
-1 day, 23:59:54.999000
-1 day, 23:59:53.933000
-1 day, 23:59:55.200000
Epsi95
  • 8,832
  • 1
  • 16
  • 34
sauron_08
  • 37
  • 1
  • 8
  • 1
    your issue coming due to `d1 – Epsi95 Feb 19 '21 at 09:41
  • @Epsi95 is there a way to get that timestamp difference value also? Not in this "-1 day" way, but in a normal way like rest of them, so that I could use that time also in my program. – sauron_08 Feb 19 '21 at 13:34
  • `datetime.strptime` takes a scalar as first argument, not an array or list, thus your code can't give your sample output. Regarding your question, this is just how `timedelta` objects are *displayed*. You can only change that by writing your own converter. – FObersteiner Feb 19 '21 at 13:54
  • @ShikharJaiswal check the answer – Epsi95 Feb 19 '21 at 14:10

2 Answers2

1

you can write your own converter, to get a nice display of timedelta objects. Ex:

from datetime import datetime

d1 = datetime.strptime('12:10:05.1',"%H:%M:%S.%f")
d2 = datetime.strptime('13:10:05.2',"%H:%M:%S.%f")

c=(d1-d2)
print(c)
# -1 day, 22:59:59.900000

def timedeltaToString(td):
    total, prefix = td.total_seconds(), ""
    if total<0: # catch negative timedelta
        total *= -1
        prefix = "-"
    hours, remainder = divmod(total, 3600)
    minutes, seconds = divmod(remainder, 60)
    return f"{prefix}{int(hours):02d}:{int(minutes):02d}:{seconds:09.6f}"

print(timedeltaToString(c))
# -01:00:00.100000

Related: Format timedelta to string

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

In the simplest way you can compare the dates. Suppose you have columns from_date and to_date. if from_date >= to_date then take from_date - to_date otherwise take to_date - from_date.

# assuming you have two columns as from_date and to_date
df['from_date'] = pd.to_datetime(df['from_date'])
df['to_date'] = pd.to_datetime(df['to_date'])


df['date_difference']  = df[['from_date', 'to_date']].apply(lambda x: (x[0]-x[1]) if (x[0]>=x[1]) else (x[1]-x[0]), axis=1)

Once you have got the timedelta, you can get all required information, for example, if you want to get only days then

df['date_difference'].dt.days

Full list found here

Epsi95
  • 8,832
  • 1
  • 16
  • 34
  • Thank you so much bro for this effort. I tried it and it worked. But I just used the above solution as it was pretty convenient to me. – sauron_08 Feb 22 '21 at 13:30