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?
use combine_first
df0 = df1.combine_first(df0)