Average of two timestamps in python
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
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