IntervalDtype#

class pandas::IntervalDtype#

Data type class for pandas extension types.

Example#

#include <pandas/pandas.h>
using namespace pandas;

// Use IntervalDtype
IntervalDtype obj;
// ... operations ...

Constructors#

Signature

Location

Example

explicit IntervalDtype(IntervalClosed closed = IntervalClosed::Right)

pd_interval_dtype.h:89

Iteration#

Signature

Return Type

Location

Example

size_t itemsize() const override

size_t

pd_interval_dtype.h:135

Type Checking#

Signature

Return Type

Location

Example

bool is_left_closed() const

bool

pd_interval_dtype.h:171

View

bool is_right_closed() const

bool

pd_interval_dtype.h:178

View

Other Methods#

Signature

Return Type

Location

Example

IntervalClosed closed() const

IntervalClosed

pd_interval_dtype.h:150

View

std::string closed_string() const

std::string

pd_interval_dtype.h:157

View

std::string kind() const override

std::string

pd_interval_dtype.h:143

View

bool matches(const std::string& dtype_name) const

bool

pd_interval_dtype.h:196

View

std::string name() const override

std::string

pd_interval_dtype.h:98

View

std::string name(const std::string& subtype_override) const

std::string

pd_interval_dtype.h:118

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

View

const std::type_info& type() const override

const std::type_info&

pd_interval_dtype.h:164

View

Code Examples#

The following examples are extracted from the test suite.

is_left_closed (pd_test_1_all.cpp:12830)
12820}
12821
12822void pd_test_interval_index_is_left_right_closed() {
12823    std::cout << "========= is_left_closed/is_right_closed =========================";
12824
12825    auto idx_right = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Right);
12826    auto idx_left = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Left);
12827    auto idx_both = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Both);
12828    auto idx_neither = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Neither);
12829
12830    bool passed = (!idx_right.is_left_closed() && idx_right.is_right_closed() &&
12831                   idx_left.is_left_closed() && !idx_left.is_right_closed() &&
12832                   idx_both.is_left_closed() && idx_both.is_right_closed() &&
12833                   !idx_neither.is_left_closed() && !idx_neither.is_right_closed());
12834    if (!passed) {
12835        std::cout << "  [FAIL] : in pd_test_interval_index_is_left_right_closed() : check failed" << std::endl;
12836        throw std::runtime_error("pd_test_interval_index_is_left_right_closed failed");
12837    }
12838
12839    std::cout << " -> tests passed" << std::endl;
12840}
is_right_closed (pd_test_1_all.cpp:12830)
12820}
12821
12822void pd_test_interval_index_is_left_right_closed() {
12823    std::cout << "========= is_left_closed/is_right_closed =========================";
12824
12825    auto idx_right = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Right);
12826    auto idx_left = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Left);
12827    auto idx_both = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Both);
12828    auto idx_neither = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Neither);
12829
12830    bool passed = (!idx_right.is_left_closed() && idx_right.is_right_closed() &&
12831                   idx_left.is_left_closed() && !idx_left.is_right_closed() &&
12832                   idx_both.is_left_closed() && idx_both.is_right_closed() &&
12833                   !idx_neither.is_left_closed() && !idx_neither.is_right_closed());
12834    if (!passed) {
12835        std::cout << "  [FAIL] : in pd_test_interval_index_is_left_right_closed() : check failed" << std::endl;
12836        throw std::runtime_error("pd_test_interval_index_is_left_right_closed failed");
12837    }
12838
12839    std::cout << " -> tests passed" << std::endl;
12840}
closed (pd_test_1_all.cpp:1903)
1893// ============================================================================
1894void test_constructors() {
1895    std::cout << "========= IntervalArray: constructors ======================= ";
1896
1897    // Default constructor
1898    pandas::IntervalArrayFloat64 empty;
1899    if (empty.size() != 0) {
1900        std::cout << "[FAIL] : in test_constructors() : default constructor size" << std::endl;
1901        return;
1902    }
1903    if (empty.closed() != pandas::IntervalClosed::Right) {
1904        std::cout << "[FAIL] : in test_constructors() : default closure" << std::endl;
1905        return;
1906    }
1907
1908    // Constructor from left/right arrays
1909    numpy::NDArray<numpy::float64> left(std::vector<size_t>{3});
1910    numpy::NDArray<numpy::float64> right(std::vector<size_t>{3});
1911    left.setElementAt({0}, 0.0);  right.setElementAt({0}, 1.0);
1912    left.setElementAt({1}, 1.0);  right.setElementAt({1}, 2.0);
1913    left.setElementAt({2}, 2.0);  right.setElementAt({2}, 3.0);
closed_string (pd_test_1_all.cpp:12813)
12803    std::cout << " -> tests passed" << std::endl;
12804}
12805
12806void pd_test_interval_index_closed_string() {
12807    std::cout << "========= closed_string =========================";
12808
12809    auto idx_right = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Right);
12810    auto idx_left = pandas::IntervalIndex64::from_breaks({0, 1, 2}, pandas::IntervalClosed::Left);
12811
12812    bool passed = (idx_right.closed_string() == "right" && idx_left.closed_string() == "left");
12813    if (!passed) {
12814        std::cout << "  [FAIL] : in pd_test_interval_index_closed_string() : closed_string check failed" << std::endl;
12815        throw std::runtime_error("pd_test_interval_index_closed_string failed");
12816    }
12817
12818    std::cout << " -> tests passed" << std::endl;
12819}
12820
12821void pd_test_interval_index_is_left_right_closed() {
12822    std::cout << "========= is_left_closed/is_right_closed =========================";
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;
matches (pd_test_5_all.cpp:15741)
15731    for (size_t i = 0; i < 5; ++i) {
15732        // DatetimeIndex serializes individual entries via get_value_str()
15733        bdate_first5.push_back(idx.get_value_str(i));
15734    }
15735    std::cout << "  case_4 bdate_range first 5: ";
15736    for (auto& s : bdate_first5) std::cout << s << " ";
15737    std::cout << "\n  case_4 expected first 5:    ";
15738    for (auto& s : expected_visible_head) std::cout << s << " ";
15739    std::cout << "\n";
15740
15741    // First date matches (both start at 2023-01-04)
15742    pandas_tests::check(contains(bdate_first5[0], "2023-01-04"),
15743          "case_4.first_bdate_is_2023-01-04", local_fail);
15744
15745    // Second date should differ: bdate gives 2023-01-05, fixture wants 2023-01-20
15746    bool second_matches_expected = contains(bdate_first5[1], "2023-01-20");
15747    pandas_tests::check(!second_matches_expected,
15748          "case_4.bdate_NOT_matching_filtered_fixture", local_fail);
15749    // The bdate second is the next business day:
15750    pandas_tests::check(contains(bdate_first5[1], "2023-01-05"),
15751          "case_4.bdate_second_is_2023-01-05_sequential", local_fail);
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;
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