Pandas msgpack vs pickle

让人想犯罪 __ 提交于 2019-11-30 13:10:25

问题


msgpack in Pandas is supposed to be a replacement for pickle.

Per the Pandas docs on msgpack:

This is a lightweight portable binary format, similar to binary JSON, that is highly space efficient, and provides good performance both on the writing (serialization), and reading (deserialization).

I find, however, that its performance does not appear to stack up against pickle.

df = pd.DataFrame(np.random.randn(10000, 100))

>>> %timeit df.to_pickle('test.p')
10 loops, best of 3: 22.4 ms per loop

>>> %timeit df.to_msgpack('test.msg')
10 loops, best of 3: 36.4 ms per loop

>>> %timeit pd.read_pickle('test.p')
100 loops, best of 3: 10.5 ms per loop

>>> %timeit pd.read_msgpack('test.msg')
10 loops, best of 3: 24.6 ms per loop

Question: Asides from potential security issues with pickle, what are the benefits of msgpack over pickle? Is pickle still the preferred method of serializing data, or do better alternatives currently exist?


回答1:


Pickle is better for the following:

  1. Numerical data or anything that uses the buffer protocol (numpy arrays) (though only if you use a somewhat recent protocol=)
  2. Python specific objects like classes, functions, etc.. (although here you should look at cloudpickle)

MsgPack is better for the following:

  1. Cross language interoperation. It's an alternative to JSON with some improvements
  2. Performance on text data and Python objects. It's a decent factor faster than Pickle at this under any setting.

As @Jeff noted above this blogpost may be of interest



来源:https://stackoverflow.com/questions/30651724/pandas-msgpack-vs-pickle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!