简体   繁体   中英

Time-weighted average with Pandas

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM