12

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.

Karel Macek
  • 1,119
  • 2
  • 11
  • 24

5 Answers5

13

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
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
10

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
Seif
  • 1,058
  • 11
  • 19
4

This method gave the same result as others:

t1 = Timestamp('2017-01-18 10:00:00.0000000')
t2 = Timestamp('2017-01-20 10:00:00.0000000')
average = Timestamp((t1.value + t2.value)/2.0)
niraj
  • 17,498
  • 4
  • 33
  • 48
3

This is how I take the median of 2 timestamps:

ts1 = pd.Timestamp('2016-1-18 10:00')

ts2 = pd.Timestamp('2016-1-18 10:20')

ts1+(ts2-ts1)/2
Out[11]: Timestamp('2016-01-18 10:10:00')

No need to test if ts2 is greater than ts2 as the equation is symmetrical.

Zeugma
  • 31,231
  • 9
  • 69
  • 81
3

Preparing a sample dataframe (2 or more timestamps to average on):

# Initiate dataframe
date_var = "date"
df = pd.DataFrame(data=[['A', '2018-08-05 17:06:01'],
                        ['A', '2018-08-05 17:06:02'],
                        ['A', '2018-08-05 17:06:03'],
                        ['B', '2018-08-05 17:06:07'],
                        ['B', '2018-08-05 17:06:09'],
                        ['B', '2018-08-05 17:06:11']],
                  columns=['column', date_var])

# Convert date-column to proper pandas Datetime-values/pd.Timestamps
df[date_var] = pd.to_datetime(df[date_var])

Extraction of the desired average Timestamp-value:

# Extract the numeric value associated to each timestamp (epoch time)
# NOTE: this is being accomplished via accessing the .value - attribute of each Timestamp in the column
In:
[tsp.value for tsp in df[date_var]]
Out:
[
    1533488761000000000, 1533488762000000000, 1533488763000000000,
    1533488767000000000, 1533488769000000000, 1533488771000000000
]

# Use this to calculate the mean, then convert the result back to a timestamp
In:
pd.Timestamp(np.nanmean([tsp.value for tsp in df[date_var]]))
Out:
Timestamp('2018-08-05 17:06:05.500000')
Andreas L.
  • 3,239
  • 5
  • 26
  • 65