0

I am trying to add more than two timestamp values and I expect to see output in minutes/seconds. How can I add two timestamps? I basically want to do: '1995-07-01 00:00:01' + '1995-07-01 00:05:06' and see if total time>=60minutes. I tried this code: df['timestamp'][0]+df['timestamp'][1]. I referred this post but my timestamps are coming from dataframe. Head of my dataframe column looks like this:

0   1995-07-01 00:00:01
1   1995-07-01 00:00:06
2   1995-07-01 00:00:09
3   1995-07-01 00:00:09
4   1995-07-01 00:00:09
Name: timestamp, dtype: datetime64[ns]

I am getting this error: TypeError: unsupported operand type(s) for +: 'Timestamp' and 'Timestamp'

Community
  • 1
  • 1
jubins
  • 317
  • 2
  • 7
  • 18
  • 2
    How do you expect to add two timestamps? If you add Tuesday and Wednesday, does that make Friday? Please show your expected output for given input. – Stephen Rauch Apr 02 '17 at 00:53
  • I want to add two timestamps '1995-07-01 00:00:01' + '1995-07-01 00:59:59' to see the output like this 60minutes. – jubins Apr 02 '17 at 01:00
  • OK, so you want some sort of duration? Duration from when? – Stephen Rauch Apr 02 '17 at 01:02
  • yes, I have dataframe of IP addresses and timestamps. I want to know total duration for each IP address. I am trying to calculate this by adding all timestamp values for each unique IP address. – jubins Apr 02 '17 at 01:04
  • You missed the important part of the question. Duration from when? – Stephen Rauch Apr 02 '17 at 01:11
  • I am not trying to subtract. If you see head of my dataframe I am trying to add time values to calculate total duration. – jubins Apr 02 '17 at 01:15
  • Sigh, I will go back to my first question. If you add Tuesday and Wednesday, does that make Friday? – Stephen Rauch Apr 02 '17 at 01:20
  • No it won't make Friday. I'm trying my best to make it clear but I'm not sure what is missing to make you understand. I hope to get solution for this soon. I am stuck on it since 3 hours. – jubins Apr 02 '17 at 01:29
  • 1
    A duration always requires a subtraction if you have a timestamp. Because a duration has a beginning and an end. You are trying to add Tuesday and Wednesday. – Stephen Rauch Apr 02 '17 at 01:32

2 Answers2

3

The problem is that adding Timestamps makes no sense. What if they were on different days? What you want are the sum of Timedeltas. We can create Timedeltas by subtracting a common date from the whole series. Let's subtract the minimum date. Then sum up the Timedeltas. Let s be your series of Timestamps

s.sub(s.dt.date.min()).sum().total_seconds()

34.0
piRSquared
  • 285,575
  • 57
  • 475
  • 624
2
#Adding two timestamps is not supported and not logical
#Probably, you really want to add the time rather than the timestamp itself
#This is how to extract the time from the timestamp then summing it up

import datetime
import time

t = ['1995-07-01 00:00:01','1995-07-01 00:00:06','1995-07-01 00:00:09','1995-07-01 00:00:09','1995-07-01 00:00:09']
tSum = datetime.timedelta()
df = pd.DataFrame(t, columns=['timestamp'])
for i in range(len(df)):
    df['timestamp'][i] = datetime.datetime.strptime(df['timestamp'][i], "%Y-%m-%d %H:%M:%S").time()
    dt=df['timestamp'][i]
    (hr, mi, sec) = (dt.hour, dt.minute, dt.second)
    sum = datetime.timedelta(hours=int(hr), minutes=int(mi),seconds=int(sec))
    tSum += sum
if tSum.seconds >= 60*60:
    print("more than 1 hour")
else:
    print("less than 1 hour")
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38