﻿# cuDF categorical codes: an integer is meaningful only with its category vocabulary

Preserve category vocabularies when moving cuDF categorical codes into model arrays, comparing partitions or reconstructing pandas categorical data.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cudf-category-code-vocabulary-identity/
Language: en

## Identify what an exported integer names

A preprocessing step extracts categorical codes from a GPU table and feeds them to an embedding lookup. The [cuDF categorical-code reference](https://docs.nvidia.com/cudf/26.10/cudf/api_docs/api/cudf.core.accessors.categorical.CategoricalAccessor.codes/) exposes the code series. The integer alone does not identify the semantic category without its vocabulary. Preserve the mapping at the boundary where the categorical column becomes an ordinary numerical array. Otherwise the model can receive a valid in-range index that refers to a different category than the producer intended.

## Compare vocabularies before comparing codes

The [pandas categorical guide](https://pandas.pydata.org/docs/user_guide/categorical.html) illustrates that the same category can have different codes in independently constructed categoricals, and that combining them can recode entries. Apply that distinction when designing a compatible GPU dataframe pipeline. Equal code values across partitions are not evidence of equal categories unless their vocabularies agree. Compare semantic labels or deliberately align the dictionaries before concatenating codes for a shared downstream model input.

## Keep class meaning separate from ordinal meaning

A numerical encoding is not automatically an ordered feature. Adding codes or measuring distance between them rarely expresses the relation between nominal categories. Decide whether the model expects an embedding index, an ordered category or a continuous magnitude. If an ordered relationship exists, record that relationship explicitly. A reordering that preserves labels can still change the integer representation used by a consumer that has incorrectly treated category position as a universal numerical scale.

## Reconstruct using the original dictionary contract

The [from_codes reference](https://pandas.pydata.org/docs/reference/api/pandas.Categorical.from_codes.html) defines construction from codes and categories or a categorical dtype. Use the relevant equivalent mapping contract when transferring an artifact between libraries. Preserve missing-value treatment separately from valid category positions. Do not reconstruct from a newly discovered alphabetic vocabulary while reusing codes produced against another order. The result may be structurally valid while assigning wrong labels to every affected observation.

## Test absent and reordered categories

Create partitions where a category is absent locally, dictionaries have different order and a value is missing. Confirm that semantic labels and the intended model indices survive alignment and export. The guides on [label-taxonomy version mapping](/journal/data-guide-label-taxonomy-version-mapping/) and [nullable feature contracts](/journal/data-guide-arrow-nullable-feature-contract/) provide related context. Store a versioned vocabulary with numerical code artifacts. Reproducibility depends on the relationship between an integer and its category, not only on retaining the integer buffer with its original dtype.
