pandas.Series.cat.to_dense#
- Series.cat.to_dense()[source]#
Return the values as an array with the dtype of the categories.
Unlike
np.asarray, this keeps extension dtypes such asInt64,periodor timezone-aware datetimes. Unlikeastype(categories.dtype), it does not raise when there are missing values and the categories’ dtype cannot hold them; the result is then upcast as inSeries.reindex()(e.g.int64tofloat64).- Returns:
- np.ndarray or ExtensionArray
The decoded values.
See also
Categorical.astypeCast to a specified dtype.
Categorical.__array__Convert to a NumPy array.
Examples
>>> cat = pd.Categorical(pd.array([1, 2, None], dtype="Int64")) >>> cat.to_dense() <IntegerArray> [1, 2, <NA>] Length: 3, dtype: Int64
>>> cat = pd.Categorical([1, 2, None]) >>> cat.to_dense() array([ 1., 2., nan])