Skip to content

Commit f40d6ca

Browse files
committed
PERF/ENH: add fast astyping for categorical input
1 parent 901b1a7 commit f40d6ca

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

pandas/core/generic.py

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
ensure_str,
6767
is_bool,
6868
is_bool_dtype,
69+
is_categorical,
6970
is_datetime64_any_dtype,
7071
is_datetime64tz_dtype,
7172
is_dict_like,
@@ -5794,6 +5795,12 @@ def astype(
57945795
for i in range(len(self.columns))
57955796
]
57965797

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+
57975804
else:
57985805
# else, only a single dtype is given
57995806
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)

0 commit comments

Comments
 (0)