IntervalArray ============= .. cpp:class:: pandas::IntervalArray Extension array type for specialized data storage. Example ------- .. code-block:: cpp #include using namespace pandas; // Use IntervalArray IntervalArray obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``IntervalArray(const numpy::NDArray& left, const numpy::NDArray& right, const numpy::NDArray& mask, IntervalClosed closed = IntervalClosed::Right, bool copy = false)`` - pd_interval_array.h:124 - * - ``IntervalArray(const numpy::NDArray& left, const numpy::NDArray& right, IntervalClosed closed = IntervalClosed::Right)`` - pd_interval_array.h:144 - * - ``explicit IntervalArray(const std::vector>>& values, IntervalClosed closed = IntervalClosed::Right)`` - pd_interval_array.h:161 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static IntervalArray from_arrays(const std::vector& left, const std::vector& right, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - pd_interval_array.h:269 - :ref:`View ` * - ``static IntervalArray from_arrays(const numpy::NDArray& left, const numpy::NDArray& right, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - pd_interval_array.h:290 - :ref:`View ` * - ``static IntervalArray from_breaks(const std::vector& breaks, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - pd_interval_array.h:230 - :ref:`View ` * - ``static IntervalArray from_breaks(const numpy::NDArray& breaks, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - pd_interval_array.h:251 - :ref:`View ` * - ``static IntervalArray from_tuples(const std::vector>& tuples, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - pd_interval_array.h:303 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const numpy::NDArray& mask() const`` - const numpy::NDArray& - pd_interval_array.h:558 - :ref:`View ` * - ``IntervalArray take(const numpy::NDArray& indices) const`` - IntervalArray - pd_interval_array.h:1298 - :ref:`View ` Data Manipulation ----------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray dropna() const`` - IntervalArray - pd_interval_array.h:639 - :ref:`View ` Missing Data ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray fillna(const std::pair& fill_value) const`` - IntervalArray - pd_interval_array.h:620 - :ref:`View ` * - ``BooleanArray isna() const`` - BooleanArray - pd_interval_array.h:593 - :ref:`View ` * - ``BooleanArray notna() const`` - BooleanArray - pd_interval_array.h:606 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t count() const`` - size_t - pd_interval_array.h:661 - :ref:`View ` Arithmetic ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const std::string& subtype_override() const`` - const std::string& - pd_interval_array.h:393 - :ref:`View ` Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray left() const`` - numpy::NDArray - pd_interval_array.h:418 - :ref:`View ` * - ``size_t len() const`` - size_t - pd_interval_array.h:365 - :ref:`View ` * - ``numpy::NDArray length() const`` - numpy::NDArray - pd_interval_array.h:454 - :ref:`View ` Sorting ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray argsort() const`` - numpy::NDArray - pd_interval_array.h:985 - :ref:`View ` Combining --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static IntervalArray concat(const std::vector& arrays)`` - static IntervalArray - pd_interval_array.h:1362 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string() const`` - std::string - pd_interval_array.h:1265 - :ref:`View ` * - ``std::vector>> to_tuples() const`` - std::vector>> - pd_interval_array.h:858 - :ref:`View ` Conversion ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray copy() const`` - IntervalArray - pd_interval_array.h:1400 - :ref:`View ` Set Operations -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray unique() const`` - IntervalArray - pd_interval_array.h:1070 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``BooleanArray is_empty() const`` - BooleanArray - pd_interval_array.h:478 - :ref:`View ` * - ``bool is_left_closed() const`` - bool - pd_interval_array.h:400 - :ref:`View ` * - ``bool is_na(size_t index) const`` - bool - pd_interval_array.h:578 - :ref:`View ` * - ``bool is_non_overlapping_monotonic() const`` - bool - pd_interval_array.h:505 - :ref:`View ` * - ``bool is_right_closed() const`` - bool - pd_interval_array.h:407 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::int64 argmax() const`` - numpy::int64 - pd_interval_array.h:1042 - :ref:`View ` * - ``numpy::int64 argmin() const`` - numpy::int64 - pd_interval_array.h:1018 - :ref:`View ` * - ``IntervalClosed closed() const`` - IntervalClosed - pd_interval_array.h:372 - :ref:`View ` * - ``std::string closed_string() const`` - std::string - pd_interval_array.h:379 - :ref:`View ` * - ``BooleanArray contains(T value) const`` - BooleanArray - pd_interval_array.h:696 - :ref:`View ` * - ``BooleanArray contains(const numpy::NDArray& values) const`` - BooleanArray - pd_interval_array.h:734 - :ref:`View ` * - ``const numpy::NDArray& data_left() const`` - const numpy::NDArray& - pd_interval_array.h:544 - * - ``const numpy::NDArray& data_right() const`` - const numpy::NDArray& - pd_interval_array.h:551 - * - ``dtype_type dtype() const`` - dtype_type - pd_interval_array.h:323 - :ref:`View ` * - ``bool empty() const`` - bool - pd_interval_array.h:358 - :ref:`View ` * - ``std::pair, IntervalArray> factorize() const`` - std::pair, IntervalArray> - pd_interval_array.h:1124 - :ref:`View ` * - ``fmt_bound(oss, l)`` - - pd_interval_array.h:1254 - * - ``fmt_bound(oss, r)`` - - pd_interval_array.h:1256 - * - ``fmt_float(oss, l)`` - - pd_interval_array.h:1204 - * - ``fmt_float(oss, r)`` - - pd_interval_array.h:1206 - * - ``bool has_na() const`` - bool - pd_interval_array.h:674 - :ref:`View ` * - ``std::string interval_to_string(size_t index) const`` - std::string - pd_interval_array.h:1176 - * - ``oss << interval_to_string(i)`` - oss << - pd_interval_array.h:1274 - * - ``FloatingArray mid() const`` - FloatingArray - pd_interval_array.h:433 - :ref:`View ` * - ``size_t nbytes() const`` - size_t - pd_interval_array.h:337 - :ref:`View ` * - ``constexpr int ndim() const`` - constexpr int - pd_interval_array.h:344 - :ref:`View ` * - ``BooleanArray overlaps(T other_left, T other_right, IntervalClosed other_closed) const`` - BooleanArray - pd_interval_array.h:781 - :ref:`View ` * - ``BooleanArray overlaps(T other_left, T other_right) const`` - BooleanArray - pd_interval_array.h:829 - :ref:`View ` * - ``numpy::NDArray right() const`` - numpy::NDArray - pd_interval_array.h:425 - :ref:`View ` * - ``IntervalArray set_closed(IntervalClosed new_closed) const`` - IntervalArray - pd_interval_array.h:843 - :ref:`View ` * - ``IntervalArray set_closed(const std::string& new_closed) const`` - IntervalArray - pd_interval_array.h:850 - :ref:`View ` * - ``void set_subtype_override(const std::string& override)`` - void - pd_interval_array.h:386 - :ref:`View ` * - ``std::vector shape() const`` - std::vector - pd_interval_array.h:351 - :ref:`View ` * - ``size_t size() const`` - size_t - pd_interval_array.h:330 - :ref:`View ` * - ``IntervalArray slice(size_t start, size_t stop, size_t step = 1) const`` - IntervalArray - pd_interval_array.h:1325 - :ref:`View ` * - ``void validate_arrays()`` - void - pd_interval_array.h:57 - * - ``void validate_bounds()`` - void - pd_interval_array.h:84 - Internal Methods ---------------- *1 internal methods (prefixed with underscore)* Code Examples ------------- The following examples are extracted from the test suite. .. _example-intervalarray-from_arrays-0: .. dropdown:: from_arrays (pd_test_1_all.cpp:1994) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1984 :emphasize-lines: 11 // ============================================================================ // Test: from_arrays factory method // ============================================================================ void test_from_arrays() { std::cout << "========= IntervalArray: from_arrays ======================= "; std::vector left_vec = {0, 10, 20}; std::vector right_vec = {5, 15, 25}; auto arr = pandas::IntervalArrayInt64::from_arrays(left_vec, right_vec); if (arr.size() != 3) { std::cout << "[FAIL] : in test_from_arrays() : size" << std::endl; return; } auto interval1 = arr[1]; if (!interval1.has_value() || interval1->first != 10 || interval1->second != 15) { std::cout << "[FAIL] : in test_from_arrays() : interval values" << std::endl; return; .. _example-intervalarray-from_arrays-1: .. dropdown:: from_arrays (pd_test_1_all.cpp:1994) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1984 :emphasize-lines: 11 // ============================================================================ // Test: from_arrays factory method // ============================================================================ void test_from_arrays() { std::cout << "========= IntervalArray: from_arrays ======================= "; std::vector left_vec = {0, 10, 20}; std::vector right_vec = {5, 15, 25}; auto arr = pandas::IntervalArrayInt64::from_arrays(left_vec, right_vec); if (arr.size() != 3) { std::cout << "[FAIL] : in test_from_arrays() : size" << std::endl; return; } auto interval1 = arr[1]; if (!interval1.has_value() || interval1->first != 10 || interval1->second != 15) { std::cout << "[FAIL] : in test_from_arrays() : interval values" << std::endl; return; .. _example-intervalarray-from_breaks-2: .. dropdown:: from_breaks (pd_test_1_all.cpp:1955) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1945 :emphasize-lines: 11 } // ============================================================================ // Test: from_breaks factory method // ============================================================================ void test_from_breaks() { std::cout << "========= IntervalArray: from_breaks ======================= "; // Create from breaks std::vector breaks = {0.0, 1.0, 2.0, 3.0, 4.0}; auto arr = pandas::IntervalArrayFloat64::from_breaks(breaks); if (arr.size() != 4) { std::cout << "[FAIL] : in test_from_breaks() : size should be n-1" << std::endl; return; } // Check intervals auto interval0 = arr[0]; if (!interval0.has_value() || interval0->first != 0.0 || interval0->second != 1.0) { std::cout << "[FAIL] : in test_from_breaks() : first interval" << std::endl; .. _example-intervalarray-from_breaks-3: .. dropdown:: from_breaks (pd_test_1_all.cpp:1955) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1945 :emphasize-lines: 11 } // ============================================================================ // Test: from_breaks factory method // ============================================================================ void test_from_breaks() { std::cout << "========= IntervalArray: from_breaks ======================= "; // Create from breaks std::vector breaks = {0.0, 1.0, 2.0, 3.0, 4.0}; auto arr = pandas::IntervalArrayFloat64::from_breaks(breaks); if (arr.size() != 4) { std::cout << "[FAIL] : in test_from_breaks() : size should be n-1" << std::endl; return; } // Check intervals auto interval0 = arr[0]; if (!interval0.has_value() || interval0->first != 0.0 || interval0->second != 1.0) { std::cout << "[FAIL] : in test_from_breaks() : first interval" << std::endl; .. _example-intervalarray-from_tuples-4: .. dropdown:: from_tuples (pd_test_1_all.cpp:2022) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2012 :emphasize-lines: 11 // ============================================================================ void test_from_tuples() { std::cout << "========= IntervalArray: from_tuples ======================= "; std::vector> tuples = { {0.0, 1.5}, {1.5, 3.0}, {3.0, 4.5} }; auto arr = pandas::IntervalArrayFloat64::from_tuples(tuples); if (arr.size() != 3) { std::cout << "[FAIL] : in test_from_tuples() : size" << std::endl; return; } auto interval2 = arr[2]; if (!interval2.has_value() || interval2->first != 3.0 || interval2->second != 4.5) { std::cout << "[FAIL] : in test_from_tuples() : interval values" << std::endl; return; .. _example-intervalarray-mask-5: .. dropdown:: mask (pd_test_1_all.cpp:9119) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9109 :emphasize-lines: 11 void pd_test_datetime_mixin_array_constructor() { std::cout << "========= DatetimeTDMixin array constructor ========================="; // Create DatetimeArray with some values numpy::NDArray data(std::vector{3}); data.setElementAt({0}, numpy::datetime64(1000000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2001 data.setElementAt({1}, numpy::datetime64(1500000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2017 data.setElementAt({2}, numpy::datetime64(1600000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2020 numpy::NDArray mask(std::vector{3}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); mask.setElementAt({2}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeTDMixin idx(arr, "timestamps"); bool passed = (idx.size() == 3 && !idx.empty() && idx.name().has_value() && *idx.name() == "timestamps" && idx.inferred_type() == "datetime"); .. _example-intervalarray-take-6: .. dropdown:: take (pd_test_1_all.cpp:5903) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5893 :emphasize-lines: 11 // Inherited Operations Tests // ============================================================================ void pd_test_categorical_index_take() { std::cout << "========= inherited take =============================="; pandas::CategoricalArray arr({"a", "b", "c", "d"}); pandas::CategoricalIndex idx(arr); std::vector indices = {0, 2, 3}; pandas::ExtensionIndex taken = idx.take(indices); bool passed = (taken.size() == 3); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_take()" << std::endl; throw std::runtime_error("pd_test_categorical_index_take failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-intervalarray-dropna-7: .. dropdown:: dropna (pd_test_1_all.cpp:531) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 521 :emphasize-lines: 11 } // Test isna array numpy::NDArray na_mask = arr.isna(); if (na_mask.getSize() != 4) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : isna size != 4" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: isna size != 4"); } // Test dropna pandas::CategoricalArray dropped = arr.dropna(); if (dropped.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : dropna size != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: dropna size != 2"); } // Test fillna (fill with existing category) pandas::CategoricalArray filled = arr.fillna("a"); // 'a' is in categories if (filled.has_na()) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : fillna should have no NA" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: fillna should have no NA"); .. _example-intervalarray-fillna-8: .. dropdown:: fillna (pd_test_1_all.cpp:537) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 527 :emphasize-lines: 11 throw std::runtime_error("pd_test_categorical_array_na_handling failed: isna size != 4"); } // Test dropna pandas::CategoricalArray dropped = arr.dropna(); if (dropped.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : dropna size != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: dropna size != 2"); } // Test fillna (fill with existing category) pandas::CategoricalArray filled = arr.fillna("a"); // 'a' is in categories if (filled.has_na()) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : fillna should have no NA" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: fillna should have no NA"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_array_add_categories() { .. _example-intervalarray-isna-9: .. dropdown:: isna (pd_test_1_all.cpp:524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 514 :emphasize-lines: 11 throw std::runtime_error("pd_test_categorical_array_na_handling failed: has_na() should be true"); } // Test count (non-NA) if (arr.count() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : count() != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: count() != 2"); } // Test isna array numpy::NDArray na_mask = arr.isna(); if (na_mask.getSize() != 4) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : isna size != 4" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: isna size != 4"); } // Test dropna pandas::CategoricalArray dropped = arr.dropna(); if (dropped.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : dropna size != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: dropna size != 2"); .. _example-intervalarray-notna-10: .. dropdown:: notna (pd_test_1_all.cpp:6595) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6585 :emphasize-lines: 11 if (!na_mask.getElementAt({2, 1})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (2,1) should be true" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: isna at (2,1)"); } // Row 0, col 0 should NOT be NA if (na_mask.getElementAt({0, 0})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (0,0) should be false" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: isna at (0,0)"); } auto notna_mask = df_na.notna(); if (notna_mask.getElementAt({1, 0})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : notna at (1,0) should be false" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: notna at (1,0)"); } } // Test fillna { std::map> float_data; float_data["X"] = {1.0, std::nan(""), 3.0}; .. _example-intervalarray-count-11: .. dropdown:: count (pd_test_1_all.cpp:66) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 56 :emphasize-lines: 11 if (arr.is_na(0)) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(0) should be false" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(0) should be false"); } if (!arr.has_na()) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : has_na() should be true" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: has_na() should be true"); } if (arr.count() != 2) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : count() should be 2" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: count() should be 2"); } std::cout << " -> tests passed" << std::endl; } void pd_test_boolean_array_kleene_and() { std::cout << "========= BooleanArray: Kleene AND ======================= "; .. _example-intervalarray-subtype_override-12: .. dropdown:: subtype_override (pd_test_3_all.cpp:24889) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24879 :emphasize-lines: 11 return dataframe_tests_bdate_timedelta_range::pd_test_bdate_timedelta_range_main(); } // ------------------- pd_test_bdate_timedelta_range (end) --------------------------- // ------------------- pd_test_interval_type_inference (begin) --------------------------- namespace dataframe_tests_interval_type_inference { void pd_test_interval_type_inference_breaks_int() { std::cout << "========= interval_type_inference_breaks_int ======================= "; auto idx = pandas::IntervalIndex::from_breaks({0.0, 1.0, 2.0, 3.0}); if (idx.subtype_override() != "int64") throw std::runtime_error("expected subtype_override 'int64', got '" + idx.subtype_override() + "'"); std::string dtype = idx.dtype_name(); if (dtype.find("int64") == std::string::npos) throw std::runtime_error("expected dtype containing 'int64', got '" + dtype + "'"); std::string fmt = idx.format_interval(0); if (fmt.find('.') != std::string::npos) throw std::runtime_error("expected integer format without decimal, got '" + fmt + "'"); std::cout << " -> tests passed" << std::endl; } .. _example-intervalarray-left-13: .. dropdown:: left (pd_test_1_all.cpp:1909) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1899 :emphasize-lines: 11 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); pandas::IntervalArrayFloat64 arr1(left, right); if (arr1.size() != 3) { std::cout << "[FAIL] : in test_constructors() : array size" << std::endl; return; } .. _example-intervalarray-len-14: .. dropdown:: len (pd_test_3_all.cpp:20867) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20857 :emphasize-lines: 11 auto title_result = s.str().title(); if (title_result[0] != "Hello World" || title_result[1] != "Hello World" || title_result[2] != "Hello World") { std::cout << " [FAIL] : title() failed" << std::endl; throw std::runtime_error("pd_test_str_capitalize_title: title() failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Test str().len() // ============================================================================ void pd_test_str_len() { std::cout << "========= Series.str().len() ============================"; pandas::Series s({"a", "bb", "ccc", ""}); auto lens = s.str().len(); if (lens[0] != 1 || lens[1] != 2 || lens[2] != 3 || lens[3] != 0) { std::cout << " [FAIL] : len() failed" << std::endl; .. _example-intervalarray-length-15: .. dropdown:: length (pd_test_1_all.cpp:2137) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2127 :emphasize-lines: 11 auto mid0 = mid_arr[0]; auto mid1 = mid_arr[1]; auto mid2 = mid_arr[2]; if (!mid0.has_value() || std::abs(mid0.value() - 1.0) > 1e-10 || !mid1.has_value() || std::abs(mid1.value() - 3.5) > 1e-10 || !mid2.has_value() || std::abs(mid2.value() - 7.5) > 1e-10) { std::cout << "[FAIL] : in test_left_right_mid_length() : mid()" << std::endl; return; } // Test length() auto len_arr = arr.length(); if (len_arr.getElementAt({0}) != 2.0 || len_arr.getElementAt({1}) != 3.0 || len_arr.getElementAt({2}) != 5.0) { std::cout << "[FAIL] : in test_left_right_mid_length() : length()" << std::endl; return; } std::cout << "-> tests passed" << std::endl; } .. _example-intervalarray-argsort-16: .. dropdown:: argsort (pd_test_1_all.cpp:1304) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1294 :emphasize-lines: 11 std::cout << "========= DatetimeArray: sorting ======================= "; pandas::DatetimeArray arr(std::vector{ "2023-06-15", "NaT", "2023-01-01", "2023-12-31" }); // argsort ascending auto indices = arr.argsort(true, "last"); // Expected order: 2023-01-01(2), 2023-06-15(0), 2023-12-31(3), NaT(1) if (indices.getElementAt({0}) != 2) { std::cout << " [FAIL] : argsort: first should be index 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argsort first"); } if (indices.getElementAt({3}) != 1) { std::cout << " [FAIL] : argsort: last should be index 1 (NaT)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: NaT position"); } .. _example-intervalarray-concat-17: .. dropdown:: concat (pd_test_1_all.cpp:17717) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 17707 :emphasize-lines: 11 } void pd_test_period_index_concat() { std::cout << "========= concat factory =============================="; std::vector ordinals1 = {0, 1}; std::vector ordinals2 = {2, 3}; pandas::PeriodIndex idx1(ordinals1, "D"); pandas::PeriodIndex idx2(ordinals2, "D"); pandas::PeriodIndex concatenated = pandas::PeriodIndex::concat({idx1, idx2}); bool passed = (concatenated.size() == 4); if (!passed) { std::cout << " [FAIL] : in pd_test_period_index_concat()" << std::endl; throw std::runtime_error("pd_test_period_index_concat failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-intervalarray-to_string-18: .. dropdown:: to_string (pd_test_1_all.cpp:2693) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2683 :emphasize-lines: 11 pandas::PeriodArray arr_m(std::vector{ "2020-01", "NaT", "2025-06" }, "M"); // Year auto years = arr_m.year(); auto y0 = years[0]; if (!y0.has_value() || y0.value() != 2020) { std::cout << " [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]"); } auto y1 = years[1]; if (y1.has_value()) { std::cout << " [FAIL] : year[1] should be NA (NaT)" << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA"); } auto y2 = years[2]; .. _example-intervalarray-to_tuples-19: .. dropdown:: to_tuples (pd_test_1_all.cpp:13037) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13027 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void pd_test_interval_index_to_tuples() { std::cout << "========= to_tuples ========================="; auto idx = pandas::IntervalIndex64::from_breaks({0, 1, 2, 3}); auto tuples = idx.to_tuples(); bool passed = (tuples.size() == 3 && tuples[0].has_value() && tuples[0]->first == 0 && tuples[0]->second == 1 && tuples[1].has_value() && tuples[1]->first == 1 && tuples[1]->second == 2 && tuples[2].has_value() && tuples[2]->first == 2 && tuples[2]->second == 3); if (!passed) { std::cout << " [FAIL] : in pd_test_interval_index_to_tuples() : check failed" << std::endl; throw std::runtime_error("pd_test_interval_index_to_tuples failed"); } .. _example-intervalarray-copy-20: .. dropdown:: copy (pd_test_1_all.cpp:5798) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5788 :emphasize-lines: 11 // ============================================================================ // Copy/Rename Tests // ============================================================================ void pd_test_categorical_index_copy() { std::cout << "========= copy ========================================"; pandas::CategoricalArray arr({"a", "b", "c"}); pandas::CategoricalIndex idx(arr, "original"); pandas::CategoricalIndex copied = idx.copy(); bool passed = (copied.size() == idx.size() && copied.name() == idx.name() && copied.categories() == idx.categories() && copied.ordered() == idx.ordered()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_copy()" << std::endl; throw std::runtime_error("pd_test_categorical_index_copy failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-intervalarray-unique-21: .. dropdown:: unique (pd_test_1_all.cpp:1345) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1335 :emphasize-lines: 11 pandas::DatetimeArray arr(std::vector{ "2023-01-01", "2023-06-15", "2023-01-01", "NaT", "2023-06-15", "NaT" }); // unique auto uniq = arr.unique(); // Should have: NaT, 2023-01-01, 2023-06-15 (3 unique values) if (uniq.size() != 3) { std::cout << " [FAIL] : unique size should be 3, got " << uniq.size() << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: size"); } // factorize auto [codes, uniques] = arr.factorize(); // Codes for NaT should be -1 if (codes.getElementAt({3}) != -1) { .. _example-intervalarray-is_empty-22: .. dropdown:: is_empty (pd_test_1_all.cpp:2164) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2154 :emphasize-lines: 11 // Test with right-closed intervals (a, b] std::vector> tuples = { {0.0, 1.0}, // Not empty {1.0, 1.0}, // Empty (1, 1] has no points {2.0, 2.0}, // Empty {2.0, 3.0} // Not empty }; auto arr_right = pandas::IntervalArrayFloat64::from_tuples(tuples, pandas::IntervalClosed::Right); auto empty_right = arr_right.is_empty(); if (empty_right[0].value_or(true) != false || empty_right[1].value_or(false) != true || empty_right[2].value_or(false) != true || empty_right[3].value_or(true) != false) { std::cout << "[FAIL] : in test_is_empty() : right-closed" << std::endl; return; } // Test with both-closed intervals [a, b] - [1, 1] is NOT empty .. _example-intervalarray-is_left_closed-23: .. 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-intervalarray-is_na-24: .. dropdown:: is_na (pd_test_1_all.cpp:51) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 41 :emphasize-lines: 11 void pd_test_boolean_array_na_handling() { std::cout << "========= BooleanArray: NA handling ======================= "; pandas::BooleanArray arr({ std::optional(true), std::nullopt, // NA at index 1 std::optional(false) }); if (!arr.is_na(1)) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(1) should be true" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(1) should be true"); } if (arr.is_na(0)) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(0) should be false" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(0) should be false"); } if (!arr.has_na()) { .. _example-intervalarray-is_non_overlapping_monotonic-25: .. dropdown:: is_non_overlapping_monotonic (pd_test_1_all.cpp:2457) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2447 :emphasize-lines: 11 // ============================================================================ // Test: is_non_overlapping_monotonic // ============================================================================ void test_is_non_overlapping_monotonic() { std::cout << "========= IntervalArray: is_non_overlapping_monotonic ======================= "; // Monotonic, non-overlapping std::vector breaks1 = {0.0, 1.0, 2.0, 3.0}; auto arr1 = pandas::IntervalArrayFloat64::from_breaks(breaks1, pandas::IntervalClosed::Right); if (!arr1.is_non_overlapping_monotonic()) { std::cout << "[FAIL] : in test_is_non_overlapping_monotonic() : should be true for breaks" << std::endl; return; } // Non-monotonic (out of order) std::vector> tuples2 = { {2.0, 3.0}, {0.0, 1.0}, {1.0, 2.0} }; auto arr2 = pandas::IntervalArrayFloat64::from_tuples(tuples2); if (arr2.is_non_overlapping_monotonic()) { .. _example-intervalarray-is_right_closed-26: .. 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-intervalarray-argmax-27: .. dropdown:: argmax (pd_test_1_all.cpp:1323) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1313 :emphasize-lines: 11 } // argmin auto min_idx = arr.argmin(); if (!min_idx.has_value() || min_idx.value() != 2) { std::cout << " [FAIL] : argmin should be 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmin"); } // argmax auto max_idx = arr.argmax(); if (!max_idx.has_value() || max_idx.value() != 3) { std::cout << " [FAIL] : argmax should be 3 (2023-12-31)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmax"); } std::cout << " -> tests passed" << std::endl; } void pd_test_datetime_array_unique() { std::cout << "========= DatetimeArray: unique/factorize ======================= "; .. _example-intervalarray-argmin-28: .. dropdown:: argmin (pd_test_1_all.cpp:1316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1306 :emphasize-lines: 11 if (indices.getElementAt({0}) != 2) { std::cout << " [FAIL] : argsort: first should be index 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argsort first"); } if (indices.getElementAt({3}) != 1) { std::cout << " [FAIL] : argsort: last should be index 1 (NaT)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: NaT position"); } // argmin auto min_idx = arr.argmin(); if (!min_idx.has_value() || min_idx.value() != 2) { std::cout << " [FAIL] : argmin should be 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmin"); } // argmax auto max_idx = arr.argmax(); if (!max_idx.has_value() || max_idx.value() != 3) { std::cout << " [FAIL] : argmax should be 3 (2023-12-31)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmax"); .. _example-intervalarray-closed-29: .. 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-intervalarray-closed_string-30: .. 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-intervalarray-contains-31: .. dropdown:: contains (pd_test_1_all.cpp:2200) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2190 :emphasize-lines: 11 // Test: contains method // ============================================================================ void test_contains() { std::cout << "========= IntervalArray: contains ======================= "; std::vector breaks = {0.0, 1.0, 2.0, 3.0}; // Right-closed intervals: (0, 1], (1, 2], (2, 3] auto arr_right = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); // Test contains(1.0) - should be in interval 0 but not 1 (since 1 is exclusive on left of interval 1) auto contains_1 = arr_right.contains(1.0); // (0, 1] contains 1: yes, (1, 2] contains 1: no (open on left), (2, 3] contains 1: no if (contains_1[0].value_or(false) != true || contains_1[1].value_or(true) != false || contains_1[2].value_or(true) != false) { std::cout << "[FAIL] : in test_contains() : right-closed contains 1.0" << std::endl; return; } // Left-closed intervals: [0, 1), [1, 2), [2, 3) .. _example-intervalarray-contains-32: .. dropdown:: contains (pd_test_1_all.cpp:2200) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2190 :emphasize-lines: 11 // Test: contains method // ============================================================================ void test_contains() { std::cout << "========= IntervalArray: contains ======================= "; std::vector breaks = {0.0, 1.0, 2.0, 3.0}; // Right-closed intervals: (0, 1], (1, 2], (2, 3] auto arr_right = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); // Test contains(1.0) - should be in interval 0 but not 1 (since 1 is exclusive on left of interval 1) auto contains_1 = arr_right.contains(1.0); // (0, 1] contains 1: yes, (1, 2] contains 1: no (open on left), (2, 3] contains 1: no if (contains_1[0].value_or(false) != true || contains_1[1].value_or(true) != false || contains_1[2].value_or(true) != false) { std::cout << "[FAIL] : in test_contains() : right-closed contains 1.0" << std::endl; return; } // Left-closed intervals: [0, 1), [1, 2), [2, 3) .. _example-intervalarray-dtype-33: .. dropdown:: dtype (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-intervalarray-empty-34: .. dropdown:: empty (pd_test_1_all.cpp:941) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 931 :emphasize-lines: 11 #include "../pandas/pd_config.h" namespace dataframe_tests { namespace dataframe_tests_config { void pd_test_config_version() { std::cout << "========= df_config: version info ======================= "; const char* version = pandas::DataFrameInfo::version(); if (version == nullptr || std::string(version).empty()) { std::cout << "[FAIL] : in pd_test_config_version() : version is null or empty" << std::endl; throw std::runtime_error("pd_test_config_version failed: version is null or empty"); } std::cout << "-> tests passed" << std::endl; } void pd_test_config_na_repr() { std::cout << "========= df_config: NA representation ======================= "; const char* na_repr = pandas::DataFrameConfig::get_na_repr(); if (na_repr == nullptr) { .. _example-intervalarray-factorize-35: .. dropdown:: factorize (pd_test_1_all.cpp:1353) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1343 :emphasize-lines: 11 // unique auto uniq = arr.unique(); // Should have: NaT, 2023-01-01, 2023-06-15 (3 unique values) if (uniq.size() != 3) { std::cout << " [FAIL] : unique size should be 3, got " << uniq.size() << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: size"); } // factorize auto [codes, uniques] = arr.factorize(); // Codes for NaT should be -1 if (codes.getElementAt({3}) != -1) { std::cout << " [FAIL] : factorize: NaT code should be -1" << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: NaT code"); } // Same values should have same codes if (codes.getElementAt({0}) != codes.getElementAt({2})) { std::cout << " [FAIL] : factorize: 2023-01-01 values should have same code" << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: same code"); } .. _example-intervalarray-has_na-36: .. dropdown:: has_na (pd_test_1_all.cpp:61) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 51 :emphasize-lines: 11 if (!arr.is_na(1)) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(1) should be true" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(1) should be true"); } if (arr.is_na(0)) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(0) should be false" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(0) should be false"); } if (!arr.has_na()) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : has_na() should be true" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: has_na() should be true"); } if (arr.count() != 2) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : count() should be 2" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: count() should be 2"); } std::cout << " -> tests passed" << std::endl; .. _example-intervalarray-mid-37: .. dropdown:: mid (pd_test_1_all.cpp:2124) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2114 :emphasize-lines: 11 // Test right() auto right_arr = arr.right(); if (right_arr.getElementAt({0}) != 2.0 || right_arr.getElementAt({1}) != 5.0 || right_arr.getElementAt({2}) != 10.0) { std::cout << "[FAIL] : in test_left_right_mid_length() : right()" << std::endl; return; } // Test mid() auto mid_arr = arr.mid(); // (0+2)/2=1, (2+5)/2=3.5, (5+10)/2=7.5 auto mid0 = mid_arr[0]; auto mid1 = mid_arr[1]; auto mid2 = mid_arr[2]; if (!mid0.has_value() || std::abs(mid0.value() - 1.0) > 1e-10 || !mid1.has_value() || std::abs(mid1.value() - 3.5) > 1e-10 || !mid2.has_value() || std::abs(mid2.value() - 7.5) > 1e-10) { std::cout << "[FAIL] : in test_left_right_mid_length() : mid()" << std::endl; return; .. _example-intervalarray-nbytes-38: .. dropdown:: nbytes (pd_test_1_all.cpp:6214) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6204 :emphasize-lines: 11 } // Test empty DataFrame pandas::DataFrame empty_df; if (!empty_df.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : should be empty" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: should be empty"); } // Test nbytes > 0 for non-empty if (df.nbytes() == 0) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : nbytes should be > 0" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: nbytes should be > 0"); } // Test columns index if (df.columns().size() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : columns size != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: columns size != 3"); } .. _example-intervalarray-ndim-39: .. dropdown:: ndim (pd_test_1_all.cpp:6195) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6185 :emphasize-lines: 11 pandas::DataFrame df(data); // Test shape auto shape = df.shape(); if (shape.size() != 2 || shape[0] != 4 || shape[1] != 3) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : shape mismatch" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: shape mismatch"); } // Test ndim if (df.ndim() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : ndim != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: ndim != 2"); } // Test empty if (df.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : should not be empty" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: should not be empty"); } .. _example-intervalarray-overlaps-40: .. dropdown:: overlaps (pd_test_1_all.cpp:2244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2234 :emphasize-lines: 11 // Test: overlaps method // ============================================================================ void test_overlaps() { std::cout << "========= IntervalArray: overlaps ======================= "; std::vector breaks = {0.0, 2.0, 4.0, 6.0}; // Right-closed: (0, 2], (2, 4], (4, 6] auto arr = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); // Check overlap with (1, 3] auto overlap_1_3 = arr.overlaps(1.0, 3.0); // (0, 2] overlaps (1, 3]? Yes (share 1-2) // (2, 4] overlaps (1, 3]? Yes (share 2-3) // (4, 6] overlaps (1, 3]? No if (overlap_1_3[0].value_or(false) != true || overlap_1_3[1].value_or(false) != true || overlap_1_3[2].value_or(true) != false) { std::cout << "[FAIL] : in test_overlaps() : overlaps (1, 3]" << std::endl; return; } .. _example-intervalarray-overlaps-41: .. dropdown:: overlaps (pd_test_1_all.cpp:2244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2234 :emphasize-lines: 11 // Test: overlaps method // ============================================================================ void test_overlaps() { std::cout << "========= IntervalArray: overlaps ======================= "; std::vector breaks = {0.0, 2.0, 4.0, 6.0}; // Right-closed: (0, 2], (2, 4], (4, 6] auto arr = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); // Check overlap with (1, 3] auto overlap_1_3 = arr.overlaps(1.0, 3.0); // (0, 2] overlaps (1, 3]? Yes (share 1-2) // (2, 4] overlaps (1, 3]? Yes (share 2-3) // (4, 6] overlaps (1, 3]? No if (overlap_1_3[0].value_or(false) != true || overlap_1_3[1].value_or(false) != true || overlap_1_3[2].value_or(true) != false) { std::cout << "[FAIL] : in test_overlaps() : overlaps (1, 3]" << std::endl; return; } .. _example-intervalarray-right-42: .. dropdown:: right (pd_test_1_all.cpp:1910) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1900 :emphasize-lines: 11 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); pandas::IntervalArrayFloat64 arr1(left, right); if (arr1.size() != 3) { std::cout << "[FAIL] : in test_constructors() : array size" << std::endl; return; } .. _example-intervalarray-set_closed-43: .. dropdown:: set_closed (pd_test_1_all.cpp:2285) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2275 :emphasize-lines: 11 std::vector breaks = {0.0, 1.0, 2.0}; auto arr = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); if (arr.closed() != pandas::IntervalClosed::Right) { std::cout << "[FAIL] : in test_set_closed() : initial closure" << std::endl; return; } // Change to left-closed auto arr_left = arr.set_closed(pandas::IntervalClosed::Left); if (arr_left.closed() != pandas::IntervalClosed::Left) { std::cout << "[FAIL] : in test_set_closed() : set to Left" << std::endl; return; } // Original should be unchanged if (arr.closed() != pandas::IntervalClosed::Right) { std::cout << "[FAIL] : in test_set_closed() : original changed" << std::endl; return; } .. _example-intervalarray-set_closed-44: .. dropdown:: set_closed (pd_test_1_all.cpp:2285) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2275 :emphasize-lines: 11 std::vector breaks = {0.0, 1.0, 2.0}; auto arr = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); if (arr.closed() != pandas::IntervalClosed::Right) { std::cout << "[FAIL] : in test_set_closed() : initial closure" << std::endl; return; } // Change to left-closed auto arr_left = arr.set_closed(pandas::IntervalClosed::Left); if (arr_left.closed() != pandas::IntervalClosed::Left) { std::cout << "[FAIL] : in test_set_closed() : set to Left" << std::endl; return; } // Original should be unchanged if (arr.closed() != pandas::IntervalClosed::Right) { std::cout << "[FAIL] : in test_set_closed() : original changed" << std::endl; return; } .. _example-intervalarray-set_subtype_override-45: .. dropdown:: set_subtype_override (pd_test_3_all.cpp:24977) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24967 :emphasize-lines: 11 std::cout << "========= Interval repr float bounds ===================="; pandas::Interval iv(0.0, 1.5); if (iv.repr() != "Interval(0.0, 1.5, closed='right')") throw std::runtime_error("repr mismatch: " + iv.repr()); std::cout << " -> tests passed" << std::endl; } void pd_test_interval_repr_timedelta() { std::cout << "========= Interval repr timedelta subtype ==============="; pandas::Interval iv(0.0, 86400000000000.0); // 1 day in nanos iv.set_subtype_override("timedelta64[ns]"); std::string r = iv.repr(); if (r.find("Timedelta") == std::string::npos) throw std::runtime_error("expected Timedelta in repr: " + r); if (r.find("1 days") == std::string::npos) throw std::runtime_error("expected '1 days' in repr: " + r); std::cout << " -> tests passed" << std::endl; } void pd_test_interval_str_integer() { std::cout << "========= Interval to_string integer bounds ============="; .. _example-intervalarray-shape-46: .. dropdown:: shape (pd_test_1_all.cpp:6188) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6178 :emphasize-lines: 11 std::cout << "========= properties ======================="; std::map> data; data["A"] = {1.0, 2.0, 3.0, 4.0}; data["B"] = {5.0, 6.0, 7.0, 8.0}; data["C"] = {9.0, 10.0, 11.0, 12.0}; pandas::DataFrame df(data); // Test shape auto shape = df.shape(); if (shape.size() != 2 || shape[0] != 4 || shape[1] != 3) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : shape mismatch" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: shape mismatch"); } // Test ndim if (df.ndim() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : ndim != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: ndim != 2"); } .. _example-intervalarray-size-47: .. dropdown:: size (pd_test_1_all.cpp:22) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12 :emphasize-lines: 11 #include "../pandas/pd_boolean_array.h" namespace dataframe_tests { namespace dataframe_tests_boolean_array { void pd_test_boolean_array_constructors() { std::cout << "========= BooleanArray: constructors ======================= "; // Default constructor pandas::BooleanArray arr1; if (arr1.size() != 0) { std::cout << " [FAIL] : in pd_test_boolean_array_constructors() : default constructor size != 0" << std::endl; throw std::runtime_error("pd_test_boolean_array_constructors failed: default constructor size != 0"); } // Initializer list constructor pandas::BooleanArray arr2({ std::optional(true), std::optional(false), std::nullopt, std::optional(true) .. _example-intervalarray-slice-48: .. dropdown:: slice (pd_test_1_all.cpp:17546) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 17536 :emphasize-lines: 11 // ============================================================================ // Slicing / Indexing Tests // ============================================================================ void pd_test_period_index_slice() { std::cout << "========= slice method ================================"; std::vector ordinals = {0, 1, 2, 3, 4}; pandas::PeriodIndex idx(ordinals, "D"); pandas::PeriodIndex sliced = idx.slice(1, 4); bool passed = (sliced.size() == 3 && sliced[0].has_value() && *sliced[0] == 1); if (!passed) { std::cout << " [FAIL] : in pd_test_period_index_slice()" << std::endl; throw std::runtime_error("pd_test_period_index_slice failed"); } std::cout << " -> tests passed" << std::endl; }