[Python] pandas test if the datatype of an input series supports nan values

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I had something like the following code using pandas 1.x that new generates a warning in pandas 2:

    import pandas as pd
    import numpy as np

    df1 = pd.DataFrame({"i":[1,2,3,4,5], "a":[2,4,6,8,10]})
    df2= pd.DataFrame({"i":[2,4], "a":[3,6],"b":[4,8]})

    df1["b"] = np.nan
    df1.loc[df2.index,:] = df2



    which throws a warning: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas.

    normally I can do this to fix:

    df1['b'] = df1['b'].astype(df2.b.dtype)


    but in my case I wont know the datatype of column b ahead of time, so I wont know if the datatype supports nan. Is there a nice way of settings df1's dtype as either df2's datatype or using something like convert_dtypes() only as a fallback?

    I know I can manually compare the column with a copy of itself with nan to see if the dtype changes but it seems computationally expensive

    Continue reading...

Compartilhe esta Página