Pandas - 按日期对日内时间序列进行分组

Pandas - grouping intra day timeseries by date(Pandas - 按日期对日内时间序列进行分组)
本文介绍了Pandas - 按日期对日内时间序列进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个多天的日内日志返回系列,我想将其缩减为每日 ohlc.我可以做类似的事情

I have an intra day series of log returns over multiple days that I would like to downsample to daily ohlc. I can do something like

hi = series.resample('B', how=lambda x: np.max(np.cumsum()))
low = series.resample('B', how=lambda x: np.min(np.cumsum())) 

但每次调用计算 cumsum 似乎效率低下.有没有办法先计算 cumsums 然后将 'ohcl' 应用于数据?

But it seems inefficient to compute cumsum on each call. Is there a way to first compute the cumsums and then apply 'ohcl' to the data?

1999-08-09 12:30:00-04:00   -0.000486
1999-08-09 12:31:00-04:00   -0.000606
1999-08-09 12:32:00-04:00   -0.000120
1999-08-09 12:33:00-04:00   -0.000037
1999-08-09 12:34:00-04:00   -0.000337
1999-08-09 12:35:00-04:00    0.000100
1999-08-09 12:36:00-04:00    0.000219
1999-08-09 12:37:00-04:00    0.000285
1999-08-09 12:38:00-04:00   -0.000981
1999-08-09 12:39:00-04:00   -0.000487
1999-08-09 12:40:00-04:00    0.000476
1999-08-09 12:41:00-04:00    0.000362
1999-08-09 12:42:00-04:00   -0.000038
1999-08-09 12:43:00-04:00   -0.000310
1999-08-09 12:44:00-04:00   -0.000337
...
1999-09-28 06:45:00-04:00    0.000000
1999-09-28 06:46:00-04:00    0.000000
1999-09-28 06:47:00-04:00    0.000000
1999-09-28 06:48:00-04:00    0.000102
1999-09-28 06:49:00-04:00   -0.000068
1999-09-28 06:50:00-04:00    0.000136
1999-09-28 06:51:00-04:00    0.000566
1999-09-28 06:52:00-04:00    0.000469
1999-09-28 06:53:00-04:00    0.000000
1999-09-28 06:54:00-04:00    0.000000
1999-09-28 06:55:00-04:00    0.000000
1999-09-28 06:56:00-04:00    0.000000
1999-09-28 06:57:00-04:00    0.000000
1999-09-28 06:58:00-04:00    0.000000
1999-09-28 06:59:00-04:00    0.000000

推荐答案

df.groupby([df.index.year, df.index.month, df.index.day]).transform(np.cumsum).resample('B', how='ohlc')

我认为这可能是我想要的,但我必须测试.

I think this might be what I want but I have to test.

zelazny7回复后:

After zelazny7's repsonse:

df.groupby(pd.TimeGrouper('D')).transform(np.cumsum).resample('D', how='ohlc')

有效,而且比我以前的解决方案更有效.

works and is also more efficient than my previous solution.

更新:

pd.TimeGrouper('D') 已被弃用,因为 pandas v0.21.0.

pd.TimeGrouper('D') is deprecated since pandas v0.21.0.

使用 pd.Grouper() 改为:

Use pd.Grouper() instead:

df.groupby(pd.Grouper(freq='D')).transform(np.cumsum).resample('D', how='ohlc')

这篇关于Pandas - 按日期对日内时间序列进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)