1.7.11 Categorical variables🔗ℹ

Neither data-frame nor tabular-asa has a categorical type, short of recoding the strings as numbers, which the post’s author once did. A 'categorical column stores each string once and reads back as symbols. When the categories are known up front, as the three New York airports in origin are, define-enum declares them, in order, as an Enum:

> (define-enum nyc-airport EWR JFK LGA)
> (define coded
    (with-columns flights
      (cast "carrier" 'categorical)
      (cast "dest" 'categorical)
      (cast "origin" nyc-airport)))
> (for/list ([name '("carrier" "dest" "origin")])
    (dtype (ref coded name)))

'(categorical categorical (enum EWR JFK LGA))

> (~> coded (ref "dest") (head 3) series->list)

'(IAH IAH MIA)

> (~> coded (filter (= (col "origin") 'JFK)) height)

1819

> (~> coded
      (group-by "origin")
      (agg (alias (mean "dep_delay") "mean_delay"))
      (sort "origin"))

shape: (3, 2)

┌────────┬────────────┐

│ origin ┆ mean_delay │

│ ---    ┆ ---        │

│ enum   ┆ f64        │

╞════════╪════════════╡

│ EWR    ┆ 18.297525  │

│ JFK    ┆ 16.611142  │

│ LGA    ┆ 14.715965  │

└────────┴────────────┘

A value outside the Enum is an error:

> (cast (series '(EWR TEB)) nyc-airport)

cast: 1 of 2 values is not in '(enum EWR JFK LGA): TEB

read-csv’s #:schema-overrides loads a column as categorical directly. API gap: not as an Enum (#133). See Categorical data and enums.