Použití dtypů Categorical a Enum

Scaling and Optimizing Data Pipelines with Polars

Liam Brannigan

Data Scientist & Polars Contributor

Kódování opakujících se řetězců

Diagram tabulky se sloupcem event_title a sloupcem area s opakujícími se řetězci.

Scaling and Optimizing Data Pipelines with Polars

Kódování opakujících se řetězců

Diagram tabulky se sloupcem event_title a sloupcem area, kde jsou řetězce zakódovány celými čísly.

Scaling and Optimizing Data Pipelines with Polars

Dataset s opakujícími se štítky

events = pl.read_parquet("chicago_events.parquet")
Scaling and Optimizing Data Pipelines with Polars

Dataset s opakujícími se štítky

events["area"].value_counts(sort=True).head(5)
shape: (5, 2)
| area          | count |
| ---           | ---   |
| str           | u32   |
|-------------  |-------|
| Loop          | 35982 |
| West Loop     | 27432 |
| Andersonville | 24660 |
| Downtown      | 24320 |
| Pullman       | 21985 |
Scaling and Optimizing Data Pipelines with Polars

Vytvoření sloupce Categorical

events_cat = events.with_columns(
    pl.col("area").cast(pl.Categorical)
)
Scaling and Optimizing Data Pipelines with Polars

Vytvoření sloupce Categorical

events_cat.select("event_title","area").head()
shape: (5, 2)
| event_title        | area          |
| ---                | ---           |
| str                | cat           |
|--------------------|---------------|
| Folk Festival      | Andersonville |
| Fireworks Night    | Downtown      |
| Greektown Market   | West Side     |
| Rail History Day   | Pullman       |
| Grant Park Concert | Loop          |
Scaling and Optimizing Data Pipelines with Polars

Vytvoření sloupce Categorical

events_cat.select("event_title","area").with_columns(
    pl.col("area").to_physical().alias("physical")
).head(3)
shape: (3, 3)
| event_title        | area          | physical |
| ---                | ---           | ---      |
| str                | cat           | u32      |
|--------------------|---------------|----------|
| Folk Festival      | Andersonville | 0        |
| Fireworks Night    | Downtown      | 1        |
| Greektown Market   | West Side     | 2        |
Scaling and Optimizing Data Pipelines with Polars

Použití výrazu pro Categorical

events_cat.select("event_title","area",
    pl.col("area").cat.starts_with("West").alias("westside"),
)
Scaling and Optimizing Data Pipelines with Polars

Použití výrazu pro Categorical

events_cat.select("event_title","area",
    pl.col("area").cat.starts_with("West").alias("westside"),
).filter(pl.col("westside")).head(2)
shape: (2, 3)
| event_title             | area      | westside |
| ---                     | ---       | ---      |
| str                     | cat       | bool     |
|-------------------------|-----------|----------|
| Greektown Market        | West Side | true     |
| West Loop Chef Showcase | West Loop | true     |
  • Ostatní operace s řetězci → přetypovat zpět na String
Scaling and Optimizing Data Pipelines with Polars

Categorical a Enum

Diagram tabulky se sloupcem event_title a sloupcem area, kde jsou řetězce zakódovány celými čísly.

Scaling and Optimizing Data Pipelines with Polars

Vytvoření sloupce Enum

area_enum = pl.Enum([
    "Albany Park",
    "Andersonville",
    ...
    "West Loop",
    "West Side",
    "Wicker Park",
])
Scaling and Optimizing Data Pipelines with Polars

Vytvoření sloupce Enum

events_enum = events.with_columns(
    pl.col("area").cast(area_enum)
)
Scaling and Optimizing Data Pipelines with Polars

Vytvoření sloupce Enum

events_enum.select("event_title", "area").head()
shape: (5, 2)
| event_title                  | area           |
| ---                          | ---            |
| str                          | enum           |
|------------------------------|----------------|
| Greektown Market             | West Side      |
| Folk Festival                | Andersonville  |
| West Loop Chef Showcase      | West Loop      |
| Rail History Day             | Pullman        |
| Fireworks Night              | Downtown       |
Scaling and Optimizing Data Pipelines with Polars

Prozkoumání dtypů Enum

events_enum.schema["area"]
Enum(categories=['Albany Park', 'Andersonville', 'Chinatown', 'Downtown', ...])
  • Enum pro pevnou slovní zásobu
  • Categorical pro proměnlivou slovní zásobu
Scaling and Optimizing Data Pipelines with Polars

Paměťová efektivita Enum

events_enum.select("event_title","area").with_columns(
    pl.col("area").to_physical().alias("physical")
).head(3)
shape: (3, 3)
| event_title        | area          | physical |
| ---                | ---           | ---      |
| str                | enum          | u8       |
|--------------------|---------------|----------|
| Folk Festival      | Andersonville | 1        |
| Fireworks Night    | Downtown      | 3        |
| Greektown Market   | West Side     | 16       |
Scaling and Optimizing Data Pipelines with Polars

Pojďme si procvičit!

Scaling and Optimizing Data Pipelines with Polars

Preparing Video For Download...