-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Series(category).replace to maintain order of categories #51057
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,3 +78,13 @@ def test_replace_categorical_ea_dtype(): | |
result = pd.Series(cat).replace(["a", "b"], ["c", pd.NA])._values | ||
expected = Categorical(pd.array(["c", pd.NA], dtype="string")) | ||
tm.assert_categorical_equal(result, expected) | ||
|
||
|
||
def test_replace_maintain_ordering(): | ||
# GH51016 | ||
dtype = pd.CategoricalDtype([0, 1, 2], ordered=True) | ||
ser = pd.Series([0, 1, 2], dtype=dtype) | ||
result = ser.replace(0, 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a test where a missing value is replaced? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any.
|
||
expected_dtype = pd.CategoricalDtype([1, 2], ordered=True) | ||
expected = pd.Series([2, 1, 2], dtype=expected_dtype) | ||
tm.assert_series_equal(expected, result, check_category_order=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do all this if
ordered=False
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.5x maintains the existing order of categories regardless of
ordered
so I'd lean towards keeping that behavior. The new ordering logic is barely measurable perf-wise relative to the whole replace operation.