Python 熊猫重采样应用np.average
我有时间序列“半小时”数据。我需要在重采样期间使用加权平均(使用Python 熊猫重采样应用np.average,python,pandas,resampling,pandas-apply,Python,Pandas,Resampling,Pandas Apply,我有时间序列“半小时”数据。我需要在重采样期间使用加权平均(使用价格)将需求重采样到“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
价格
)将需求
重采样到“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
我想使用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.
当未指定轴=0时,错误为:
TypeError: Axis must be specified when shapes of a and weights differ.
问题可能在于如何指定权重。权重的长度必须为48,但我怀疑lambda函数使用的是price
的全长。
谢谢 您可以创建自己的加权平均值:
wp = (df['demand'] * df['price']).resample('H').sum()
wp / df.resample('H')['price'].sum()
2012-01-01 00:00:00 31066.720251
2012-01-01 01:00:00 30509.935034
2012-01-01 02:00:00 29351.065288
2012-01-01 03:00:00 27558.233718
...
您可以创建自己的加权平均值:
wp = (df['demand'] * df['price']).resample('H').sum()
wp / df.resample('H')['price'].sum()
2012-01-01 00:00:00 31066.720251
2012-01-01 01:00:00 30509.935034
2012-01-01 02:00:00 29351.065288
2012-01-01 03:00:00 27558.233718
...
看来你的子集只差一点点。要在一天内进行平均,请对整个数据帧重新采样,然后仅在一天内进行平均:
import pandas as pd
import numpy as np
df = pd.DataFrame([('2012-01-01 00:00', 30940.500000, 42.18),
('2012-01-01 00:30', 31189.166667, 43.48),
('2012-01-01 01:00', 30873.166667, 42.28),
('2012-01-01 01:30', 30110.833333, 38.48),
('2012-01-01 02:00', 29721.500000, 37.28),
('2012-01-01 02:30', 28970.000000, 36.24),
('2012-01-01 03:00', 27955.000000, 32.16),
('2012-01-02 20:30', 41685.500000, 40.51),
('2012-01-02 21:00', 40177.833333, 41.79),
('2012-01-02 21:30', 38238.000000, 31.50),
('2012-01-02 22:00', 36395.333333, 37.54),
('2012-01-02 22:30', 34543.333333, 39.55),
('2012-01-02 23:00', 32652.000000, 40.88),
('2012-01-02 23:30', 30941.333333, 38.16)])
df[0] = pd.to_datetime(df[0])
df.set_axis(['date', 'demand', 'price'], axis=1, inplace=True)
df.set_index('date', inplace=True)
#
# Above is just setup, here's the rub:
#
df.resample('1D').apply(lambda x: np.average(x.demand, weights=x.price))
看来你的子集只差一点点。要在一天内进行平均,请对整个数据帧重新采样,然后仅在一天内进行平均:
import pandas as pd
import numpy as np
df = pd.DataFrame([('2012-01-01 00:00', 30940.500000, 42.18),
('2012-01-01 00:30', 31189.166667, 43.48),
('2012-01-01 01:00', 30873.166667, 42.28),
('2012-01-01 01:30', 30110.833333, 38.48),
('2012-01-01 02:00', 29721.500000, 37.28),
('2012-01-01 02:30', 28970.000000, 36.24),
('2012-01-01 03:00', 27955.000000, 32.16),
('2012-01-02 20:30', 41685.500000, 40.51),
('2012-01-02 21:00', 40177.833333, 41.79),
('2012-01-02 21:30', 38238.000000, 31.50),
('2012-01-02 22:00', 36395.333333, 37.54),
('2012-01-02 22:30', 34543.333333, 39.55),
('2012-01-02 23:00', 32652.000000, 40.88),
('2012-01-02 23:30', 30941.333333, 38.16)])
df[0] = pd.to_datetime(df[0])
df.set_axis(['date', 'demand', 'price'], axis=1, inplace=True)
df.set_index('date', inplace=True)
#
# Above is just setup, here's the rub:
#
df.resample('1D').apply(lambda x: np.average(x.demand, weights=x.price))
感谢,就像在下面的@ayhan中一样,我想你想用x['price'].*x['demand'].]除,而不是用x['price'.]除,而不是用'demand'df.groupby(df.index.date).应用(lambda x:sum(x['price'.]x['demand'.])谢谢,就像在下面的@ayhan中,我想你想用x['price'.]除,而不是用'demand'.]除,谢谢。我已经检查过了,这似乎有效。还有兴趣看看是否有人知道
.apply(np.average())
的正确语法(请其他人注意):虽然我问的问题是1D重采样,这个将2个半小时聚合为1小时的解决方案很好,因为它便于手动检查。@dreab啊,是的,我实际上更改了它,以便确认结果,但在粘贴到此处时忘记更改它。谢谢。我已经检查过了,这似乎有效。还有兴趣看看是否有人知道.apply(np.average())
的正确语法(请其他人注意):虽然我问的问题是1D重采样,这个将2个半小时聚合为1小时的解决方案很好,因为它便于手动检查。@dreab啊,是的,我实际上更改了它,以便确认结果,但在粘贴到此处时忘记更改它。谢谢@heath raftery。我只是检查一下。你用的是什么版本的熊猫?谢谢@Heath Raftery。我的数据帧也是这样-但由于pandas 0.20稍旧,本例中的set_axis
语法会抛出错误。因此,关键是x.demand
而不仅仅是x
在np.average
@dreab是的,这让我沮丧了好几年——我最终升级到了最新版本(0.22.0),但当我在0.20时,我不得不使用设置轴(1,['date','demand','price'])
(前两个参数的顺序互换).熊猫们偷偷地改变,这样它就可以保持排行榜的首位:pThanks@heath raftery。我只是检查一下。你用的是什么版本的熊猫?谢谢@Heath Raftery。我的数据帧也是这样-但由于pandas 0.20稍旧,本例中的set_axis
语法会抛出错误。因此,关键是x.demand
而不仅仅是x
在np.average
@dreab是的,这让我沮丧了好几年——我最终升级到了最新版本(0.22.0),但当我在0.20时,我不得不使用设置轴(1,['date','demand','price'])
(前两个参数的顺序互换).熊猫们偷偷摸摸地做出改变,这样它就可以保持排行榜的首位:p