FutureWarning: specifying 'categories' or 'ordered' in .astype() is deprecated; pass a CategoricalDtype instead

Viewed 6539

The warning in the title is produced by pandas 0.21.0 on Python 3.6.3 with code such as pd.Series(["a", "b", "b"]).astype("category", categories = ["a", "b", "c"]). How exactly is one supposed to write this now?

2 Answers
pd.Categorical(pd.Series(['a','b','b']), categories = ['a', 'b', 'c'])

Also you can use the ordered parameter to create a categorical hierarchy

result = pd.Categorical(pd.Series(['a','b','b']), categories = ['a', 'b', 'c'], ordered = True)

Update to convert to Series dtype

pd.Series(result)
Related