CategoricalDtype#

class pandas::CategoricalDtype#

Data type class for pandas extension types.

Example#

#include <pandas/pandas.h>
using namespace pandas;

// Use CategoricalDtype
CategoricalDtype obj;
// ... operations ...

Constructors#

Signature

Location

Example

CategoricalDtype(const std::vector<std::string>& categories, bool ordered = false, const std::string& categories_dtype = "object")

pd_categorical_dtype.h:47

Indexing / Selection#

Signature

Return Type

Location

Example

const std::string& get_category(int32_t code) const

const std::string&

pd_categorical_dtype.h:179

int32_t get_category_code(const std::string& value) const

int32_t

pd_categorical_dtype.h:165

Iteration#

Signature

Return Type

Location

Example

size_t itemsize() const override

size_t

pd_categorical_dtype.h:75

Other Methods#

Signature

Return Type

Location

Example

const std::vector<std::string>& categories() const

const std::vector<std::string>&

pd_categorical_dtype.h:118

View

const std::string& categories_dtype_str() const

const std::string&

pd_categorical_dtype.h:133

bool has_category(const std::string& value) const

bool

pd_categorical_dtype.h:156

View

std::string kind() const override

std::string

pd_categorical_dtype.h:83

View

std::string name() const override

std::string

pd_categorical_dtype.h:61

View

size_t num_categories() const

size_t

pd_categorical_dtype.h:147

View

numpy::DType numpy_dtype() const override

numpy::DType

pd_categorical_dtype.h:68

bool ordered() const

bool

pd_categorical_dtype.h:126

View

std::string repr() const override

std::string

pd_categorical_dtype.h:98

View

void set_categories_dtype(const std::string& dtype)

void

pd_categorical_dtype.h:140

const std::type_info& type() const override

const std::type_info&

pd_categorical_dtype.h:90

View

CategoricalDtype with_categories(const std::vector<std::string>& new_categories) const

CategoricalDtype

pd_categorical_dtype.h:244

CategoricalDtype with_ordered(bool ordered) const

CategoricalDtype

pd_categorical_dtype.h:237

Code Examples#

The following examples are extracted from the test suite.

