我有时间序列“半小时”数据。我需要在重新采样期间demand
使用加权平均值(使用price
)重新采样到“1 天”。
dft
demand price
2012-01-01 00:00:00 30940.500000 42.18
2012-01-01 00:30:00 31189.166667 43.48
2012-01-01 01:00:00 30873.166667 42.28
2012-01-01 01:30:00 30110.833333 38.48
2012-01-01 02:00:00 29721.500000 37.28
2012-01-01 02:30:00 28970.000000 36.24
2012-01-01 03:00:00 27955.000000 32.16
... ...
2014-12-30 20:30:00 41685.500000 40.51
2014-12-30 21:00:00 40177.833333 41.79
2014-12-30 21:30:00 38238.000000 31.50
2014-12-30 22:00:00 36395.333333 37.54
2014-12-30 22:30:00 34543.333333 39.55
2014-12-30 23:00:00 32652.000000 40.88
2014-12-30 23:30:00 30941.333333 38.16
我想重新采样demand
到1D
(1天),使用price
列作为权重,使用np.average()
我看过一些例子,但有些东西不是很点击。我得到的最接近的是:
dftwei = dft.price.resample('1D').apply(lambda x: np.average(x, weights=dft.demand, axis=0))
但问题是这给出了:
ValueError: Length of weights not compatible with specified axis.
axis=0
未指定时,错误为:
TypeError: Axis must be specified when shapes of a and weights differ.
问题可能在于如何weights
指定。权重的长度必须为 48,但我怀疑 lambda 函数使用的是price
. 谢谢!