[Python] Pandas : make future.no_silent_downcasting working

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    Python 3.11, Pandas 2.2.3

    I currently track FutureWarnings to sanitize an existing code. To silent and opt-in future Downcasting behavior in 'replace' is deprecated and will be removed in a future version, I use pd.set_option("future.no_silent_downcasting", True).

    Then, I block on the following minimal example:

    import pandas as pd
    print(f"{pd.__version__=}") # '2.2.3'
    # discard "Downcasting behavior in `replace`..."
    pd.set_option("future.no_silent_downcasting", True)

    # Raise en Exception on any Warning:
    from warnings import warn, simplefilter
    simplefilter('error') # raise on Warning

    df0 = pd.DataFrame({"a": [True, True, False, True, False]})
    df1 = pd.DataFrame({"a": [False, True]}, index=[2,4])

    # First attempt:
    # raise `FutureWarning: Setting an item of incompatible dtype is deprecated and will raise
    # in a future error of pandas.
    # Value '[True True False True True]' has dtype incompatible with bool, please explicitly cast to a compatible dtype first.`
    df0.update(df1) # Raise a FutureWarning

    # Second attempt
    df0.update(df1.astype(df0.dtypes)) # Also raise the same FutureWarning


    I'm a bit confused... How to solve this Warning?

    Continue reading...

Compartilhe esta Página