pandas.Categorical.to_dense#

Categorical.to_dense()[source]#

Return the values as an array with the dtype of the categories.

Unlike np.asarray, this keeps extension dtypes such as Int64, period or timezone-aware datetimes. Unlike astype(categories.dtype), it does not raise when there are missing values and the categories’ dtype cannot hold them; the result is then upcast as in Series.reindex() (e.g. int64 to float64).

Returns:
np.ndarray or ExtensionArray

The decoded values.

See also

Categorical.astype

Cast 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])