StringDtype =========== .. cpp:class:: pandas::StringDtype Data type class for pandas extension types. Example ------- .. code-block:: cpp #include using namespace pandas; // Use StringDtype StringDtype obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``StringDtype() = default`` - pd_string_dtype.h:27 - Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - pd_string_dtype.h:50 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string kind() const override`` - std::string - pd_string_dtype.h:58 - :ref:`View ` * - ``bool matches(const std::string& dtype_name) const`` - bool - pd_string_dtype.h:74 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_string_dtype.h:34 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - pd_string_dtype.h:42 - * - ``std::string repr() const override`` - std::string - pd_string_dtype.h:87 - :ref:`View ` * - ``const std::type_info& type() const override`` - const std::type_info& - pd_string_dtype.h:65 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-stringdtype-kind-0: .. 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-stringdtype-matches-1: .. dropdown:: matches (pd_test_5_all.cpp:15741) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15731 :emphasize-lines: 11 for (size_t i = 0; i < 5; ++i) { // DatetimeIndex serializes individual entries via get_value_str() bdate_first5.push_back(idx.get_value_str(i)); } std::cout << " case_4 bdate_range first 5: "; for (auto& s : bdate_first5) std::cout << s << " "; std::cout << "\n case_4 expected first 5: "; for (auto& s : expected_visible_head) std::cout << s << " "; std::cout << "\n"; // First date matches (both start at 2023-01-04) pandas_tests::check(contains(bdate_first5[0], "2023-01-04"), "case_4.first_bdate_is_2023-01-04", local_fail); // Second date should differ: bdate gives 2023-01-05, fixture wants 2023-01-20 bool second_matches_expected = contains(bdate_first5[1], "2023-01-20"); pandas_tests::check(!second_matches_expected, "case_4.bdate_NOT_matching_filtered_fixture", local_fail); // The bdate second is the next business day: pandas_tests::check(contains(bdate_first5[1], "2023-01-05"), "case_4.bdate_second_is_2023-01-05_sequential", local_fail); .. _example-stringdtype-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-stringdtype-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-stringdtype-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