10

What's the most efficient way to calculate the time-weighted average of a TimeSeries in Pandas 0.8? For example, say I want the time-weighted average of df.y - df.x as created below:

import pandas
import numpy as np
times = np.datetime64('2012-05-31 14:00') + np.timedelta64(1, 'ms') * np.cumsum(10**3 * np.random.exponential(size=10**6))
x = np.random.normal(size=10**6)
y = np.random.normal(size=10**6)
df = pandas.DataFrame({'x': x, 'y': y}, index=times)

I feel like this operation should be very easy to do, but everything I've tried involves several messy and slow type conversions.

user2303
  • 1,213
  • 10
  • 11

1 Answers1

7

You can convert df.index to integers and use that to compute the average. There is a shortcut asi8 property that returns an array of int64 values:

np.average(df.y - df.x, weights=df.index.asi8)
Wes McKinney
  • 101,437
  • 32
  • 142
  • 108
  • 6
    Thanks! I want to weight the values by the time durations, so I used `np.average((df.y - df.x)[:-1], weights=np.diff(df.index.asi8))` – user2303 Jun 01 '12 at 19:44