BUG: RangeIndex.astype('category') by topper-123 · Pull Request #41263 · pandas-dev/pandas (original) (raw)

There's currently inconsistent behaviour when converting RangeIndex to CategoricalIndex using different methods. This fixes that.

ridx = pd.RangeIndex(5) pd.CategoricalIndex(ridx).categories RangeIndex(start=0, stop=5, step=1) # both master and this PR ridx.astype("category").categories Int64Index([0, 1, 2, 3, 4], dtype='int64') # master RangeIndex(start=0, stop=5, step=1) # this PR

In general, when supplying a Index subclass to Categorical/CategoricalIndex, the new categories should be of that type (unless the supplied index is a CategoricalIndex itself).

Discovered working on tests for #41153.