Open
Description
import numpy as np
import pandas as pd
ii = pd.interval_range(1, 10)
ser = pd.Series(ii).copy()
mask = np.zeros(ser.shape, dtype=bool)
mask[1] = True
ii.insert(1, np.nan) # <- IntervalDtype[float64]
ii.putmask(mask, np.nan) # <- IntervalDtype[float64]
ser.where(mask, np.nan) # <- ValueError
ser[1] = np.nan <- coerces to object
ATM dtypes.cast.ensure_dtype_can_hold_na
is incorrect for EA dtypes that cannot hold NA values (the IntervalDtype[int]
example is the only one that comes to mind).
We have special-casing for IntervalDtype in Index._find_common_type_compat
which is why the Index methods above behave well. Short-term, we can move that special-casing into ensure_dtype_can_hold_na
. Longer-term, we need a way for EADtype subclasses to specify this behavior.
This could be seen as a special case of xref #24246