Skip to content

Commit 686bf71

Browse files
committed
BUG: RangeIndex.astype('category')
1 parent f33480d commit 686bf71

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
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

+1-3
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -667,19 +667,19 @@ def test_astype_category(self, copy, name, ordered, simple_index):
667667
# standard categories
668668
dtype = CategoricalDtype(ordered=ordered)
669669
result = idx.astype(dtype, copy=copy)
670-
expected = CategoricalIndex(idx.values, name=name, ordered=ordered)
670+
expected = CategoricalIndex(idx, name=name, ordered=ordered)
671671
tm.assert_index_equal(result, expected)
672672

673673
# non-standard categories
674674
dtype = CategoricalDtype(idx.unique().tolist()[:-1], ordered)
675675
result = idx.astype(dtype, copy=copy)
676-
expected = CategoricalIndex(idx.values, name=name, dtype=dtype)
676+
expected = CategoricalIndex(idx, name=name, dtype=dtype)
677677
tm.assert_index_equal(result, expected)
678678

679679
if ordered is False:
680680
# dtype='category' defaults to ordered=False, so only test once
681681
result = idx.astype("category", copy=copy)
682-
expected = CategoricalIndex(idx.values, name=name)
682+
expected = CategoricalIndex(idx, name=name)
683683
tm.assert_index_equal(result, expected)
684684

685685
def test_is_unique(self, simple_index):

0 commit comments

Comments
 (0)