我正在开发一个用于财务目的的模型。我将整个 S&P500 组件放在一个文件夹中,存储了尽可能多的 .hdf 文件。每个 .hdf 文件都有自己的多索引(年-周-分)。
顺序代码示例(非并行化):
import os
from classAsset import Asset
def model(current_period, previous_perdiod):
# do stuff on the current period, based on stats derived from previous_period
return results
if __name__ == '__main__':
for hdf_file in os.listdir('data_path'):
asset = Asset(hdf_file)
for year in asset.data.index.get_level_values(0).unique().values:
for week in asset.data.loc[year].index.get_level_values(0).unique().values:
previous_period = asset.data.loc[(start):(end)].Open.values # start and end are defined in another function
current_period = asset.data.loc[year, week].Open.values
model(current_period, previous_period)
为了加快进程,我使用 multiprocessing.pool 同时在多个 .hdf 文件上运行相同的算法,所以我对处理速度很满意(我有一个 4c/8t CPU) .但现在我发现了 Dask。
在Dask documentation 'DataFrame Overview'他们指出:
普通可并行操作(快速):
- 逐元素操作:df.x + df.y, df * df
- 按行选择:df[df.x > 0]
- Loc: df.loc[4.0:10.5](这是我最感兴趣的地方)
另外,在 Dask documentation 'Use Cases'他们指出:
A programmer has a function that they want to run many times on different inputs. Their function and inputs might use arrays or dataframes internally, but conceptually their problem isn’t a single large array or dataframe.
They want to run these functions in parallel on their laptop while they prototype but they also intend to eventually use an in-house cluster. They wrap their function in dask.delayed and let the appropriate dask scheduler parallelize and load balance the work.
所以我确定我遗漏了一些东西,或者可能不仅仅是一些东西。使用 multiprocessing.pool 和 dask.multiprocessing 处理许多单个 pandas 数据帧有什么区别?
你认为我应该针对我的具体情况使用 Dask 吗?谢谢你们。
最佳答案
没有区别。 Dask 正在做您在自定义代码中所做的事情。它使用 pandas 和线程或多处理池来实现并行。
出于某些原因,您可能更喜欢 Dask
- 它会弄清楚如何自动编写并行算法
- 您将来可能希望扩展到集群
但如果您所拥有的对您来说效果很好,那么我会坚持下去。
关于python - dask.multiprocessing 或 pandas + multiprocessing.pool : what's the difference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754524/