I have two timestamps (pandas.tslib.Timestamp) ts1
and ts2
and I want to calculate the average of them.
(ts1+ts2)/2
However, I am getting this error:
TypeError: unsupported operand type(s) for +: 'Timestamp' and 'Timestamp'
Thank you for any help.
subtracting a timestamp from another generates an interval, which may then be divided.
as the error says, adding timestamps is not allowed.
the solution involves calculating the interval, halving the interval and then adding the halved interval to the earlier timestamp or subtracting from the later timestamp.
from pandas.tslib import Timestamp
d1 = Timestamp.now()
# wait a few seconds
d2 = Timestamp.now()
d3 = d1 + (d2 - d1) / 2
# d3 will be the timestamp exactly in between d1 & d2
Objects similar to datetime.datetime
objects do not support addition as well, because there is no meaning in adding dates. You should use datetime.timedelta
to get the average time.
How ? This way:
average_delta = (ts2 - ts1) / 2
average_ts = ts1 + average_delta
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With