Q: Error: astype() got an unexpected keyword argument 'categories'  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'.

A: 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


A: 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()


A: I think you missed a square bracket: df['Grades' should be df['Grades'].astype ...
Tested: 
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 = df['Grades'].astype('category',categories=['D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A', 'A+'],ordered=True)


print(df)

Output:
excellent    A+
excellent     A
excellent    A-
good         B+
good          B
good         B-
ok           C+
ok            C
ok           C-
poor         D+
poor          D
Name: Grades, dtype: category
Categories (11, object): [D < D+ < C- < C ... B+ < A- < A < A+]

