Attrs#

class pandas::Attrs#

Utility class for pandas operations.

Example#

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

// Use Attrs
Attrs obj;
// ... operations ...

Constructors#

Signature

Location

Example

Attrs() = default

pd_attrs.h:52

Attrs(const Attrs&) = default

pd_attrs.h:57

Attrs(Attrs&&) = default

pd_attrs.h:62

Indexing / Selection#

Signature

Return Type

Location

Example

T get(const std::string& key) const

T

pd_attrs.h:103

View

T get(const std::string& key, T default_value) const

T

pd_attrs.h:119

View

Combining#

Signature

Return Type

Location

Example

void merge(const Attrs& other, bool overwrite = true)

void

pd_attrs.h:217

View

Conversion#

Signature

Return Type

Location

Example

Attrs copy() const

Attrs

pd_attrs.h:229

View

Iteration#

Signature

Return Type

Location

Example

std::vector<std::string> keys() const

std::vector<std::string>

pd_attrs.h:188

View

Other Methods#

Signature

Return Type

Location

Example

void clear()

void

pd_attrs.h:164

View

bool contains(const std::string& key) const

bool

pd_attrs.h:79

View

bool empty() const

bool

pd_attrs.h:180

View

bool has_type(const std::string& key) const

bool

pd_attrs.h:204

View

bool remove(const std::string& key)

bool

pd_attrs.h:157

View

void set(const std::string& key, T&& value)

void

pd_attrs.h:90

View

size_t size() const

size_t

pd_attrs.h:172

View

bool try_get(const std::string& key, T& out_value) const

bool

pd_attrs.h:139

Code Examples#

The following examples are extracted from the test suite.

