CategoricalDtype#

class numpy::CategoricalDtype#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

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

Constructors#

Signature

Location

Example

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

df_categorical_dtype.h:44

Indexing / Selection#

Signature

Return Type

Location

Example

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

const std::string&

df_categorical_dtype.h:161

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

int32_t

df_categorical_dtype.h:147

size_t itemsize() const override

size_t

df_categorical_dtype.h:71

View

Other Methods#

Signature

Return Type

Location

Example

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

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

df_categorical_dtype.h:114

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

bool

df_categorical_dtype.h:138

std::string kind() const override

std::string

df_categorical_dtype.h:79

std::string name() const override

std::string

df_categorical_dtype.h:57

View

size_t num_categories() const

size_t

df_categorical_dtype.h:129

numpy::DType numpy_dtype() const override

numpy::DType

df_categorical_dtype.h:64

bool ordered() const

bool

df_categorical_dtype.h:122

View

CategoricalDtype() : ordered\_(false)

CategoricalDtype() :

df_categorical_dtype.h:37

std::string repr() const override

std::string

df_categorical_dtype.h:94

const std::type_info& type() const override

const std::type_info&

df_categorical_dtype.h:86

View

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

CategoricalDtype

df_categorical_dtype.h:221

CategoricalDtype with_ordered(bool ordered) const

CategoricalDtype

df_categorical_dtype.h:214

Code Examples#

The following examples are extracted from the test suite.

itemsize (np_test_2_all.cpp:3540)
3530      }
3531
3532      // Test size
3533      if (!(size(arr) == 24)) {
3534          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)";
3535          std::cout << std::string("[FAIL] ") + description << std::endl;
3536          throw std::runtime_error(description);
3537      }
3538
3539      // Test itemsize
3540      if (!(itemsize(arr) == sizeof(double))) {
3541          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))";
3542          std::cout << std::string("[FAIL] ") + description << std::endl;
3543          throw std::runtime_error(description);
3544      }
3545
3546      // Test nbytes
3547      if (!(nbytes(arr) == 24 * sizeof(double))) {
3548          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))";
3549          std::cout << std::string("[FAIL] ") + description << std::endl;
3550          throw std::runtime_error(description);
name (np_test_1_all.cpp:24865)
24855      uint64_t before_restore = rng.next_uint64();
24856      rng.set_state(state);
24857      uint64_t after_restore = rng.next_uint64();
24858
24859      if (before_restore != after_restore) {
24860        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : state restore failed";
24861        throw std::runtime_error("np_test_bitgen_mt19937 failed: state");
24862      }
24863
24864      // Test name
24865      if (rng.name() != "MT19937") {
24866        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : incorrect name";
24867        throw std::runtime_error("np_test_bitgen_mt19937 failed: name");
24868      }
24869
24870      std::cout << " -> tests passed" << std::endl;
24871    }
24872
24873    // ============================================================================
24874    // PCG64 TESTS
24875    // ============================================================================
ordered (np_test_2_all.cpp:8640)
8630          if (indices.size() != n) {
8631            std::cout << "   IPP large lexsort:                                                                      -> [FAIL]";
8632            throw std::runtime_error("IPP large lexsort: FAILED - incorrect result size");
8633          }
8634
8635          // Verify sorting correctness
8636          for (size_t i = 1; i < indices.size(); ++i) {
8637            size_t prev_idx = indices[i - 1];
8638            size_t curr_idx = indices[i];
8639
8640            // Check if correctly ordered (second key primary)
8641            if (keys[1][curr_idx] < keys[1][prev_idx] ||
8642              (keys[1][curr_idx] == keys[1][prev_idx] && keys[0][curr_idx] < keys[0][prev_idx])) {
8643              std::cout << "   IPP large lexsort order:                                                                -> [FAIL]";
8644              throw std::runtime_error("IPP large lexsort: FAILED - incorrect ordering");
8645            }
8646          }
8647
8648          auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
8649          // std::cout << "[OK] IPP large lexsort (" << n << " elements): PASSED in " << duration_us << " μs" << std::endl;
8650        }
type (np_test_1_all.cpp:2240)
2230        std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 1}).is_type<std::string>())";
2231        std::cout << std::string("[FAIL] ") + description << std::endl;
2232        throw std::runtime_error(description);
2233    }
2234    if (!(multi_array.getElementAt({0, 2}).is_type<double>())) {
2235        std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 2}).is_type<double>())";
2236        std::cout << std::string("[FAIL] ") + description << std::endl;
2237        throw std::runtime_error(description);
2238    }
2239
2240    // Test 4: Arrays with all same type (homogeneous)
2241    NDArray<object_> homo_array = createObjectZeros({5});
2242    for (size_t i = 0; i < 5; ++i) {
2243        homo_array.setElementAt({i}, object_(static_cast<int>(i * 10)));
2244    }
2245
2246    // Verify all are same type
2247    for (size_t i = 0; i < 5; ++i) {
2248        object_ obj = homo_array.getElementAt({i});
2249        if (!(obj.is_type<int>())) {
2250            std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<int>())";