Closed
Description
ser = pd.Series(pd.Categorical([1], categories=[1, 2, 3]))
print(ser)
# 0 1
# dtype: category
# Categories (3, int64): [1, 2, 3]
ser = ser.replace(1, None)
print(ser)
# 0 NaN
# dtype: category
# Categories (2, object): [2, 3]
One gets the same behavior if None
is replaced by pd.NA
, and similar behavior with np.nan
but this is coerced to float. Since the categories can be integers and the Series still hold NA values, it seems to me this shouldn't coerce to object.
ser = pd.Series(pd.Categorical([None], categories=[1, 2, 3]))
print(ser)
# 0 NaN
# dtype: category
# Categories (3, int64): [1, 2, 3]
Probably unrelated: #46884