草庐IT

CategoricalDtype

全部标签

python - FutureWarning : specifying 'categories' or 'ordered' in . astype() 已弃用;改为传递 CategoricalDtype

标题中的警告是由pandas0.21.0在Python3.6.3上产生的,代码如pd.Series(["a","b","b"]).astype("category",类别=["a","b","c"])。现在应该怎么写这个? 最佳答案 警告中提到的CategoricalDtype可用pd.api.types.CategoricalDtype.所以,你可以这样写pd.Series(["a","b","b"]).astype(pd.api.types.CategoricalDtype(categories=["a","b","c"])).