get (pd_test_1_all.cpp:10290)
10280void pd_test_extension_index_get_loc_unique() {
10281    std::cout << "========= get_loc (unique) =========================";
10282
10283    pandas::CategoricalArray arr({"apple", "banana", "cherry"});
10284    pandas::CategoricalIndex idx(arr);
10285
10286    auto loc_apple = idx.get_loc("apple");
10287    auto loc_banana = idx.get_loc("banana");
10288    auto loc_cherry = idx.get_loc("cherry");
10289
10290    bool passed = (std::holds_alternative<size_t>(loc_apple) && std::get<size_t>(loc_apple) == 0 &&
10291                   std::get<size_t>(loc_banana) == 1 &&
10292                   std::get<size_t>(loc_cherry) == 2);
10293    if (!passed) {
10294        std::cout << "  [FAIL] : in pd_test_extension_index_get_loc_unique() : get_loc check failed" << std::endl;
10295        throw std::runtime_error("pd_test_extension_index_get_loc_unique failed");
10296    }
10297
10298    std::cout << " -> tests passed" << std::endl;
10299}
get (pd_test_1_all.cpp:10290)
10280void pd_test_extension_index_get_loc_unique() {
10281    std::cout << "========= get_loc (unique) =========================";
10282
10283    pandas::CategoricalArray arr({"apple", "banana", "cherry"});
10284    pandas::CategoricalIndex idx(arr);
10285
10286    auto loc_apple = idx.get_loc("apple");
10287    auto loc_banana = idx.get_loc("banana");
10288    auto loc_cherry = idx.get_loc("cherry");
10289
10290    bool passed = (std::holds_alternative<size_t>(loc_apple) && std::get<size_t>(loc_apple) == 0 &&
10291                   std::get<size_t>(loc_banana) == 1 &&
10292                   std::get<size_t>(loc_cherry) == 2);
10293    if (!passed) {
10294        std::cout << "  [FAIL] : in pd_test_extension_index_get_loc_unique() : get_loc check failed" << std::endl;
10295        throw std::runtime_error("pd_test_extension_index_get_loc_unique failed");
10296    }
10297
10298    std::cout << " -> tests passed" << std::endl;
10299}
merge (pd_test_1_all.cpp:13639)
13629namespace dataframe_tests {
13630    namespace dataframe_tests_joining {
13631
13632        // Helper to check approximate equality
13633        bool approx_equal(double a, double b, double tol = 1e-9) {
13634            if (std::isnan(a) && std::isnan(b)) return true;
13635            return std::abs(a - b) < tol;
13636        }
13637
13638        // =====================================================================
13639        // merge() Tests
13640        // =====================================================================
13641
13642        void pd_test_joining_merge_inner() {
13643            std::cout << "========= merge inner join ============================";
13644
13645            // Left DataFrame: id, value_left
13646            std::map<std::string, std::vector<double>> left_data = {
13647                {"id", {1.0, 2.0, 3.0, 4.0}},
13648                {"value_left", {10.0, 20.0, 30.0, 40.0}}
13649            };
copy (pd_test_1_all.cpp:5798)
5788// ============================================================================
5789// Copy/Rename Tests
5790// ============================================================================
5791
5792void pd_test_categorical_index_copy() {
5793    std::cout << "========= copy ========================================";
5794
5795    pandas::CategoricalArray arr({"a", "b", "c"});
5796    pandas::CategoricalIndex idx(arr, "original");
5797
5798    pandas::CategoricalIndex copied = idx.copy();
5799
5800    bool passed = (copied.size() == idx.size() && copied.name() == idx.name() &&
5801                   copied.categories() == idx.categories() && copied.ordered() == idx.ordered());
5802    if (!passed) {
5803        std::cout << "  [FAIL] : in pd_test_categorical_index_copy()" << std::endl;
5804        throw std::runtime_error("pd_test_categorical_index_copy failed");
5805    }
5806
5807    std::cout << " -> tests passed" << std::endl;
5808}
keys (pd_test_1_all.cpp:16319)
16309            }
16310
16311            // Test default value
16312            passed = attrs.get<int>("missing", 99) == 99;
16313            if (!passed) {
16314                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : default value" << std::endl;
16315                throw std::runtime_error("pd_test_ndframe_attrs failed: default value");
16316            }
16317
16318            // Test keys
16319            auto keys = attrs.keys();
16320            passed = keys.size() == 3;
16321            if (!passed) {
16322                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : keys()" << std::endl;
16323                throw std::runtime_error("pd_test_ndframe_attrs failed: keys()");
16324            }
16325
16326            // Test remove
16327            passed = attrs.remove("count") && !attrs.contains("count");
16328            if (!passed) {
16329                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl;
clear (pd_test_1_all.cpp:16341)
16331            }
16332
16333            // Test has_type
16334            passed = attrs.has_type<std::string>("author") && !attrs.has_type<int>("author");
16335            if (!passed) {
16336                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : has_type" << std::endl;
16337                throw std::runtime_error("pd_test_ndframe_attrs failed: has_type");
16338            }
16339
16340            // Test clear
16341            attrs.clear();
16342            passed = attrs.empty();
16343            if (!passed) {
16344                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : clear" << std::endl;
16345                throw std::runtime_error("pd_test_ndframe_attrs failed: clear");
16346            }
16347
16348            std::cout << " -> tests passed" << std::endl;
16349        }
16350
16351        // =====================================================================
contains (pd_test_1_all.cpp:2200)
2190// Test: contains method
2191// ============================================================================
2192void test_contains() {
2193    std::cout << "========= IntervalArray: contains ======================= ";
2194
2195    std::vector<numpy::float64> breaks = {0.0, 1.0, 2.0, 3.0};
2196
2197    // Right-closed intervals: (0, 1], (1, 2], (2, 3]
2198    auto arr_right = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right);
2199
2200    // Test contains(1.0) - should be in interval 0 but not 1 (since 1 is exclusive on left of interval 1)
2201    auto contains_1 = arr_right.contains(1.0);
2202    // (0, 1] contains 1: yes, (1, 2] contains 1: no (open on left), (2, 3] contains 1: no
2203    if (contains_1[0].value_or(false) != true ||
2204        contains_1[1].value_or(true) != false ||
2205        contains_1[2].value_or(true) != false) {
2206        std::cout << "[FAIL] : in test_contains() : right-closed contains 1.0" << std::endl;
2207        return;
2208    }
2209
2210    // Left-closed intervals: [0, 1), [1, 2), [2, 3)
empty (pd_test_1_all.cpp:941)
931#include "../pandas/pd_config.h"
932
933namespace dataframe_tests {
934
935namespace dataframe_tests_config {
936
937    void pd_test_config_version() {
938        std::cout << "========= df_config: version info ======================= ";
939        const char* version = pandas::DataFrameInfo::version();
940        if (version == nullptr || std::string(version).empty()) {
941            std::cout << "[FAIL] : in pd_test_config_version() : version is null or empty" << std::endl;
942            throw std::runtime_error("pd_test_config_version failed: version is null or empty");
943        }
944        std::cout << "-> tests passed" << std::endl;
945    }
946
947    void pd_test_config_na_repr() {
948        std::cout << "========= df_config: NA representation ======================= ";
949        const char* na_repr = pandas::DataFrameConfig::get_na_repr();
950        if (na_repr == nullptr) {
has_type (pd_test_1_all.cpp:16334)
16324            }
16325
16326            // Test remove
16327            passed = attrs.remove("count") && !attrs.contains("count");
16328            if (!passed) {
16329                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl;
16330                throw std::runtime_error("pd_test_ndframe_attrs failed: remove");
16331            }
16332
16333            // Test has_type
16334            passed = attrs.has_type<std::string>("author") && !attrs.has_type<int>("author");
16335            if (!passed) {
16336                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : has_type" << std::endl;
16337                throw std::runtime_error("pd_test_ndframe_attrs failed: has_type");
16338            }
16339
16340            // Test clear
16341            attrs.clear();
16342            passed = attrs.empty();
16343            if (!passed) {
16344                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : clear" << std::endl;
remove (pd_test_1_all.cpp:16327)
16317            // Test keys
16318            auto keys = attrs.keys();
16319            passed = keys.size() == 3;
16320            if (!passed) {
16321                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : keys()" << std::endl;
16322                throw std::runtime_error("pd_test_ndframe_attrs failed: keys()");
16323            }
16324
16325            // Test remove
16326            passed = attrs.remove("count") && !attrs.contains("count");
16327            if (!passed) {
16328                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl;
16329                throw std::runtime_error("pd_test_ndframe_attrs failed: remove");
16330            }
16331
16332            // Test has_type
16333            passed = attrs.has_type<std::string>("author") && !attrs.has_type<int>("author");
16334            if (!passed) {
16335                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : has_type" << std::endl;
16336                throw std::runtime_error("pd_test_ndframe_attrs failed: has_type");
set (pd_test_1_all.cpp:16281)
16271            pandas::Attrs attrs;
16272
16273            // Test empty
16274            bool passed = attrs.empty() && attrs.size() == 0;
16275            if (!passed) {
16276                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : empty attrs" << std::endl;
16277                throw std::runtime_error("pd_test_ndframe_attrs failed: empty attrs");
16278            }
16279
16280            // Test set/get string
16281            attrs.set("author", std::string("John Doe"));
16282            passed = attrs.contains("author") && attrs.get<std::string>("author") == "John Doe";
16283            if (!passed) {
16284                std::cout << "  [FAIL] : in pd_test_ndframe_attrs() : set/get string" << std::endl;
16285                throw std::runtime_error("pd_test_ndframe_attrs failed: set/get string");
16286            }
16287
16288            // Test set/get double
16289            attrs.set("version", 1.5);
16290            passed = std::abs(attrs.get<double>("version") - 1.5) < 1e-10;
16291            if (!passed) {
size (pd_test_1_all.cpp:22)
12#include "../pandas/pd_boolean_array.h"
13
14namespace dataframe_tests {
15
16namespace dataframe_tests_boolean_array {
17    void pd_test_boolean_array_constructors() {
18        std::cout << "========= BooleanArray: constructors ======================= ";
19
20        // Default constructor
21        pandas::BooleanArray arr1;
22        if (arr1.size() != 0) {
23            std::cout << "  [FAIL] : in pd_test_boolean_array_constructors() : default constructor size != 0" << std::endl;
24            throw std::runtime_error("pd_test_boolean_array_constructors failed: default constructor size != 0");
25        }
26
27        // Initializer list constructor
28        pandas::BooleanArray arr2({
29            std::optional<bool>(true),
30            std::optional<bool>(false),
31            std::nullopt,
32            std::optional<bool>(true)