从不同的时间系列创建pandas列

时间:2017-10-24 21:58:02

标签: python python-3.x pandas

我有一个存储在数据帧中的指标,时间为1小时系列,我想将其提取到1分钟时间系列的数据帧,但我希望这个指标有一个值一分钟数组的可能索引,而不是每小时。

我尝试了以下代码:

import pandas as pd
import datetime 

DAX_H1 = pd.read_csv("D:\Finance python\Data\DAX\GER30_H1_fixed.csv",index_col='date',error_bad_lines=False)
DAX_M1 = pd.read_csv("D:\Finance python\Data\DAX\GER30_m1_fixed.csv",index_col='date',error_bad_lines=False)

DAX_M1=DAX_M1.iloc[0:6000]
DAX_H1=DAX_H1.iloc[0:110]

DAX_H1["EWMA_8"]=DAX_H1["Open"].ewm(min_periods=8,com=3.5).mean()

DAX_H1.index =pd.to_datetime(DAX_H1.index)

DAX_M1.index =pd.to_datetime(DAX_M1.index)

DAX_M1["H1_EWMA_8"]=""

for i in DAX_M1.index:
    DAX_M1["H1_EWMA_8"][i] = DAX_H1["EWMA_8"][pd.Timestamp(datetime.datetime(i.year,i.month,i.day,i.hour))]

然而,它似乎不起作用,即使它起作用,我也认为它会很慢。

如果我只是执行以下操作:

DAX_M1["H1_EWMA_8"]=DAX_H1["EWMA_8"]

我没有为每个索引"H1_EWMA_8"的值:

2014-01-02 16:53:00            NaN
2014-01-02 16:54:00            NaN
2014-01-02 16:55:00            NaN
2014-01-02 16:56:00            NaN
2014-01-02 16:57:00            NaN
2014-01-02 16:58:00            NaN
2014-01-02 16:59:00            NaN
2014-01-02 17:00:00    9449.979026
2014-01-02 17:01:00            NaN
2014-01-02 17:02:00            NaN
Name: H1_EWMA_8, dtype: float64

是否有一种简单的方法可以将NaN值替换为"H1_EWMA_8"的最后一个可用值?

DAX_M1和DAX_H1的某些部分用于说明目的:

DAX_M1

                     Open  High   Low  Close  Total Ticks
date                                                     
2014-01-01 22:00:00  9597  9597  9597   9597            1
2014-01-02 07:05:00  9597  9619  9597   9618           18
2014-01-02 07:06:00  9618  9621  9617   9621            5
2014-01-02 07:07:00  9621  9623  9620   9623            6
2014-01-02 07:08:00  9623  9625  9623   9625            9
2014-01-02 07:09:00  9625  9625  9622   9623            6
2014-01-02 07:10:00  9623  9625  9622   9624           13
2014-01-02 07:11:00  9624  9626  9624   9626            8
2014-01-02 07:12:00  9626  9626  9623   9623            9
2014-01-02 07:13:00  9623  9625  9623   9625            5

DAX_H1:

                     Open  High   Low  Close  Total Ticks       EWMA_8
date                                                                  
2014-01-01 22:00:00  9597  9597  9597   9597            1          NaN
2014-01-02 07:00:00  9597  9626  9597   9607          322          NaN
2014-01-02 08:00:00  9607  9617  9510   9535         1730          NaN
2014-01-02 09:00:00  9535  9537  9465   9488         1428          NaN
2014-01-02 10:00:00  9488  9505  9478   9490          637          NaN
2014-01-02 11:00:00  9490  9512  9473   9496          817          NaN
2014-01-02 12:00:00  9496  9510  9495   9504          450          NaN
2014-01-02 13:00:00  9504  9514  9484   9484          547  9518.123073
2014-01-02 14:00:00  9484  9493  9424   9436         1497  9509.658500

欢迎任何帮助!

修改:此解决方案有效:

DAX_M1 = DAX_M1.fillna(method='ffill') 

0 个答案:

没有答案