3

我有一个 csv 文件,其中包含跨越多天的 1 分钟股票数据。每天从 9:30 运行到 16:00。

缺少时间序列中的一些分钟:(此处缺少 2013-09-16 09:32:00 和 2013-09-17 09:31:00)

2013-09-16 09:30:00,461.01,461.49,461,461,183507
2013-09-16 09:31:00,460.82,461.6099,460.39,461.07,212774
2013-09-16 09:33:00,460.0799,460.88,458.97,459.2401,207880
2013-09-16 09:34:00,458.97,460.08,458.8,460.04,148121
...
2013-09-16 15:59:00,449.72,450.0774,449.59,449.95,146399
2013-09-16 16:00:00,450.12,450.12,449.65,449.65,444594
2013-09-17 09:30:00,448,448,447.5,447.96,173624
2013-09-17 09:32:00,450.6177,450.9,449.05,449.2701,268715
2013-09-17 09:33:00,451.39,451.96,450.58,450.7061,197019
...
...

使用熊猫,我如何转发填充系列,以便每一分钟都存在?我应该看起来像这样:

2013-09-16 09:30:00,461.01,461.49,461,461,183507
2013-09-16 09:31:00,460.82,461.6099,460.39,461.07,212774
2013-09-16 09:32:00,460.82,461.6099,460.39,461.07,212774 <-- forward filled
2013-09-16 09:33:00,460.0799,460.88,458.97,459.2401,207880
2013-09-16 09:34:00,458.97,460.08,458.8,460.04,148121
...
2013-09-16 15:59:00,449.72,450.0774,449.59,449.95,146399
2013-09-16 16:00:00,450.12,450.12,449.65,449.65,444594
2013-09-17 09:30:00,448,448,447.5,447.96,173624
2013-09-17 09:31:00,448,448,447.5,447.96,173624 <-- forward filled
2013-09-17 09:32:00,450.6177,450.9,449.05,449.2701,268715
2013-09-17 09:33:00,451.39,451.96,450.58,450.7061,197019
...

如果连续丢失多分钟,它还需要考虑......

4

2 回答 2

4

所以我将你的前 4 行复制到一个数据框中:

Out[49]:
                    0         1         2       3         4       5
0 2013-09-16 09:30:00  461.0100  461.4900  461.00  461.0000  183507
1 2013-09-16 09:31:00  460.8200  461.6099  460.39  461.0700  212774
2 2013-09-16 09:33:00  460.0799  460.8800  458.97  459.2401  207880
3 2013-09-16 09:34:00  458.9700  460.0800  458.80  460.0400  148121

然后

df1 = df.set_index(keys=[0]).resample('1min', fill_method='ffill')
df1

Out[52]:
                            1         2       3         4       5
0                                                                
2013-09-16 09:30:00  461.0100  461.4900  461.00  461.0000  183507
2013-09-16 09:31:00  460.8200  461.6099  460.39  461.0700  212774
2013-09-16 09:32:00  460.8200  461.6099  460.39  461.0700  212774
2013-09-16 09:33:00  460.0799  460.8800  458.97  459.2401  207880
2013-09-16 09:34:00  458.9700  460.0800  458.80  460.0400  148121

这还将处理多个缺失值并向前填充它们。

所以如果我有类似的数据

2013-09-17 09:30:00,448,448,447.5,447.96,173624
2013-09-17 09:33:00,451.39,451.96,450.58,450.7061,197019

并做与之前相同的事情:

Out[55]:
                          1       2       3         4       5
0                                                            
2013-09-17 09:30:00  448.00  448.00  447.50  447.9600  173624
2013-09-17 09:31:00  448.00  448.00  447.50  447.9600  173624
2013-09-17 09:32:00  448.00  448.00  447.50  447.9600  173624
2013-09-17 09:33:00  451.39  451.96  450.58  450.7061  197019

这里的关键是你必须有一个 datetimeindex,如果你想把它作为一个列保存,那么你可以drop=Falseset_index.

于 2013-10-09T10:41:32.363 回答
2

这对您来说可能会稍微好一些,因为它考虑了不同的日子,这样您就不必每天都填写:

只需创建数据框:

list1 = [["2013-09-16 09:29:00","461.01","461.49","461","461","183507"],
["2013-09-16 09:31:00", "460.82", "461.6099", "460.39", "461.07", "212774"], 
["2013-09-16 09:34:00", "460.0799", "460.88", "458.97", "459.2401", "207880"], 
["2013-09-17 09:35:00", "458.97", "460.08", "458.8", "460.04", "148121"]]

cols = ['date','price1','price2','price3', 'price4', 'price5']

df = DataFrame(list1, columns=cols)

将索引设置为日期列:

df['date'] = pd.to_datetime(df['date'])

df.set_index('date', inplace=True)

重新索引并填充孔,然后向前填充生成的 NaN 值,然后在上午 9:30 到下午 4:00 之外的所有时间下降:

df2 = df.reindex(pd.date_range(df.index[0], df.index[-1], freq='Min')).ffill().ix[df2.index.indexer_between_time(pd.datetime(year=1,month=1,day=1,hour=9,minute=30).time(), datetime.time(16))] 

这些语句可以按顺序分解:

首先,重新索引数据框,以便您的索引对应于您的开始日期/时间到结束日期/时间,频率为 1 分钟:

df2 = df.reindex(pd.date_range(df.index[0], df.index[-1], freq='Min')) 

这将创建许多新索引与旧索引不一致的 NaN 值。我们用 ffill(前向填充)填充它,尽管还有其他选项:

df2.ffill(inplace=True)

最后,去掉上午 9:30 到下午 4:00 时间范围之外的时间:

df_final = df2.ix[df2.index.indexer_between_time(pd.datetime(year=1,month=1,day=1,hour=9,minute=30).time(), datetime.time(16))]

因为 .time() 不需要 9.5 并且文档有点稀疏,所以我只是创建了一个 datetime 对象,其时间值设置为上午 9:30,然后使用 .time() 来获取它。我敢肯定,有更好的方法。

于 2013-10-09T11:58:07.720 回答