Getting "is_categorical is deprecated" error while using JamesSteinEncoder

Viewed 2594

Getting below error when trying to fit JamesSteinEncoder

encoder = JamesSteinEncoder().fit(X, y)

FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead
  elif pd.api.types.is_categorical(cols):

sklearn.__version__ :  '0.23.2'

Code:

speed = ['DE','AU','US','FR','GB','KR','AU']
lifespan = [1, 0, 1, 1, 1, 0, 1]
life = [1, 1, 0, 1, 1, 0, np.nan]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan,
                  'life':life}, index=index)

df['speed']= df['speed'].astype('category')

 from category_encoders import JamesSteinEncoder
    X = df['speed']
    y = df['lifespan']
    enc = JamesSteinEncoder().fit(X, y)

/Users/*/opt/anaconda3/envs/proj/lib/python3.7/site-packages/category_encoders/utils.py:21: FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead
  elif pd.api.types.is_categorical(cols):
1 Answers

Good thing about python is you can change the source code yourself. I did it, you can also do this:

Open file with vim, /Users/*/opt/anaconda3/envs/proj/lib/python3.7/sitepackages/category_encoders/utils.py Goto line 21 change

elif pd.api.types.is_categorical(cols):

to

elif pd.api.types.is_categorical_dtype(cols):

save the change and exit vim. and You are done. Now you have made the change for yourself and you won't get this warning again.

Related