Skip to content

Commit d580e85

Browse files
committed
BUG: RangeIndex.astype('category')
1 parent 734ca8a commit d580e85

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ Indexing
750750
- Bug in :meth:`DatetimeIndex.insert` when inserting ``np.datetime64("NaT")`` into a timezone-aware index incorrectly treating the timezone-naive value as timezone-aware (:issue:`39769`)
751751
- Bug in incorrectly raising in :meth:`Index.insert`, when setting a new column that cannot be held in the existing ``frame.columns``, or in :meth:`Series.reset_index` or :meth:`DataFrame.reset_index` instead of casting to a compatible dtype (:issue:`39068`)
752752
- Bug in :meth:`RangeIndex.append` where a single object of length 1 was concatenated incorrectly (:issue:`39401`)
753+
- Bug in :meth:`RangeIndex.astype` where when converting to :class:`CategoricalIndex`, the categories became a :class:`Int64Index` instead of a :class:`RangeIndex` (:issue:`xxxxx`)
753754
- Bug in setting ``numpy.timedelta64`` values into an object-dtype :class:`Series` using a boolean indexer (:issue:`39488`)
754755
- Bug in setting numeric values into a into a boolean-dtypes :class:`Series` using ``at`` or ``iat`` failing to cast to object-dtype (:issue:`39582`)
755756
- Bug in :meth:`DataFrame.loc.__setitem__` when setting-with-expansion incorrectly raising when the index in the expanding axis contains duplicates (:issue:`40096`)

pandas/core/indexes/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,7 @@ def astype(self, dtype, copy=True):
907907
elif is_categorical_dtype(dtype):
908908
from pandas.core.indexes.category import CategoricalIndex
909909

910-
return CategoricalIndex(
911-
self._values, name=self.name, dtype=dtype, copy=copy
912-
)
910+
return CategoricalIndex(self, name=self.name, dtype=dtype, copy=copy)
913911

914912
elif is_extension_array_dtype(dtype):
915913
return Index(np.asarray(self), name=self.name, dtype=dtype, copy=copy)

pandas/tests/indexes/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,19 +672,19 @@ def test_astype_category(self, copy, name, ordered, simple_index):
672672
# standard categories
673673
dtype = CategoricalDtype(ordered=ordered)
674674
result = idx.astype(dtype, copy=copy)
675-
expected = CategoricalIndex(idx.values, name=name, ordered=ordered)
675+
expected = CategoricalIndex(idx, name=name, ordered=ordered)
676676
tm.assert_index_equal(result, expected)
677677

678678
# non-standard categories
679679
dtype = CategoricalDtype(idx.unique().tolist()[:-1], ordered)
680680
result = idx.astype(dtype, copy=copy)
681-
expected = CategoricalIndex(idx.values, name=name, dtype=dtype)
681+
expected = CategoricalIndex(idx, name=name, dtype=dtype)
682682
tm.assert_index_equal(result, expected)
683683

684684
if ordered is False:
685685
# dtype='category' defaults to ordered=False, so only test once
686686
result = idx.astype("category", copy=copy)
687-
expected = CategoricalIndex(idx.values, name=name)
687+
expected = CategoricalIndex(idx, name=name)
688688
tm.assert_index_equal(result, expected)
689689

690690
def test_is_unique(self, simple_index):

0 commit comments

Comments
 (0)