Skip to content

LabelEncoder ignores pandas CategoricalDtype order #12086

@avibrazil

Description

@avibrazil

The order of labels of pandas’ categorical features as CategoricalDtype(order=True) might be used by estimators. For example:

print(houses['quality'].unique())
[poor, fair, typical, good, excellent]
Categories (4, object): [poor < fair < typical < good < excellent]

Note how order is embedded in the data type above.

I was expecting codes like these:
0 poor
1 fair
2 typical
3 good
4 excellent

And I'm sure estimators would provide more meaningful results if such order was used.

But LabelEncoder gives random integer codes, probably using data as it comes:
3 poor
1 fair
0 typical
4 good
2 excellent

Thank you in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementModerateAnything that requires some knowledge of conventions and best practices

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions