SparseDtype =========== .. cpp:class:: pandas::SparseDtype Data type class for pandas extension types. Example ------- .. code-block:: cpp #include using namespace pandas; // Use SparseDtype SparseDtype obj; // ... operations ... Arithmetic ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string subtype_name() const`` - std::string - pd_sparse_dtype.h:156 - Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - pd_sparse_dtype.h:132 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_default_fill_value() const`` - bool - pd_sparse_dtype.h:196 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static T default_fill_value()`` - static T - pd_sparse_dtype.h:80 - * - ``T fill_value() const`` - T - pd_sparse_dtype.h:148 - :ref:`View ` * - ``SparseDtype() : fill_value_(default_fill_value())`` - SparseDtype() : - pd_sparse_dtype.h:66 - * - ``explicit SparseDtype(T fill_value) : fill_value_(fill_value)`` - explicit SparseDtype(T fill_value) : - pd_sparse_dtype.h:72 - * - ``std::string kind() const override`` - std::string - pd_sparse_dtype.h:140 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_sparse_dtype.h:101 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - pd_sparse_dtype.h:125 - * - ``std::string repr() const override`` - std::string - pd_sparse_dtype.h:189 - :ref:`View ` * - ``void set_fill_value_display(const std::string& display)`` - void - pd_sparse_dtype.h:95 - * - ``void set_subtype_name(const std::string& name)`` - void - pd_sparse_dtype.h:93 - * - ``const std::type_info& type() const override`` - const std::type_info& - pd_sparse_dtype.h:163 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-sparsedtype-fill_value-0: .. dropdown:: fill_value (pd_test_1_all.cpp:3229) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3219 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_sparse_array_fill_value_property() { std::cout << "========= SparseArray: fill_value property ======================= "; std::vector data = {-1, 5, -1, 10, -1}; pandas::SparseArray arr(data, static_cast(-1)); if (arr.fill_value() != -1) { std::cout << " [FAIL] : in pd_test_sparse_array_fill_value_property() : fill_value != -1" << std::endl; throw std::runtime_error("pd_test_sparse_array_fill_value_property failed: fill_value != -1"); } // Test default fill_value for float (NaN) pandas::SparseArray arr_float; if (!std::isnan(arr_float.fill_value())) { std::cout << " [FAIL] : in pd_test_sparse_array_fill_value_property() : default float fill_value should be NaN" << std::endl; throw std::runtime_error("pd_test_sparse_array_fill_value_property failed: default float fill_value should be NaN"); } .. _example-sparsedtype-kind-1: .. 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-sparsedtype-name-2: .. 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-sparsedtype-repr-3: .. 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-sparsedtype-type-4: .. 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