IndexDtype ========== .. cpp:class:: pandas::IndexDtype Data type class for pandas extension types. Example ------- .. code-block:: cpp #include using namespace pandas; // Create IndexDtype IndexDtype idx({1, 2, 3}, "my_index"); size_t len = idx.size(); Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - pd_index_dtype.h:109 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_datetime() const`` - bool - pd_index_dtype.h:213 - * - ``bool is_floating() const`` - bool - pd_index_dtype.h:199 - :ref:`View ` * - ``bool is_integer() const`` - bool - pd_index_dtype.h:192 - :ref:`View ` * - ``bool is_nullable() const override`` - bool - pd_index_dtype.h:138 - * - ``bool is_numeric() const`` - bool - pd_index_dtype.h:185 - :ref:`View ` * - ``bool is_object() const`` - bool - pd_index_dtype.h:206 - :ref:`View ` * - ``bool is_timedelta() const`` - bool - pd_index_dtype.h:220 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string inferred_type() const`` - std::string - pd_index_dtype.h:166 - :ref:`View ` * - ``std::string kind() const override`` - std::string - pd_index_dtype.h:118 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_index_dtype.h:39 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - pd_index_dtype.h:75 - * - ``std::string repr() const override`` - std::string - pd_index_dtype.h:154 - :ref:`View ` * - ``const std::type_info& type() const override`` - const std::type_info& - pd_index_dtype.h:146 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-indexdtype-is_floating-0: .. dropdown:: is_floating (pd_test_3_all.cpp:622) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 612 :emphasize-lines: 11 // Test with integer index pandas::IndexDtype int_dtype; if (!int_dtype.is_numeric()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be numeric" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric"); } if (!int_dtype.is_integer()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer"); } if (int_dtype.is_floating()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating"); } if (int_dtype.is_object()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object"); } // Test with float index pandas::IndexDtype float_dtype; .. _example-indexdtype-is_integer-1: .. dropdown:: is_integer (pd_test_3_all.cpp:618) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 608 :emphasize-lines: 11 void pd_test_3_all_index_dtype_checks() { std::cout << "========= IndexDtype.is_numeric/integer/floating/object() "; // Test with integer index pandas::IndexDtype int_dtype; if (!int_dtype.is_numeric()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be numeric" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric"); } if (!int_dtype.is_integer()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer"); } if (int_dtype.is_floating()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating"); } if (int_dtype.is_object()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object"); .. _example-indexdtype-is_numeric-2: .. dropdown:: is_numeric (pd_test_3_all.cpp:614) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 604 :emphasize-lines: 11 // ============================================================================ // Category 4: Index Type Checking (IndexDtype) // ============================================================================ void pd_test_3_all_index_dtype_checks() { std::cout << "========= IndexDtype.is_numeric/integer/floating/object() "; // Test with integer index pandas::IndexDtype int_dtype; if (!int_dtype.is_numeric()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be numeric" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric"); } if (!int_dtype.is_integer()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer"); } if (int_dtype.is_floating()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating"); .. _example-indexdtype-is_object-3: .. dropdown:: is_object (pd_test_3_all.cpp:626) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 616 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric"); } if (!int_dtype.is_integer()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer"); } if (int_dtype.is_floating()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating"); } if (int_dtype.is_object()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object"); } // Test with float index pandas::IndexDtype float_dtype; if (!float_dtype.is_numeric()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : float should be numeric" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: float is_numeric"); } .. _example-indexdtype-inferred_type-4: .. dropdown:: inferred_type (pd_test_1_all.cpp:5270) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5260 :emphasize-lines: 11 } void pd_test_categorical_index_array_constructor() { std::cout << "========= array constructor ==========================="; pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"}); pandas::CategoricalIndex idx(arr, "fruits"); bool passed = (idx.size() == 4 && !idx.empty() && idx.name().has_value() && *idx.name() == "fruits" && idx.inferred_type() == "categorical"); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_array_constructor()" << std::endl; throw std::runtime_error("pd_test_categorical_index_array_constructor failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_values_constructor() { std::cout << "========= values constructor =========================="; .. _example-indexdtype-kind-5: .. dropdown:: kind (pd_test_1_all.cpp:300) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 290 :emphasize-lines: 11 void pd_test_boolean_array_dtype() { std::cout << "========= BooleanArray: dtype ======================= "; pandas::BooleanArray arr; if (arr.dtype().name() != "boolean") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name"); } if (arr.dtype().kind() != "b") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind"); } std::cout << " -> tests passed" << std::endl; } } int pd_test_boolean_array_main() { std::cout << "====================================== running pd_test_boolean_array ==================================== " << std::endl; .. _example-indexdtype-name-6: .. dropdown:: name (pd_test_1_all.cpp:295) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 285 :emphasize-lines: 11 throw std::runtime_error("pd_test_boolean_array_reductions failed: mean"); } std::cout << " -> tests passed" << std::endl; } void pd_test_boolean_array_dtype() { std::cout << "========= BooleanArray: dtype ======================= "; pandas::BooleanArray arr; if (arr.dtype().name() != "boolean") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name"); } if (arr.dtype().kind() != "b") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind"); } std::cout << " -> tests passed" << std::endl; .. _example-indexdtype-repr-7: .. dropdown:: repr (pd_test_1_all.cpp:10906) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10896 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_repr() { std::cout << "========= repr ========================="; pandas::CategoricalArray arr({"a", "b", "c"}); // Use ExtensionIndex directly to test base class repr pandas::ExtensionIndex idx(arr, "test"); std::string repr_str = idx.repr(); bool passed = (!repr_str.empty() && repr_str.find("ExtensionIndex") != std::string::npos); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_repr() : repr check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_repr failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-indexdtype-type-8: .. dropdown:: type (pd_test_3_all.cpp:15450) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15440 :emphasize-lines: 11 /** * Test Series.convert_dtypes() parameter flags */ void pd_test_series_convert_dtypes_flags() { std::cout << "========= Series.convert_dtypes() flags ================="; // Test convert_integer=false - with floats disabled too, so it becomes string/object pandas::Series s({"1", "2", "3"}, "numbers"); auto converted = s.convert_dtypes(true, true, false, true, false); // convert_integer=false, convert_floating=false // Should remain object type (Series has dtype_name()="object") // When integer and floating are both disabled for integer-like strings, it falls back to string type if (converted->dtype_name() != "object") { std::cout << " [FAIL] : dtype should be object when convert_integer=false and convert_floating=false, got " << converted->dtype_name() << std::endl; throw std::runtime_error("pd_test_series_convert_dtypes_flags failed: convert_integer"); } // Test convert_boolean=false - strings stay as object/string type pandas::Series s_bool({"true", "false"}, "bools"); auto converted_bool = s_bool.convert_dtypes(true, true, true, false, true); // convert_boolean=false