IntervalDtype ============= .. cpp:class:: pandas::IntervalDtype Data type class for pandas extension types. Example ------- .. code-block:: cpp #include using namespace pandas; // Use IntervalDtype IntervalDtype obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit IntervalDtype(IntervalClosed closed = IntervalClosed::Right)`` - pd_interval_dtype.h:89 - Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - pd_interval_dtype.h:135 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_left_closed() const`` - bool - pd_interval_dtype.h:171 - :ref:`View ` * - ``bool is_right_closed() const`` - bool - pd_interval_dtype.h:178 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalClosed closed() const`` - IntervalClosed - pd_interval_dtype.h:150 - :ref:`View ` * - ``std::string closed_string() const`` - std::string - pd_interval_dtype.h:157 - :ref:`View ` * - ``std::string kind() const override`` - std::string - pd_interval_dtype.h:143 - :ref:`View ` * - ``bool matches(const std::string& dtype_name) const`` - bool - pd_interval_dtype.h:196 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_interval_dtype.h:98 - :ref:`View ` * - ``std::string name(const std::string& subtype_override) const`` - std::string - pd_interval_dtype.h:118 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - pd_interval_dtype.h:128 - * - ``std::string repr() const override`` - std::string - pd_interval_dtype.h:205 - :ref:`View ` * - ``const std::type_info& type() const override`` - const std::type_info& - pd_interval_dtype.h:164 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-intervaldtype-is_left_closed-0: .. dropdown:: is_left_closed (pd_test_1_all.cpp:12830) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12820 :emphasize-lines: 11 } void pd_test_interval_index_is_left_right_closed() { std::cout << "========= is_left_closed/is_right_closed ========================="; auto idx_right = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Right); auto idx_left = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Left); auto idx_both = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Both); auto idx_neither = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Neither); bool passed = (!idx_right.is_left_closed() && idx_right.is_right_closed() && idx_left.is_left_closed() && !idx_left.is_right_closed() && idx_both.is_left_closed() && idx_both.is_right_closed() && !idx_neither.is_left_closed() && !idx_neither.is_right_closed()); if (!passed) { std::cout << " [FAIL] : in pd_test_interval_index_is_left_right_closed() : check failed" << std::endl; throw std::runtime_error("pd_test_interval_index_is_left_right_closed failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-intervaldtype-is_right_closed-1: .. dropdown:: is_right_closed (pd_test_1_all.cpp:12830) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12820 :emphasize-lines: 11 } void pd_test_interval_index_is_left_right_closed() { std::cout << "========= is_left_closed/is_right_closed ========================="; auto idx_right = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Right); auto idx_left = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Left); auto idx_both = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Both); auto idx_neither = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Neither); bool passed = (!idx_right.is_left_closed() && idx_right.is_right_closed() && idx_left.is_left_closed() && !idx_left.is_right_closed() && idx_both.is_left_closed() && idx_both.is_right_closed() && !idx_neither.is_left_closed() && !idx_neither.is_right_closed()); if (!passed) { std::cout << " [FAIL] : in pd_test_interval_index_is_left_right_closed() : check failed" << std::endl; throw std::runtime_error("pd_test_interval_index_is_left_right_closed failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-intervaldtype-closed-2: .. dropdown:: closed (pd_test_1_all.cpp:1903) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1893 :emphasize-lines: 11 // ============================================================================ void test_constructors() { std::cout << "========= IntervalArray: constructors ======================= "; // Default constructor pandas::IntervalArrayFloat64 empty; if (empty.size() != 0) { std::cout << "[FAIL] : in test_constructors() : default constructor size" << std::endl; return; } if (empty.closed() != pandas::IntervalClosed::Right) { std::cout << "[FAIL] : in test_constructors() : default closure" << std::endl; return; } // Constructor from left/right arrays numpy::NDArray left(std::vector{3}); numpy::NDArray right(std::vector{3}); left.setElementAt({0}, 0.0); right.setElementAt({0}, 1.0); left.setElementAt({1}, 1.0); right.setElementAt({1}, 2.0); left.setElementAt({2}, 2.0); right.setElementAt({2}, 3.0); .. _example-intervaldtype-closed_string-3: .. dropdown:: closed_string (pd_test_1_all.cpp:12813) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12803 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_interval_index_closed_string() { std::cout << "========= closed_string ========================="; auto idx_right = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Right); auto idx_left = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Left); bool passed = (idx_right.closed_string() == "right" && idx_left.closed_string() == "left"); if (!passed) { std::cout << " [FAIL] : in pd_test_interval_index_closed_string() : closed_string check failed" << std::endl; throw std::runtime_error("pd_test_interval_index_closed_string failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_interval_index_is_left_right_closed() { std::cout << "========= is_left_closed/is_right_closed ========================="; .. _example-intervaldtype-kind-4: .. 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-intervaldtype-matches-5: .. 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-intervaldtype-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-intervaldtype-name-7: .. 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-intervaldtype-repr-8: .. 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-intervaldtype-type-9: .. 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