FutureWarning:不推荐在.astype()中指定'categories'或'ordered'; 传递一个CategoricalDtype

Kod*_*ist 9 python pandas

标题中的警告是由Python 3.6.3上的pandas 0.21.0生成的,其代码如下pd.Series(["a", "b", "b"]).astype("category", categories = ["a", "b", "c"]).现在应该怎么写这个呢?

Kod*_*ist 13

CategoricalDtype在警告中提到可作为pd.api.types.CategoricalDtype.所以,你可以写pd.Series(["a", "b", "b"]).astype(pd.api.types.CategoricalDtype(categories = ["a", "b", "c"])).

  • 不明白为什么它变得更复杂/更长? (3认同)
  • @randomSampling是的,设计不佳。 (2认同)