Skip to content

BUG: Series.replace with categorical dtype and null values coerces to object #50872

Closed
@rhshadrach

Description

@rhshadrach
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions