[Python] Pandas astype becomes in-place operation for data loaded from pickle files

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    Pandas astype() appears to unexpectedly switch to performing in-place operations after loading data from a pickle file. Concretly, for astype(str), the data type of the input dataframe values is modified. What is causing this behavior?

    Pandas version: 2.0.3

    Minimal example:

    import pandas as pd
    import numpy as np

    # create a test dataframe
    df = pd.DataFrame({'col1': ['hi']*10 + [False]*20 + [np.nan]*30})

    # print the data types of the cells, before and after casting to string
    print(pd.unique([type(elem) for elem in df['col1'].values]))
    _ = df.astype(str)
    print(pd.unique([type(elem) for elem in df['col1'].values]))

    # store the dataframe as pkl and directly load it again
    outpath = 'C:/Dokumente/my_test_df.pkl'
    df.to_pickle(outpath)
    df2 = pd.read_pickle(outpath)

    # print the data types of the cells, before and after casting to string
    print(pd.unique([type(elem) for elem in df2['col1'].values]))
    _ = df2.astype(str)
    print(pd.unique([type(elem) for elem in df2['col1'].values]))


    Output:

    [​IMG]

    Continue reading...

Compartilhe esta Página