categories (pd_test_1_all.cpp:389)
379        std::vector<std::optional<std::string>> vals = {
380            std::optional<std::string>("low"),
381            std::optional<std::string>("high"),
382            std::optional<std::string>("medium")
383        };
384        pandas::CategoricalArray arr3(vals, cats, true);  // ordered
385        if (!arr3.ordered()) {
386            std::cout << "  [FAIL] : in pd_test_categorical_array_constructors() : should be ordered" << std::endl;
387            throw std::runtime_error("pd_test_categorical_array_constructors failed: should be ordered");
388        }
389        if (arr3.categories().size() != 3) {
390            std::cout << "  [FAIL] : in pd_test_categorical_array_constructors() : categories size != 3" << std::endl;
391            throw std::runtime_error("pd_test_categorical_array_constructors failed: categories size != 3");
392        }
393
394        std::cout << " -> tests passed" << std::endl;
395    }
396
397    void pd_test_categorical_array_from_codes() {
398        std::cout << "========= CategoricalArray: from_codes ======================= ";
has_category (pd_test_1_all.cpp:5303)
5293}
5294
5295void pd_test_categorical_index_values_with_categories_constructor() {
5296    std::cout << "========= values with categories constructor ==========";
5297
5298    std::vector<std::optional<std::string>> values = {"a", "b", "a"};
5299    std::vector<std::string> categories = {"a", "b", "c", "d"};
5300    pandas::CategoricalIndex idx(values, categories, true, "explicit_cats");
5301
5302    bool passed = (idx.size() == 3 && idx.num_categories() == 4 &&
5303                   idx.ordered() && idx.has_category("c") && idx.has_category("d"));
5304    if (!passed) {
5305        std::cout << "  [FAIL] : in pd_test_categorical_index_values_with_categories_constructor()" << std::endl;
5306        throw std::runtime_error("pd_test_categorical_index_values_with_categories_constructor failed");
5307    }
5308
5309    std::cout << " -> tests passed" << std::endl;
5310}
5311
5312void pd_test_categorical_index_copy_constructor() {
5313    std::cout << "========= copy constructor ============================";
kind (pd_test_1_all.cpp:300)
290    void pd_test_boolean_array_dtype() {
291        std::cout << "========= BooleanArray: dtype ======================= ";
292
293        pandas::BooleanArray arr;
294        if (arr.dtype().name() != "boolean") {
295            std::cout << "  [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl;
296            throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name");
297        }
298
299        if (arr.dtype().kind() != "b") {
300            std::cout << "  [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl;
301            throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind");
302        }
303
304        std::cout << " -> tests passed" << std::endl;
305    }
306}
307
308int pd_test_boolean_array_main() {
309    std::cout << "====================================== running pd_test_boolean_array ==================================== " << std::endl;
name (pd_test_1_all.cpp:295)
285            throw std::runtime_error("pd_test_boolean_array_reductions failed: mean");
286        }
287
288        std::cout << " -> tests passed" << std::endl;
289    }
290
291    void pd_test_boolean_array_dtype() {
292        std::cout << "========= BooleanArray: dtype ======================= ";
293
294        pandas::BooleanArray arr;
295        if (arr.dtype().name() != "boolean") {
296            std::cout << "  [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl;
297            throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name");
298        }
299
300        if (arr.dtype().kind() != "b") {
301            std::cout << "  [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl;
302            throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind");
303        }
304
305        std::cout << " -> tests passed" << std::endl;
num_categories (pd_test_1_all.cpp:5285)
5275    std::cout << " -> tests passed" << std::endl;
5276}
5277
5278void pd_test_categorical_index_values_constructor() {
5279    std::cout << "========= values constructor ==========================";
5280
5281    std::vector<std::optional<std::string>> values = {"a", "b", "a", std::nullopt, "c"};
5282    pandas::CategoricalIndex idx(values, std::optional<std::string>("letters"), false);
5283
5284    bool passed = (idx.size() == 5 && idx.num_categories() == 3 &&
5285                   !idx.ordered() && idx.name().has_value() && *idx.name() == "letters");
5286    if (!passed) {
5287        std::cout << "  [FAIL] : in pd_test_categorical_index_values_constructor()" << std::endl;
5288        throw std::runtime_error("pd_test_categorical_index_values_constructor failed");
5289    }
5290
5291    std::cout << " -> tests passed" << std::endl;
5292}
5293
5294void pd_test_categorical_index_values_with_categories_constructor() {
ordered (pd_test_1_all.cpp:359)
349    void pd_test_categorical_array_constructors() {
350        std::cout << "========= CategoricalArray: constructors ======================= ";
351
352        // Default constructor
353        pandas::CategoricalArray arr1;
354        if (arr1.size() != 0) {
355            std::cout << "  [FAIL] : in pd_test_categorical_array_constructors() : default constructor size != 0" << std::endl;
356            throw std::runtime_error("pd_test_categorical_array_constructors failed: default constructor size != 0");
357        }
358        if (arr1.ordered()) {
359            std::cout << "  [FAIL] : in pd_test_categorical_array_constructors() : default should be unordered" << std::endl;
360            throw std::runtime_error("pd_test_categorical_array_constructors failed: default should be unordered");
361        }
362
363        // Constructor from values (infer categories)
364        std::vector<std::optional<std::string>> values = {
365            std::optional<std::string>("a"),
366            std::optional<std::string>("b"),
367            std::optional<std::string>("a"),
368            std::optional<std::string>("c")
repr (pd_test_1_all.cpp:10906)
10896    std::cout << " -> tests passed" << std::endl;
10897}
10898
10899void pd_test_extension_index_repr() {
10900    std::cout << "========= repr =========================";
10901
10902    pandas::CategoricalArray arr({"a", "b", "c"});
10903    // Use ExtensionIndex<CategoricalArray> directly to test base class repr
10904    pandas::ExtensionIndex<pandas::CategoricalArray> idx(arr, "test");
10905
10906    std::string repr_str = idx.repr();
10907
10908    bool passed = (!repr_str.empty() && repr_str.find("ExtensionIndex") != std::string::npos);
10909    if (!passed) {
10910        std::cout << "  [FAIL] : in pd_test_extension_index_repr() : repr check failed" << std::endl;
10911        throw std::runtime_error("pd_test_extension_index_repr failed");
10912    }
10913
10914    std::cout << " -> tests passed" << std::endl;
10915}
type (pd_test_3_all.cpp:15450)
15440/**
15441 * Test Series.convert_dtypes() parameter flags
15442 */
15443void pd_test_series_convert_dtypes_flags() {
15444    std::cout << "========= Series.convert_dtypes() flags =================";
15445
15446    // Test convert_integer=false - with floats disabled too, so it becomes string/object
15447    pandas::Series<std::string> s({"1", "2", "3"}, "numbers");
15448    auto converted = s.convert_dtypes(true, true, false, true, false);  // convert_integer=false, convert_floating=false
15449
15450    // Should remain object type (Series<std::string> has dtype_name()="object")
15451    // When integer and floating are both disabled for integer-like strings, it falls back to string type
15452    if (converted->dtype_name() != "object") {
15453        std::cout << "  [FAIL] : dtype should be object when convert_integer=false and convert_floating=false, got " << converted->dtype_name() << std::endl;
15454        throw std::runtime_error("pd_test_series_convert_dtypes_flags failed: convert_integer");
15455    }
15456
15457    // Test convert_boolean=false - strings stay as object/string type
15458    pandas::Series<std::string> s_bool({"true", "false"}, "bools");
15459    auto converted_bool = s_bool.convert_dtypes(true, true, true, false, true);  // convert_boolean=false