IndexDtype#
-
class pandas::IndexDtype#
Data type class for pandas extension types.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Create IndexDtype
IndexDtype<int64_t> idx({1, 2, 3}, "my_index");
size_t len = idx.size();
Iteration#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
size_t |
pd_index_dtype.h:109 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
pd_index_dtype.h:213 |
|
|
bool |
pd_index_dtype.h:199 |
|
|
bool |
pd_index_dtype.h:192 |
|
|
bool |
pd_index_dtype.h:138 |
|
|
bool |
pd_index_dtype.h:185 |
|
|
bool |
pd_index_dtype.h:206 |
|
|
bool |
pd_index_dtype.h:220 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::string |
pd_index_dtype.h:166 |
|
|
std::string |
pd_index_dtype.h:118 |
|
|
std::string |
pd_index_dtype.h:39 |
|
|
numpy::DType |
pd_index_dtype.h:75 |
|
|
std::string |
pd_index_dtype.h:154 |
|
|
const std::type_info& |
pd_index_dtype.h:146 |
Code Examples#
The following examples are extracted from the test suite.
is_floating (pd_test_3_all.cpp:622)
612 // Test with integer index
613 pandas::IndexDtype<numpy::int64> int_dtype;
614 if (!int_dtype.is_numeric()) {
615 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be numeric" << std::endl;
616 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric");
617 }
618 if (!int_dtype.is_integer()) {
619 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl;
620 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer");
621 }
622 if (int_dtype.is_floating()) {
623 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl;
624 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating");
625 }
626 if (int_dtype.is_object()) {
627 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl;
628 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object");
629 }
630
631 // Test with float index
632 pandas::IndexDtype<double> float_dtype;
is_integer (pd_test_3_all.cpp:618)
608void pd_test_3_all_index_dtype_checks() {
609 std::cout << "========= IndexDtype.is_numeric/integer/floating/object() ";
610
611 // Test with integer index
612 pandas::IndexDtype<numpy::int64> int_dtype;
613 if (!int_dtype.is_numeric()) {
614 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be numeric" << std::endl;
615 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric");
616 }
617 if (!int_dtype.is_integer()) {
618 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl;
619 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer");
620 }
621 if (int_dtype.is_floating()) {
622 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl;
623 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating");
624 }
625 if (int_dtype.is_object()) {
626 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl;
627 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object");
is_numeric (pd_test_3_all.cpp:614)
604// ============================================================================
605// Category 4: Index Type Checking (IndexDtype)
606// ============================================================================
607
608void pd_test_3_all_index_dtype_checks() {
609 std::cout << "========= IndexDtype.is_numeric/integer/floating/object() ";
610
611 // Test with integer index
612 pandas::IndexDtype<numpy::int64> int_dtype;
613 if (!int_dtype.is_numeric()) {
614 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be numeric" << std::endl;
615 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric");
616 }
617 if (!int_dtype.is_integer()) {
618 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl;
619 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer");
620 }
621 if (int_dtype.is_floating()) {
622 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl;
623 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating");
is_object (pd_test_3_all.cpp:626)
616 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric");
617 }
618 if (!int_dtype.is_integer()) {
619 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl;
620 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer");
621 }
622 if (int_dtype.is_floating()) {
623 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl;
624 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating");
625 }
626 if (int_dtype.is_object()) {
627 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl;
628 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object");
629 }
630
631 // Test with float index
632 pandas::IndexDtype<double> float_dtype;
633 if (!float_dtype.is_numeric()) {
634 std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : float should be numeric" << std::endl;
635 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: float is_numeric");
636 }
inferred_type (pd_test_1_all.cpp:5270)
5260}
5261
5262void pd_test_categorical_index_array_constructor() {
5263 std::cout << "========= array constructor ===========================";
5264
5265 pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"});
5266 pandas::CategoricalIndex idx(arr, "fruits");
5267
5268 bool passed = (idx.size() == 4 && !idx.empty() &&
5269 idx.name().has_value() && *idx.name() == "fruits" &&
5270 idx.inferred_type() == "categorical");
5271 if (!passed) {
5272 std::cout << " [FAIL] : in pd_test_categorical_index_array_constructor()" << std::endl;
5273 throw std::runtime_error("pd_test_categorical_index_array_constructor failed");
5274 }
5275
5276 std::cout << " -> tests passed" << std::endl;
5277}
5278
5279void pd_test_categorical_index_values_constructor() {
5280 std::cout << "========= values 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;
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