Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas astype categories not working

Tags:

pandas

I tired to change column to catgeory using documentation from http://pandas.pydata.org/pandas-docs/stable/categorical.html

df = pd.DataFrame({'A':[1,2,3,4,5], 'B':['a','b','c','d','e'], 'C':['A','B','A','B','A']})
df['C']=df['C'].astype('category')

If I try to pass the categories

df['C']=df['C'].astype('category',categories=['A','B'])

It errors out saying

TypeError: _astype() got an unexpected keyword argument 'categories'

whats the right way to pass categories to astype()?

like image 269
Sudhir Jain Avatar asked Jun 21 '16 18:06

Sudhir Jain


1 Answers

You now need to now pass it in via CategorialDtype as the astype method no longer accepts them

from pandas.api.types import CategoricalDtype
df = pd.DataFrame({'A':[1,2,3,4,5], 'B':['a','b','c','d','e'], 'C':['A','B','A','B','A']})
df['C']=df['C'].astype(CategoricalDtype(categories=['A','B']))
like image 98
cts Avatar answered Sep 22 '22 21:09

cts