【问题标题】:Getting a EOF error when calling pd.read_pickle调用 pd.read_pickle 时出现 EOF 错误
【发布时间】:2017-06-15 18:23:43
【问题描述】:

有一个关于 pandas DataFrame 和 pd.read_pickle() 函数的快速问题。基本上,我有一个大而简单的数据框(333 mb)。当我在数据帧上运行 pd.read_pickle 时,我得到了 EOFError。

有没有办法解决这个问题?这可能是什么原因造成的?

【问题讨论】:

  • 当我遇到这个错误时,我发现这是由于最初的酸洗没有正确完成。泡菜文件已创建,但未正确完成。在我看来,这是泡菜中 EOFError 的唯一可能来源,泡菜格式错误,即未完成。

标签: pandas dataframe pickle


【解决方案1】:

当我使用以下方法创建泡菜时,我看到了相同的 EOFError:

pandas.DataFrame.to_pickle('path.pkl', compression='bz2')

然后尝试阅读:

pandas.read_pickle('path.pkl')

我通过在读取时提供压缩解决了这个问题:

pandas.read_pickle('path.pkl', compression='bz2')

根据 Pandas 文档:

compression : {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}, default ‘infer’

    string representing the compression to use in the output file. By default, 
    infers from the file extension in specified path.

因此,只需将路径从“path.pkl”更改为“path.bz2”也可以解决问题。

【讨论】:

    【解决方案2】:

    我可以确认greg_data的宝贵意见:

    当我遇到这个错误时,我发现这是由于 初始酸洗未正确完成。泡菜文件是 已创建,但未正确完成。在我看来这是唯一的 泡菜中 EOFError 的可能来源,泡菜是 格式错误,即未完成。

    我在酸洗过程中的错误是:

    ---------------------------------------------------------------------------
    
    TypeError                                 Traceback (most recent call last)
    
    <ipython-input-40-263240bbee7e> in <module>()
    ----> 1 main()
    
    <ipython-input-38-9b3c6d782a2a> in main()
         43     with open("/content/drive/MyDrive/{}.file".format(tm.id), "wb") as f:
    ---> 44         pickle.dump(tm, f, pickle.HIGHEST_PROTOCOL)
         45 
         46     print('Coherence:', get_coherence(tm, token_lists, 'c_v'))
    
    TypeError: can't pickle weakref objects
    

    并且在读取pickle过程中明显没有完成的那个pickle文件时,出现了报错:

    pd.read_pickle(r'/content/drive/MyDrive/TEST_2021_06_01_10_23_02.file')
    

    错误:

    ---------------------------------------------------------------------------
    
    EOFError                                  Traceback (most recent call last)
    
    <ipython-input-41-460bdd0a2779> in <module>()
    ----> 1 object = pd.read_pickle(r'/content/drive/MyDrive/TEST_2021_06_01_10_23_02.file')
    
    /usr/local/lib/python3.7/dist-packages/pandas/io/pickle.py in read_pickle(filepath_or_buffer, compression)
        180                 # We want to silence any warnings about, e.g. moved modules.
        181                 warnings.simplefilter("ignore", Warning)
    --> 182                 return pickle.load(f)
        183         except excs_to_catch:
        184             # e.g.
    
    EOFError: Ran out of input
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2021-03-17
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多