You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's easy to make a Series categorical by using .astype('category'), and this yields the same result as calling the constructor with dtype='category'.
The same call fails for Index:
s = pd.Series(['a', 'b', 'a'])
sc = pd.Series(['a', 'b', 'a'], dtype='category')
s.astype('category')
# 0 a
# 1 b
# 2 a
# dtype: category
# Categories (2, object): [a, b]
# # both ways yield the same result
sc.equals(s.astype('category'))
# True
# # For Index ...
t = pd.Index(s.values)
t
# Index(['a', 'b', 'a'], dtype='object')
# # ... the direct constructor works
tc = pd.Index(['a', 'b', 'a'], dtype='category')
# # ... but not the conversion
t.astype('category')
# TypeError: data type "category" not understood
The text was updated successfully, but these errors were encountered:
If I understand correctly, this looks to be fixed on master by #18677:
In [2]: pd.__version__Out[2]: '0.23.0.dev0+807.g563a6ad'In [3]: idx=pd.Index(list('aba'))
In [4]: idxOut[4]: Index(['a', 'b', 'a'], dtype='object')
In [5]: idx.astype('category')
Out[5]: CategoricalIndex(['a', 'b', 'a'], categories=['a', 'b'], ordered=False, dtype='category')
With the same procedure failing on 0.22.0:
In [2]: pd.__version__Out[2]: '0.22.0'In [3]: idx=pd.Index(list('aba'))
In [4]: idxOut[4]: Index(['a', 'b', 'a'], dtype='object')
In [5]: idx.astype('category')
---------------------------------------------------------------------------TypeError: data type "category"notunderstood
It's easy to make a Series categorical by using
.astype('category')
, and this yields the same result as calling the constructor withdtype='category'
.The same call fails for Index:
The text was updated successfully, but these errors were encountered: