Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rhshadrach opened this issue Jan 19, 2023 · 1 comment · Fixed by #50857
Closed

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

rhshadrach opened this issue Jan 19, 2023 · 1 comment · Fixed by #50857
Labels
Bug Categorical Categorical Data Type replace replace method

Comments

@rhshadrach
Copy link
Member

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

@rhshadrach rhshadrach added Bug Categorical Categorical Data Type replace replace method labels Jan 19, 2023
@lukemanley
Copy link
Member

#50857 is an existing PR that fixes this. I added a test for this in that PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants