pd.Grouper功能 - 在许多df中的一个上失败

时间:2018-06-06 06:31:02

标签: python pandas debugging

我正在尝试使用pd.Grouper作为答案迭代多个单独的df here.

现在,这适用于我8个df中的7个,只需要几秒钟。然而其中一个 - 即使是最大的也没有被抓住并且最终因内存错误而死亡,我不知道为什么因为df几乎相同。

代码的故障块如下:

g = df.groupby(pd.Grouper(freq="5s"))
df2 = pd.DataFrame(
    dict(
    open = g["price"].first(),
    close = g["price"].last(),
    high = g["price"].max(),
    low = g["price"].min(),
    volume = g["volume"].sum(),
    buy_volume = g["buy_volume"].sum(),
    sell_volume = -g["sell_volume"].sum(),
    num_trades = g["size"].count(),
    num_buy_trades = g["buy_trade"].sum(),
    num_sell_trades = g["sell_trade"].sum(),
    pct_buy_trades  = g["buy_trade"].mean() * 100,
    pct_sell_trades = g["sell_trade"].mean() * 100,
    )
)

有问题的样本df采用以下格式:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 3589964 entries, 1970-01-01 00:00:01.528000 to 2018-06-03 05:54:02.690000
Data columns (total 8 columns):
price          float64
size           float64
buy_sell       bool
volume         float64
buy_volume     float64
sell_volume    float64
buy_trade      bool
sell_trade     bool
dtypes: bool(3), float64(5)
memory usage: 254.6 MB

有3.5mil条目如下:

     price    size     buy_sell    volume   buy_volume  sell_volume buy_trade   sell_trade
T
2018-05-18 12:05:11.407 8097.02 0.007823    False   0.007823    0.007823    0.000000    True    False
2018-05-18 12:05:11.720 8097.02 0.129632    False   0.129632    0.129632    0.000000    True    False
2018-05-18 12:05:12.402 8097.02 0.037028    False   0.037028    0.037028    0.000000    True    False
2018-05-18 12:05:12.786 8097.03 0.307939    False   0.307939    0.307939    0.000000    True    False
2018-05-18 12:05:12.786 8097.02 0.025517    False   0.025517    0.025517    0.000000    True    False
2018-05-18 12:05:12.788 8097.03 0.014835    False   0.014835    0.014835    0.000000    True    False
2018-05-18 12:05:14.226 8097.03 0.006198    False   0.006198    0.006198    0.000000    True    False
2018-05-18 12:05:14.341 8092.00 -0.010989   True    0.010989    0.000000    -0.010989   False   True
2018-05-18 12:05:15.307 8092.00 -0.000011   True    0.000011    0.000000    -0.000011   False   True
2018-05-18 12:05:15.307 8091.99 -0.019989   True    0.019989    0.000000    -0.019989   False   True
2018-05-18 12:05:15.387 8091.99 -0.007340   True    0.007340    0.000000    -0.007340   False   True
2018-05-18 12:05:15.603 8091.99 -0.002440   True    0.002440    0.000000    -0.002440   False   True
2018-05-18 12:05:15.679 8090.01 -0.098909   True    0.098909    0.000000    -0.098909   False   True

这是另一个完全正常的df并在几秒钟内完成:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1952985 entries, 2018-05-18 12:05:11.791000 to 2018-06-03 05:53:57
Data columns (total 8 columns):
price          float64
side           object
size           int64
volume         int64
buy_volume     float64
sell_volume    float64
buy_trade      bool
sell_trade     bool
dtypes: bool(2), float64(3), int64(2), object(1)
memory usage: 188.0+ MB

    price   side    size    volume  buy_volume  sell_volume buy_trade   sell_trade
timestamp                               
2018-05-18 12:05:11.791 8112.0  Sell    -4085   4085    0.0 -4085.0 False   True
2018-05-18 12:05:11.811 8111.5  Sell    -598    598 0.0 -598.0  False   True
2018-05-18 12:05:11.849 8111.5  Sell    -3000   3000    0.0 -3000.0 False   True
2018-05-18 12:05:11.876 8111.5  Sell    -1300   1300    0.0 -1300.0 False   True
2018-05-18 12:05:11.949 8111.5  Sell    -3408   3408    0.0 -3408.0 False   True
2018-05-18 12:05:12.476 8111.5  Sell    -50000  50000   0.0 -50000.0    False   True
2018-05-18 12:05:12.523 8111.5  Sell    -2500   2500    0.0 -2500.0 False   True
2018-05-18 12:05:12.698 8111.5  Sell    -8000   8000    0.0 -8000.0 False   True
2018-05-18 12:05:12.722 8111.5  Sell    -8000   8000    0.0 -8000.0 False   True
2018-05-18 12:05:12.809 8111.5  Sell    -815    815 0.0 -815.0  False   True
  • 我不知道为什么会这样。我怎么开始调试呢?

我一直在等待复制错误信息,但它已被卡住了50分钟。

感谢您的帮助,我一直在努力!

1 个答案:

答案 0 :(得分:1)

我的第一个想法是按sort_index排序index

如果仍有性能问题,DataetimeIndex - groupby应该存在数据字符问题,会创建许多小5s组。

编辑:

double check DatetimIndex后:

DatetimeIndex: 3589964 entries, 1970-01-01 00:00:01.528000 to 2018-06-03 05:54:02.690000

所以这里有大量的af组,这是不良表现的原因。

相关问题