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?
See CategoricalDtype for more. An empty CategoricalDtype with a specific dtype can be created by providing an empty index. As follows, An Index containing the unique categories allowed.
FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning. In the series, I will store pandas timestamps, which I don't know what's the string to use for the dtype. I can't find it. How can I remove those annoying warnings?
This class is useful for specifying the type of a Categorical independent of the values. See CategoricalDtype for more. An empty CategoricalDtype with a specific dtype can be created by providing an empty index.
pandas.api.types.CategoricalDtype (categories = None, ordered = None) : This class is useful for specifying the type of Categorical data independent of the values, with categories and orderness. categories : [index like] Unique categorization of the categories. ordered : [boolean] If false, then the categorical is treated as unordered.
The CategoricalDtype
mentioned in the warning is available as pd.api.types.CategoricalDtype
. So, you can write pd.Series(["a", "b", "b"]).astype(pd.api.types.CategoricalDtype(categories = ["a", "b", "c"]))
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With