CLN: make CategoricalIndex._create_categorical a classmethod by topper-123 · Pull Request #21618 · pandas-dev/pandas (original) (raw)
Currently, CategoricalIndex._create_categorical
is a staticmethod, and is being called internally using either instances or classes as its first argument, e.g.:
- in
_is_dtype_compat
an instance is supplied as the first argument, - in
__new__
a class is supplied as the first argument.
This is confusing and makes the code paths different depending on how the method is called. It makes it difficult to reason about the precise output of the method.
This PR cleans this up by making _create_categorical
a classmethod. This simplifies stuff, and we can also remove the method's first parameter when calling it.
Calling _create_categorical
unneccesarily is one reason for the slowness of #20395. After this cleanup PR I will do another that should get #20395 come down to 1.6 ms as well as give some other related performance improvements.