You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think it would be great if pandas.core.categorical.Categorical.rename_categories could take a dict-like argument as an alternative to the index-like argument that it currently requires. This would make the syntax more similar to that of DataFrame.rename. The proposed change would allow for something like this:
Code Sample
>>>>df['categorical_var']
0apples1oranges2apples3apples>>>># rename categories from 'apples' and 'oranges' to 'red fruits' and 'orange fruits'>>>>df['categorical_var'].rename_categories({ 'apples': 'red fruits', 'oranges': 'orange fruits'})
0redfruits1orangefruits2redfruits3redfruits
Why?
1. Allows the developer to execute the rename without any prior knowledge of the order of the categories.
>>>>df['categorical_var']
0apples1oranges2apples3apples>>>># to figure out current category order>>>>print(df['categorical_var'].cat.categories)
['apples', 'oranges']
>>>># rename based on order>>>>df['categorical_var'].rename_categories(['red fruits', 'orange fruits'])
0redfruits1orangefruits2redfruits3redfruits
2. Allows the developer to much more easily rename one or a small subset of categories without having to worry about the rest of the category names.
@linusmarco : Thanks for reporting this! I think that makes good sense, especially since it would be consistent with DataFrame.rename. Have a look to see what you would need to do to make this work.
Proposal
I think it would be great if pandas.core.categorical.Categorical.rename_categories could take a dict-like argument as an alternative to the index-like argument that it currently requires. This would make the syntax more similar to that of DataFrame.rename. The proposed change would allow for something like this:
Code Sample
Why?
1. Allows the developer to execute the rename without any prior knowledge of the order of the categories.
This:
instead of this:
2. Allows the developer to much more easily rename one or a small subset of categories without having to worry about the rest of the category names.
This:
instead of this:
3. As I mentioned above, this also makes the syntax more similar to the DataFrame.rename syntax, which I think increases overall usability
I would be happy to start working on this if others agree that adding the feature makes sense.
The text was updated successfully, but these errors were encountered: