PERF/ENH: add fast astyping for Categorical by arw2019 · Pull Request #37355 · pandas-dev/pandas (original) (raw)
It's to fix the error message for CategoricalIndex
. If we don't catch TypeError
we end up with TypeError: Cannot cast Index to dtype float64
(below) versus something like TypeError: Cannot cast object to dtype float64
In [2]: idx = pd.CategoricalIndex(["a", "b", "c", "a", "b", "c"])
In [3]: idx.astype('float')
ValueError Traceback (most recent call last) /workspaces/pandas-arw2019/pandas/core/indexes/base.py in astype(self, dtype, copy) 700 try: --> 701 casted = self._values.astype(dtype, copy=copy) 702 except (TypeError, ValueError) as err:
ValueError: could not convert string to float: 'a'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last) in ----> 1 idx.astype('float')
/workspaces/pandas-arw2019/pandas/core/indexes/category.py in astype(self, dtype, copy) 369 @doc(Index.astype) 370 def astype(self, dtype, copy=True): --> 371 res_data = self._data.astype(dtype, copy=copy) 372 return Index(res_data, name=self.name) 373
/workspaces/pandas-arw2019/pandas/core/arrays/categorical.py in astype(self, dtype, copy) 427 # GH8628 (PERF): astype category codes instead of astyping array 428 try: --> 429 astyped_cats = self.categories.astype(dtype=dtype, copy=copy) 430 except (ValueError): 431 raise ValueError(
/workspaces/pandas-arw2019/pandas/core/indexes/base.py in astype(self, dtype, copy) 701 casted = self._values.astype(dtype, copy=copy) 702 except (TypeError, ValueError) as err: --> 703 raise TypeError( 704 f"Cannot cast {type(self).name} to dtype {dtype}" 705 ) from err
TypeError: Cannot cast Index to dtype float64