[Python] Solving incompatible dtype warning for pandas DataFrame when setting new column...

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

  1. Stack

    Stack Membro Participativo

    Setting the value of a new dataframe column:

    df.loc[df["Measure] == metric.label, "source_data_url"] = metric.source_data_url


    now (as of Pandas version 2.1.0) gives a warning,

    FutureWarning:
    Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value ' metric_3' has dtype incompatible with float64, please explicitly cast to a compatible dtype
    first.


    The Pandas documentation discusses how the problem can be solved for a Series but it is not clear how to do this iteratively (the line above is called in a loop over metrics and it's the final metric that gives the warning) when assigning a new DataFrame column. How can this be done?

    Continue reading...

Compartilhe esta Página