Skip to main content
 首页 » 编程设计

python之Pandas 在特定时间(不是午夜)将数据帧规范化为数据(报价)

2024年10月25日10jackei

我的数据框 (ds) 中的每日数据看起来像这样跨越了几年:

对于每一天,我都需要将所有报价标准化为该特定日期的特定时间。例如,在 6 月 1 日,我需要将所有报价标准化为 6 月 1 日下午 3 点的报价,但在 6 月 2 日,我需要将它们全部标准化为 6 月 2 日下午 3 点的报价。

我有一个每天下午 3 点所有报价的数据框,如下所示:

所以我认为如果我可以简单地将我的报价数据帧 (ds) 除以下午 3 点数据帧,它会起作用:

dr = ds.groupby(ds.index.date).apply(lambda x: x / b) 

请注意,下午 3 点数据框包含的天数比报价数据框 (ds) 更多。因此,报价数据框必须划分为正确的相应日期在下午 3 点的数据框中,可能使用类似 b.index.date

的东西

我也尝试过不使用单独的数据框,而是从报价数据框 (ds) 本身中提取最后一个可用的报价 - 这也不起作用:

编辑:感谢 DSM 的建议。这有帮助吗?不确定我是否应该 pd.to_string() 数据框,因为它似乎也不容易剪贴板可读。

引用 ds 数据框:

bid ask mid 
2000-01-04 14:45:12+00:00   281.0   281.5   281.25 
2000-01-04 14:46:10+00:00   281.0   282.0   281.5 
2000-01-04 14:47:14+00:00   281.2   282.2   281.7 
2000-01-04 14:47:22+00:00   281.25  281.85  281.55 
2000-01-04 14:47:47+00:00   281.25  281.75  281.5 
2000-01-04 14:48:09+00:00   281.4   281.9   281.65 
2000-01-04 14:48:40+00:00   281.3   282.3   281.8 
2000-01-04 14:49:40+00:00   281.3   281.8   281.55 
2000-01-04 14:49:45+00:00   281.2   282.2   281.7 
2000-01-04 14:50:53+00:00   281.4   281.9   281.65 

3pm 数据框:

bid_close   ask_close   price 
2000-01-04 15:00:00+00:00   281.35  281.95  281.65 
2000-01-05 15:00:00+00:00   280.73  281.48  281.105 
2000-01-06 15:00:00+00:00   279.7   280.3   280.0 
2000-01-07 15:00:00+00:00   282.3   282.9   282.6 
2000-01-10 15:00:00+00:00   281.7   282.3   282.0 
2000-01-11 15:00:00+00:00   282.1   282.7   282.4 
2000-01-12 15:00:00+00:00   281.9   282.5   282.2 
2000-01-13 15:00:00+00:00   281.9   282.7   282.3 
2000-01-14 15:00:00+00:00   283.15  283.75  283.45 
2000-01-17 15:00:00+00:00   285.5   286.0   285.75 

还有命令:

c = ds.groupby(ds.index.date).apply(lambda x: x / x.between_time('14:30:00', '14:59:59').resample('30Min', how='last').dropna(how='all')) 

我也尝试过(看起来很接近):

df = a.groupby(a.index.date).apply(lambda x: x / x.between_time('14:45:00', '14:59:59').tail(1)) 

这是一个似乎相关的链接(以及我基于上面命令的链接):Grouping daily data by month in python/pandas and then normalizing

请您参考如下方法:

这是第一天的简化示例,考虑到 d 是您的 quotes ds dataframe 和 n 是您的 3pm 数据框:

#get the first day of the 3pm dataframe 
first_day = n.index.levels[0][0] 
#get the day of the quotes dataframe from the first day of 3pm frame  
d1 = d.ix[first_day] 
#get the 3pm values for the first day 
n1 = n.ix[first_day] 
#normalize the bid column, don't forget to modify the range variable  
norm = pd.concat([d1[d1.columns[i]].apply(lambda x: x / n1[n1.columns[i]]) for i in range(3)], axis = 1) 
 
>>> norm 
                    15:00:00+00:00  15:00:00+00:00      15:00:00+00:00 
14:45:12+00:00  0.9987559978674249  0.9984039723355205  0.9985797976211611 
14:46:10+00:00  0.9987559978674249  1.0001773364071644  0.9994674241079354 
14:47:14+00:00  0.9994668562288963  1.000886682035822   1.0001775252973548 
14:47:22+00:00  0.9996445708192642  0.9996453271856713  0.9996449494052904 
14:47:47+00:00  0.9996445708192642  0.9992906543713425  0.9994674241079354 
14:48:09+00:00  1.0001777145903676  0.9998226635928356  1.0 
14:48:40+00:00  0.9998222854096321  1.0012413548501509  1.0005325758920647 
14:49:40+00:00  0.9998222854096321  0.9994679907785069  0.9996449494052904 
14:49:45+00:00  0.9994668562288963  1.000886682035822   1.0001775252973548 
14:50:53+00:00  1.0001777145903676  0.9998226635928356  1.0