We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7dcaaf4 commit 5d82b02Copy full SHA for 5d82b02
pandas/core/generic.py
@@ -66,6 +66,7 @@
66
ensure_str,
67
is_bool,
68
is_bool_dtype,
69
+ is_categorical,
70
is_datetime64_any_dtype,
71
is_datetime64tz_dtype,
72
is_dict_like,
@@ -5794,6 +5795,12 @@ def astype(
5794
5795
for i in range(len(self.columns))
5796
]
5797
5798
+ elif is_categorical(self):
5799
+ new_data = pd.Categorical.from_codes(
5800
+ self.cat.codes, categories=self.cat.categories.astype(dtype)
5801
+ )
5802
+ return self._constructor(new_data).__finalize__(self, method="astype")
5803
+
5804
else:
5805
# else, only a single dtype is given
5806
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
0 commit comments