在不添加新日期的情况下重新采样盘中 pandas DataFrame

Resample intraday pandas DataFrame without add new days(在不添加新日期的情况下重新采样盘中 pandas DataFrame)
本文介绍了在不添加新日期的情况下重新采样盘中 pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在不添加新日期的情况下对一些日内数据进行下采样

I want to downsample some intraday data without adding in new days

df.resample('30Min')

将添加周末等,这是不可取的.反正有这个吗?

Will add weekends etc which is undesirable. Is there anyway around this?

推荐答案

组合的 groupby/resample 可能会起作用:

A combined groupby/resample might work:

In [22]: dates = pd.date_range('01-Jan-2014','11-Jan-2014', freq='T')[0:-1]
    ...: dates = dates[dates.dayofweek < 5]
    ...: s = pd.TimeSeries(np.random.randn(dates.size), dates)
    ...: 

In [23]: s.size
Out[23]: 11520

In [24]: s.groupby(lambda d: d.date()).resample('30min').size
Out[24]: 384

In [25]: s.groupby(lambda d: d.date()).resample('30min')
Out[25]: 
2014-01-01  2014-01-01 00:00:00    0.202943
            2014-01-01 00:30:00   -0.466010
            2014-01-01 01:00:00    0.029175
            2014-01-01 01:30:00   -0.064492
            2014-01-01 02:00:00   -0.113348
            2014-01-01 02:30:00    0.100408
            2014-01-01 03:00:00   -0.036561
            2014-01-01 03:30:00   -0.029578
            2014-01-01 04:00:00   -0.047602
            2014-01-01 04:30:00   -0.073846
            2014-01-01 05:00:00   -0.410143
            2014-01-01 05:30:00    0.143853
            2014-01-01 06:00:00   -0.077783
            2014-01-01 06:30:00   -0.122345
            2014-01-01 07:00:00    0.153003
...
2014-01-10  2014-01-10 16:30:00   -0.107377
            2014-01-10 17:00:00   -0.157420
            2014-01-10 17:30:00    0.201802
            2014-01-10 18:00:00   -0.189018
            2014-01-10 18:30:00   -0.310503
            2014-01-10 19:00:00   -0.086091
            2014-01-10 19:30:00   -0.090800
            2014-01-10 20:00:00   -0.263758
            2014-01-10 20:30:00   -0.036789
            2014-01-10 21:00:00    0.041957
            2014-01-10 21:30:00   -0.192332
            2014-01-10 22:00:00   -0.263690
            2014-01-10 22:30:00   -0.395939
            2014-01-10 23:00:00   -0.171149
            2014-01-10 23:30:00    0.263057
Length: 384

In [26]: np.unique(_25.index.get_level_values(1).minute)
Out[26]: array([ 0, 30])

In [27]: np.unique(_25.index.get_level_values(1).dayofweek)
Out[27]: array([0, 1, 2, 3, 4]) 

这篇关于在不添加新日期的情况下重新采样盘中 pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

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 时间序列插值和正则化)