Treat CategoricalDtype as a predefined vocabulary for a pandas column: declare every permitted value before converting the data. section 12.0
Allow a future category
A plain categorical Series rejects a later assignment such as s.loc[7] = 'd'. Define the complete category set first, then convert with it: abcd_category = pd.CategoricalDtype(['a', 'b', 'c', 'd']); s = Series(['a', 'b', 'c', 'a', 'b', 'c', 'c', 'c']).astype(abcd_category); s.loc[7] = 'd'. The assignment succeeds because 'd' was declared in advance. section 12.0
Apply a schema to a column
For Olympic medal data, define medals_category = pd.CategoricalDtype(['Gold', 'Bronze', 'Silver']) and assign df['Medal'] = df['Medal'].astype(medals_category). The same pattern supplies a controlled categorical type to an existing column. section 12.0
Table 12.1 What you need to know (view table figure)
Use the categorical-data entry to connect explicit type conversion with related pandas memory and data-file operations. section 12.0
