python – 使每日pandas DataFrame接收相同的Weekly(重新采样)DataFrame值
作者:互联网
鉴于以下每日价格DataFrame:
open high low close volume
date
2017-11-01 44.66 44.75 43.56 43.56 1000
2017-11-03 43.56 43.74 42.19 42.93 2500
2017-11-06 43.15 43.43 42.45 42.66 2000
2017-11-07 42.40 42.70 41.19 42.25 1500
2017-11-08 42.50 43.50 41.77 43.26 200
2017-11-09 43.46 43.46 41.94 43.00 5000
2017-11-10 43.75 43.75 40.60 41.02 500
2017-11-13 41.60 42.01 40.03 41.90 125
2017-11-14 42.05 43.21 41.67 41.90 1000
2017-11-16 41.98 42.48 41.63 41.96 1200
2017-11-17 41.87 42.69 41.71 42.36 1250
2017-11-21 42.70 43.10 42.15 42.30 800
2017-11-22 42.30 42.38 40.92 41.19 300
2017-11-23 41.11 41.69 40.96 41.21 0
2017-11-24 41.26 41.40 40.35 40.37 2000
2017-11-27 40.28 40.36 39.10 39.80 3000
2017-11-28 40.23 40.40 39.50 40.04 500
我重新采样到Weekly DataFrame(使用本文末尾提供的函数):
open high low close volume
date
2017-10-30 44.66 44.75 42.19 42.93 3500
2017-11-06 43.15 43.75 40.60 41.02 9200
2017-11-13 41.60 43.21 40.03 42.36 3575
2017-11-20 42.70 43.10 40.35 40.37 3100
2017-11-27 40.28 40.40 39.10 40.04 3500
我希望我可以使用Weekly中的数据“重新采样”Daily DataFrame.它应该如下所示:
open high low close volume
date
2017-11-01 44.66 44.75 42.19 42.93 3500
2017-11-03 44.66 44.75 42.19 42.93 3500
2017-11-06 43.15 43.75 40.60 41.02 9200
2017-11-07 43.15 43.75 40.60 41.02 9200
2017-11-08 43.15 43.75 40.60 41.02 9200
2017-11-09 43.15 43.75 40.60 41.02 9200
2017-11-10 43.15 43.75 40.60 41.02 9200
2017-11-13 41.60 43.21 40.03 42.36 3575
2017-11-14 41.60 43.21 40.03 42.36 3575
2017-11-16 41.60 43.21 40.03 42.36 3575
2017-11-17 41.60 43.21 40.03 42.36 3575
2017-11-21 42.70 43.10 40.35 40.37 3100
2017-11-22 42.70 43.10 40.35 40.37 3100
2017-11-23 42.70 43.10 40.35 40.37 3100
2017-11-24 42.70 43.10 40.35 40.37 3100
2017-11-27 40.28 40.40 39.10 40.04 3500
2017-11-28 40.28 40.40 39.10 40.04 3500
如果它有帮助,这是我用来制作每周(第二)数据帧的函数:
def sampleWeekly(dfDaily):
weeklySampler = dfDaily.resample("W", label='left', loffset=pd.DateOffset(days=1))
dfWeekly = weeklySampler.agg({"open":"first", "high":"max", "low":"min", "close":"last", "volume":"sum"})
dfWeekly = dfWeekly.loc[:, ("open","high","low","close","volume")]
return dfWeekly
如果有人能帮我找到一个聪明/有效的方法来创建第三个数据帧,我真的很感激.谢谢!
解决方法:
你可以使用,combine_first,where和ffill:
dfweekly.combine_first(dfdaily)\
.where(dfweekly.notnull())\
.ffill()
Ouptut:
open high low close volume
date
2017-10-30 44.66 44.75 42.19 42.93 3500.0
2017-11-01 44.66 44.75 42.19 42.93 3500.0
2017-11-03 44.66 44.75 42.19 42.93 3500.0
2017-11-06 43.15 43.75 40.60 41.02 9200.0
2017-11-07 43.15 43.75 40.60 41.02 9200.0
2017-11-08 43.15 43.75 40.60 41.02 9200.0
2017-11-09 43.15 43.75 40.60 41.02 9200.0
2017-11-10 43.15 43.75 40.60 41.02 9200.0
2017-11-13 41.60 43.21 40.03 42.36 3575.0
2017-11-14 41.60 43.21 40.03 42.36 3575.0
2017-11-16 41.60 43.21 40.03 42.36 3575.0
2017-11-17 41.60 43.21 40.03 42.36 3575.0
2017-11-20 42.70 43.10 40.35 40.37 3100.0
2017-11-21 42.70 43.10 40.35 40.37 3100.0
2017-11-22 42.70 43.10 40.35 40.37 3100.0
2017-11-23 42.70 43.10 40.35 40.37 3100.0
2017-11-24 42.70 43.10 40.35 40.37 3100.0
2017-11-27 40.28 40.40 39.10 40.04 3500.0
2017-11-28 40.28 40.40 39.10 40.04 3500.0
更新:
dfweekly.combine_first(dfdaily)\
.where(dfweekly.notnull())\
.ffill().reindex(dfdaily.index)
标签:python,pandas,dataframe,resampling,stocks 来源: https://codeday.me/bug/20190724/1521600.html