Python pd.Grouper函数-在多个df中的一个上失败
我正试图用pd.gropper作为答案迭代多个单独的df 现在,我的8个测向中有7个测向有效,只需几秒钟。然而,其中一个——即使是最大的一个也没有——被抓到并挂起,最终死于内存错误,我不知道为什么,因为df几乎是相同的 故障代码块如下所示:Python pd.Grouper函数-在多个df中的一个上失败,python,pandas,debugging,Python,Pandas,Debugging,我正试图用pd.gropper作为答案迭代多个单独的df 现在,我的8个测向中有7个测向有效,只需几秒钟。然而,其中一个——即使是最大的一个也没有——被抓到并挂起,最终死于内存错误,我不知道为什么,因为df几乎是相同的 故障代码块如下所示: g = df.groupby(pd.Grouper(freq="5s")) df2 = pd.DataFrame( dict( open = g["price"].first(), close = g["price"].last(),
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
这是另一个工作完全正常并在几秒钟内完成的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分钟
谢谢你的帮助,这让我头痛不已 我的第一个想法是按索引排序
如果仍然存在性能问题,则DataetimeIndex-groupby的数据字符问题会创建许多小的5s组
编辑:
DatetimIndex之后是:
下面是大量af组性能不佳的原因。如果没有完整的数据,很难知道,但可能DataetimeIndex的字符问题-groupby需要创建许多小的5s组。进行排序索引,发现一个错误行,其中的日期是1970-1-1,解析历元时间时出错,将其删除,现在已修复!奇怪的是,一次约会造成了如此多的悲伤,一定是每5秒就检查一次。很好的直觉!这是我的第一个想法,但因为看到2018-05-18 12:05:11.791000到2018-06-03 05:53:57,我认为datetimeindex分类得太多了:是的,但我再次这样做是为了确保,帖子中的问题是datetimeindex:3589964个条目,1970-01-01 00:00:01.528000到2018-06-03 05:54:02.690000。请随意回答,以便我可以奖励:
<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
DatetimeIndex: 3589964 entries, 1970-01-01 00:00:01.528000 to 2018-06-03 05:54:02.690000