Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: astype() got an unexpected keyword argument 'categories'

Tags:

python

pandas

 df = pd.DataFrame(['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D'],
                      index=['excellent', 'excellent', 'excellent', 'good', 'good', 'good', 'ok', 'ok', 'ok', 'poor', 'poor'])
    df.rename(columns={0: 'Grades'}, inplace=True)
    df

I am trying to create an ordered category from the above dataframe using the following code -

df = df['Grades'].astype('category',categories=['D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A', 'A+'],ordered=True)

However it gives the error : astype() got an unexpected keyword argument 'categories'.

like image 260
Kakarot_7 Avatar asked May 20 '20 08:05

Kakarot_7


2 Answers

From pandas 0.25+ are removed these arguments:

Removed the previously deprecated ordered and categories keyword arguments in astype (GH17742)


In newer pandas versions is necesary use CategoricalDtype and pass to astype:

from pandas.api.types import CategoricalDtype

cats = ['D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A', 'A+']
cat_type = CategoricalDtype(categories=cats, ordered=True)
df['Grades'] = df['Grades'].astype(cat_type)
print (df)
             Grades
excellent     A+
excellent      A
excellent     A-
good          B+
good           B
good          B-
ok            C+
ok             C
ok            C-
poor          D+
poor           D

Or use Categorical:

df['Grades'] = pd.Categorical(df['Grades'], categories=cats, ordered=True)
print (df)
             Grades
excellent     A+
excellent      A
excellent     A-
good          B+
good           B
good          B-
ok            C+
ok             C
ok            C-
poor          D+
poor           D
like image 82
jezrael Avatar answered Oct 30 '22 05:10

jezrael


I think the issue is related to your Panda's version

try this:

import pandas as pd
import numpy as np
from pandas.api.types import CategoricalDtype



df = pd.DataFrame(['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D'],
                  index=['excellent', 'excellent', 'excellent', 'good', 'good', 'good', 'ok', 'ok', 'ok', 'poor', 'poor'])
df.rename(columns={0: 'Grades'}, inplace=True)
df
grades = df['Grades'].astype(CategoricalDtype(categories = ['D','D+', 
                                                       'C-', 'C', 'C+',
                                                       'B-', 'B', 'B+',
                                                       'A-', 'A', 'A+'],
                                                      ordered=True))
grades.head()
like image 36
kha Avatar answered Oct 30 '22 05:10

kha