Skip to content
6 changes: 3 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,16 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
else:
# GH8628 (PERF): astype category codes instead of astyping array
try:
astyped_cats = self.categories.astype(dtype=dtype, copy=copy)
new_cats = extract_array(self.categories, extract_numpy=True)
new_cats = new_cats.astype(dtype=dtype, copy=copy)
except (
TypeError, # downstream error msg for CategoricalIndex is misleading
ValueError,
):
msg = f"Cannot cast {self.categories.dtype} dtype to {dtype}"
raise ValueError(msg)

astyped_cats = extract_array(astyped_cats, extract_numpy=True)
result = take_1d(astyped_cats, libalgos.ensure_platform_int(self._codes))
result = take_1d(new_cats, libalgos.ensure_platform_int(self._codes))

return result

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_astype(self, ordered):
tm.assert_numpy_array_equal(result, expected)

result = cat.astype(int)
expected = np.array(cat, dtype="int64")
expected = np.array(cat, dtype="int")
tm.assert_numpy_array_equal(result, expected)

result = cat.astype(float)
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_astype_categorical_to_other(self):
exp = Series(["a", "b", "b", "a", "a", "c", "c", "c"])
tm.assert_series_equal(cat.astype("str"), exp)
s2 = Series(Categorical(["1", "2", "3", "4"]))
exp2 = Series([1, 2, 3, 4]).astype("int64")
exp2 = Series([1, 2, 3, 4]).astype("int")
tm.assert_series_equal(s2.astype("int"), exp2)

# object don't sort correctly, so just compare that we have the same
Expand All @@ -94,6 +94,14 @@ def cmp(a, b):
result = ser.astype("object").astype(CategoricalDtype())
tm.assert_series_equal(result, roundtrip_expected)

def test_categorical_int_to_int32(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be named test_categorical_astype_to_int

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize on the any_int_dtype, ideally this also works on any_int_nullable_dtype (but not sure that will just work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - created a new fixture any_int_or_nullable_int_dtype

# GH 39402

df = DataFrame(data={"col1": [2.0, -1.0, 3.0]})
df.col1 = df.col1.astype("category")
df.col1 = df.col1.astype(np.int32)
assert df.col1.dtype.type is np.int32

def test_series_to_categorical(self):
# see gh-16524: test conversion of Series to Categorical
series = Series(["a", "b", "c"])
Expand Down