Initializing playground…
← Back to roadmap

🏷️ Categorical Accessor — Interactive Playground

Manage categorical data — mirrors pandas.Categorical and Series.cat.
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · Categories and codes

Access series.cat.categories for the sorted unique labels and series.cat.codes for the integer encoding.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 · Nulls are encoded as -1

Missing values (null) are not counted as categories and get a code of -1.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 · Add and remove categories

Use addCategories() to register new labels (without needing them in the data yet) and removeCategories() to drop labels (values become null).

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 · Remove unused categories

removeUnusedCategories() drops any categories not present in the data.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 · Rename categories

Pass an object mapping old names → new names, or an array of replacement names.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

6 · Set and reorder categories

setCategories() replaces the entire category list (values not in the new list become null). reorderCategories() changes the order without adding or removing.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

7 · Value counts per category

valueCounts() returns a Series with the count of each category (zero for unused ones).

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

8 · Ordered categories

asOrdered() marks a categorical as ordered (enabling comparisons). The order is determined by the categories array index.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

API Reference

The .cat accessor is available on any Series containing string or categorical data. It provides pandas-compatible category management methods.

series.cat.categories    // Index of sorted unique labels
series.cat.codes         // Series<number> of integer codes (-1 for null)
series.cat.nCategories   // number of distinct categories
series.cat.ordered       // boolean — is the categorical ordered?

series.cat.addCategories(newCats: string[]): Series
series.cat.removeCategories(cats: string[]): Series
series.cat.removeUnusedCategories(): Series
series.cat.renameCategories(mapping: Record | string[]): Series
series.cat.setCategories(cats: string[]): Series
series.cat.reorderCategories(cats: string[], ordered?: boolean): Series
series.cat.asOrdered(): Series
series.cat.asUnordered(): Series
series.cat.valueCounts(): Series<number>