[Python] Pickle encoding utf-8 issue

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to pickle a pandas dataframe to my local directory so I can work on it in another jupyter notebook. The write appears to go successful at first but when trying to read it in a new jupyter notebook the read is unsuccessful.

    When I open the pickle file I appear to have wrote, the file's only contents are:

    Error! /Users/.../income.pickle is not UTF-8 encoded Saving disabled. See console for more details.

    I also checked and the pickle file itself is only a few kilobytes.

    Here's my code for writing the pickle:


    with open('income.pickle', 'wb', encoding='UTF-8') as to_write:
    pickle.dump(new_income_df, to_write)



    And here's my code for reading it:


    with open('income.pickle', 'rb') as read_file:
    income_df = pickle.load(read_file)



    Also when I return income_df I get this output:

    Series([], dtype: float64)

    It's an empty series that I errors on when trying to call most series methods on it.

    If anyone knows a fix for this I'm all ears. Thanks in advance!

    EDIT:

    This is the solution I arrived at:

    with open('cleaned_df', 'wb') as to_write:
    pickle.dump(df, to_write)

    with open('cleaned_df','rb') as read_file:
    df = pickle.load(read_file)


    Which was much simpler than I expected

    Continue reading...

Compartilhe esta Página