【问题标题】:How do I reindex a pandas DataFrame while also resampling it and aggregating its data according to the new index?如何重新索引 pandas DataFrame,同时根据新索引对其进行重新采样并聚合其数据?
【发布时间】:2021-02-04 03:21:54
【问题描述】:

1) 我在 pandas DataFrame 中有以下 1 分钟频率数据:

0 Open High Low Close Volume
2010-10-19 06:31:00 58.75 58.81 58.58 58.59 228125
2010-10-19 06:32:00 58.59 58.68 58.55 58.57 153303
2010-10-19 06:33:00 58.57 58.6 58.5 58.52 115647
2010-10-19 06:34:00 58.52 58.58 58.48 58.58 63577
2010-10-19 06:35:00 58.57 58.59 58.51 58.53 111770

2)我还有以下索引数组:

[2010-10-19 06:32:00, 2010-10-19 06:35:00]

3) 我想根据索引数组重新索引 DataFrame,这样新的 DataFrame 将只有索引数组的 2 行,同时设法对其重新采样,以便新数据帧的第一行是原始数据帧前两行的高点中较高的,新数据帧第二行的低点是原始数据帧中三个低点中较低的,以此类推。

通常,人们会通过 .resample() 和 .agg() 聚合数据,但前提是您已经拥有所需状态的数据框。我不能以这样的方式使用 reindex(),我可以用 .resample() 跟进它并完成此操作。

我想我正在寻找一种方法来一次性重新索引和重新采样。我怎样才能最好地做到这一点?

【问题讨论】:

    标签: python pandas dataframe reindex pandas-resample


    【解决方案1】:

    改编自pandas Dataframe resampling with specific dates的答案

    from datetime import datetime
    
    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame(
        data={c: np.random.rand(5) for c in ['o', 'h', 'l', 'c', 'v']},
        index=pd.date_range(datetime(2020, 10, 19, 6, 31), datetime(2020, 10, 19, 6, 35), freq='T')
    )
    print(df)
    
                                o         h         l         c         v
    2020-10-19 06:31:00  0.868832  0.011599  0.614113  0.920998  0.237791
    2020-10-19 06:32:00  0.909751  0.277570  0.820222  0.493289  0.941469
    2020-10-19 06:33:00  0.998590  0.667477  0.108915  0.551331  0.081069
    2020-10-19 06:34:00  0.160800  0.179726  0.987618  0.351980  0.253893
    2020-10-19 06:35:00  0.553217  0.873212  0.291289  0.235526  0.525988
    
    sample_index = pd.DatetimeIndex([datetime(2020, 10, 19, 6, 32), datetime(2020, 10, 19, 6, 35)])
    agg = {'o': 'first', 'h': 'max', 'l': 'min', 'c': 'last', 'v': 'sum'}
    ohlcv = df.groupby(sample_index[sample_index.searchsorted(df.index)]).agg(agg)
    print(ohlcv)
    
                                o         h         l         c         v
    2020-10-19 06:32:00  0.868832  0.277570  0.614113  0.493289  1.179259
    2020-10-19 06:35:00  0.998590  0.873212  0.108915  0.235526  0.860951
    

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2023-01-17
      • 2012-10-09
      相关资源
      最近更新 更多