【问题标题】:Resampling trade data into OHLCV with pandas使用 pandas 将贸易数据重新采样到 OHLCV
【发布时间】:2014-02-04 02:40:21
【问题描述】:

我在 pandas DataFrame 中有历史交易数据,包含价格和交易量列,由 DateTimeIndex 索引。

例如:

>>> print df.tail()
                             price  volume
2014-01-15 14:29:54+00:00  949.975    0.01
2014-01-15 14:29:59+00:00  941.370    0.01
2014-01-15 14:30:17+00:00  949.975    0.01
2014-01-15 14:30:24+00:00  941.370    0.01
2014-01-15 14:30:36+00:00  949.975    0.01

现在,我可以使用 df.resample(freq, how={'price': 'ohlc'}) 将其重新采样到 OHLC 数据中,这很好,但我还想包含音量。

当我尝试df.resample(freq, how={'price': 'ohlc', 'volume': 'sum'}) 时,我得到:

ValueError: Shape of passed values is (2,), indices imply (2, 95)

我不太确定我的数据集出了什么问题,或者为什么会失败。任何人都可以帮助阐明这一点吗?非常感谢。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    问题不在于重采样,而在于尝试将 MultiIndex(从价格 OHLC)与常规索引(对于 Volume sum)连接起来。

    In [17]: df
    Out[17]: 
                           price  volume
    2014-01-15 14:29:54  949.975    0.01
    2014-01-15 14:29:59  941.370    0.01
    2014-01-15 14:30:17  949.975    0.01
    2014-01-15 14:30:24  941.370    0.01
    2014-01-15 14:30:36  949.975    0.01
    
    [5 rows x 2 columns]
    
    In [18]: df.resample('30s', how={'price': 'ohlc'})  # Note the MultiIndex
    Out[18]: 
                           price                           
                            open     high      low    close
    2014-01-15 14:29:30  949.975  949.975  941.370  941.370
    2014-01-15 14:30:00  949.975  949.975  941.370  941.370
    2014-01-15 14:30:30  949.975  949.975  949.975  949.975
    
    [3 rows x 4 columns]
    
    In [19]: df.resample('30s', how={'volume': 'sum'})  # Regular Index for columns
    Out[19]: 
                         volume
    2014-01-15 14:29:30    0.02
    2014-01-15 14:30:00    0.02
    2014-01-15 14:30:30    0.01
    
    [3 rows x 1 columns]
    

    我猜你可以手动为(volume, sum) 创建一个MultiIndex,然后concat:

    In [34]: vol = df.resample('30s', how={'volume': 'sum'})
    
    In [35]: vol.columns = pd.MultiIndex.from_tuples([('volume', 'sum')])
    
    In [36]: vol
    Out[36]: 
                         volume
                            sum
    2014-01-15 14:29:30    0.02
    2014-01-15 14:30:00    0.02
    2014-01-15 14:30:30    0.01
    
    [3 rows x 1 columns]
    
    In [37]: price = df.resample('30s', how={'price': 'ohlc'})
    
    In [38]: pd.concat([price, vol], axis=1)
    Out[38]: 
                           price                             volume
                            open     high      low    close     sum
    2014-01-15 14:29:30  949.975  949.975  941.370  941.370    0.02
    2014-01-15 14:30:00  949.975  949.975  941.370  941.370    0.02
    2014-01-15 14:30:30  949.975  949.975  949.975  949.975    0.01
    
    [3 rows x 5 columns]
    

    但如果 resample 可以自动处理这个问题可能会更好。

    【讨论】:

    • 这似乎可以通过新的重采样 API (df.resample('30S').agg({'price': 'ohlc', 'volume': 'sum'})) 解决。
    【解决方案2】:

    您现在可以在更高版本的 Pandas 中执行此操作 示例:Pandas 版本 0.22.00 df.resample('30S').mean()

    【讨论】:

      猜你喜欢
      • 2014-01-21
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      相关资源
      最近更新 更多