NDFrame#

class pandas::NDFrame#

Core data container class in the pandas namespace.

Example#

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

// Use NDFrame
NDFrame obj;
// ... operations ...

Constructors#

Signature

Location

Example

NDFrame() = default

pd_ndframe.h:102

NDFrame(const NDFrame& other)

pd_ndframe.h:106

NDFrame(NDFrame&& other) noexcept

pd_ndframe.h:111

Indexing / Selection#

Signature

Return Type

Location

Example

const Attrs& attrs() const override

const Attrs&

pd_ndframe.h:167

View

Attrs& attrs() override

Attrs&

pd_ndframe.h:172

View

Data Manipulation#

Signature

Return Type

Location

Example

Derived rename_axis(const std::optional<std::string>& mapper, int axis = 0) const

Derived

pd_ndframe.h:297

View

Derived& set_axis(std::unique_ptr<IndexBase> labels, int axis = 0)

Derived&

pd_ndframe.h:280

View

Reshaping#

Signature

Return Type

Location

Example

Derived T_() const

Derived

pd_ndframe.h:381

View

Derived transpose() const

Derived

pd_ndframe.h:389

View

I/O#

Signature

Return Type

Location

Example

numpy::NDArray<T> to_numpy() const

numpy::NDArray<T>

pd_ndframe.h:401

View

Conversion#

Signature

Return Type

Location

Example

void copy_metadata_from(const NDFrame& other)

void

pd_ndframe.h:414

Iteration#

Signature

Return Type

Location

Example

void items(Func&& func) const

void

pd_ndframe.h:353

View

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

std::vector<std::string>

pd_ndframe.h:365

View

Set Operations#

Signature

Return Type

Location

Example

numpy::NDArray<numpy::bool_> isin(const std::vector<T>& values) const

numpy::NDArray<numpy::bool_>

pd_ndframe.h:323

View

Other Methods#

Signature

Return Type

Location

Example

bool all(int axis = 0, bool bool_only = false, bool skipna = true) const override

bool

pd_ndframe.h:212

View

bool any(int axis = 0, bool bool_only = false, bool skipna = true) const override

bool

pd_ndframe.h:245

View

std::string dtype_name() const override

std::string

pd_ndframe.h:140

View

const Flags& flags() const override

const Flags&

pd_ndframe.h:182

View

Flags& flags_mutable()

Flags&

pd_ndframe.h:187

func(idx.get_value_str(i), vals.getElementAt({i}))

pd_ndframe.h:357

View

void propagate_attrs(Derived& result) const

void

pd_ndframe.h:423

Derived& self() { return static_cast<Derived&>(\*this)

Derived&

pd_ndframe.h:90

View

const Derived& self() const { return static_cast<const Derived&>(\*this)

const Derived&

pd_ndframe.h:95

View

self().set_index(std::move(labels))

pd_ndframe.h:282

View

void set_attrs(const Attrs& attrs) override

void

pd_ndframe.h:177

void set_flags(const Flags& flags, bool copy = true, bool allows_duplicate_labels = true)

void

pd_ndframe.h:195

View

Code Examples#

The following examples are extracted from the test suite.

attrs (pd_test_1_all.cpp:16361)
16351        // =====================================================================
16352        // Series Attrs Integration Tests
16353        // =====================================================================
16354
16355        void pd_test_ndframe_series_attrs() {
16356            std::cout << "========= series attrs integration =============================" << std::endl;
16357
16358            pandas::Series<double> s({1.0, 2.0, 3.0});
16359
16360            // Test setting attrs on Series
16361            s.attrs().set("source", std::string("test_data"));
16362            s.attrs().set("timestamp", 1234567890);
16363
16364            bool passed = s.attrs().get<std::string>("source") == "test_data";
16365            if (!passed) {
16366                std::cout << "  [FAIL] : in pd_test_ndframe_series_attrs() : set/get source" << std::endl;
16367                throw std::runtime_error("pd_test_ndframe_series_attrs failed: set/get source");
16368            }
16369
16370            passed = s.attrs().get<int>("timestamp") == 1234567890;
16371            if (!passed) {
attrs (pd_test_1_all.cpp:16361)
16351        // =====================================================================
16352        // Series Attrs Integration Tests
16353        // =====================================================================
16354
16355        void pd_test_ndframe_series_attrs() {
16356            std::cout << "========= series attrs integration =============================" << std::endl;
16357
16358            pandas::Series<double> s({1.0, 2.0, 3.0});
16359
16360            // Test setting attrs on Series
16361            s.attrs().set("source", std::string("test_data"));
16362            s.attrs().set("timestamp", 1234567890);
16363
16364            bool passed = s.attrs().get<std::string>("source") == "test_data";
16365            if (!passed) {
16366                std::cout << "  [FAIL] : in pd_test_ndframe_series_attrs() : set/get source" << std::endl;
16367                throw std::runtime_error("pd_test_ndframe_series_attrs failed: set/get source");
16368            }
16369
16370            passed = s.attrs().get<int>("timestamp") == 1234567890;
16371            if (!passed) {
rename_axis (pd_test_1_all.cpp:6760)
6750                    throw std::runtime_error("pd_test_dataframe_index_ops failed: get_optional Z");
6751                }
6752            }
6753
6754            // Test rename_axis
6755            {
6756                std::map<std::string, std::vector<int>> data;
6757                data["A"] = {1, 2, 3};
6758                pandas::DataFrame df(data);
6759
6760                auto renamed = df.rename_axis("my_index", 0);
6761                // Should not throw
6762            }
6763
6764            // Test reindex_like
6765            {
6766                std::map<std::string, std::vector<int>> data1;
6767                data1["A"] = {1, 2};
6768                data1["B"] = {3, 4};
6769                pandas::DataFrame df1(data1);
set_axis (pd_test_1_all.cpp:6673)
6663            std::cout << " -> tests passed" << std::endl;
6664        }
6665
6666        // =====================================================================
6667        // Test: Index Operations
6668        // =====================================================================
6669        void pd_test_dataframe_index_ops() {
6670            std::cout << "========= index operations =================";
6671
6672            // Test set_axis (rows)
6673            {
6674                std::map<std::string, std::vector<int>> data;
6675                data["A"] = {1, 2, 3};
6676                pandas::DataFrame df(data);
6677
6678                auto renamed = df.set_axis({"x", "y", "z"}, 0);
6679                std::string idx0 = renamed.index().get_value_str(0);
6680                if (idx0 != "x") {
6681                    std::cout << "  [FAIL] : in pd_test_dataframe_index_ops() : set_axis first label should be 'x'" << std::endl;
6682                    throw std::runtime_error("pd_test_dataframe_index_ops failed: set_axis");
T_ (pd_test_1_all.cpp:16634)
16624        // =====================================================================
16625        // Transpose Tests
16626        // =====================================================================
16627
16628        void pd_test_ndframe_transpose() {
16629            std::cout << "========= transpose ============================================" << std::endl;
16630
16631            pandas::Series<int> s({1, 2, 3});
16632
16633            // For Series, T_() returns a copy
16634            auto transposed = s.T_();
16635            bool passed = transposed.size() == s.size();
16636            if (!passed) {
16637                std::cout << "  [FAIL] : in pd_test_ndframe_transpose() : T_() size" << std::endl;
16638                throw std::runtime_error("pd_test_ndframe_transpose failed: T_() size");
16639            }
16640
16641            passed = transposed[0] == 1 && transposed[1] == 2 && transposed[2] == 3;
16642            if (!passed) {
16643                std::cout << "  [FAIL] : in pd_test_ndframe_transpose() : T_() values" << std::endl;
transpose (pd_test_1_all.cpp:16648)
16638                std::cout << "  [FAIL] : in pd_test_ndframe_transpose() : T_() size" << std::endl;
16639                throw std::runtime_error("pd_test_ndframe_transpose failed: T_() size");
16640            }
16641
16642            passed = transposed[0] == 1 && transposed[1] == 2 && transposed[2] == 3;
16643            if (!passed) {
16644                std::cout << "  [FAIL] : in pd_test_ndframe_transpose() : T_() values" << std::endl;
16645                throw std::runtime_error("pd_test_ndframe_transpose failed: T_() values");
16646            }
16647
16648            // Test transpose() alias
16649            auto transposed2 = s.transpose();
16650            passed = transposed2.size() == s.size();
16651            if (!passed) {
16652                std::cout << "  [FAIL] : in pd_test_ndframe_transpose() : transpose() size" << std::endl;
16653                throw std::runtime_error("pd_test_ndframe_transpose failed: transpose() size");
16654            }
16655
16656            std::cout << " -> tests passed" << std::endl;
16657        }
to_numpy (pd_test_1_all.cpp:16764)
16754        // =====================================================================
16755        // to_numpy Tests
16756        // =====================================================================
16757
16758        void pd_test_ndframe_to_numpy() {
16759            std::cout << "========= to_numpy =============================================" << std::endl;
16760
16761            pandas::Series<int> s({10, 20, 30});
16762
16763            auto arr = s.to_numpy();
16764
16765            bool passed = arr.getSize() == 3;
16766            if (!passed) {
16767                std::cout << "  [FAIL] : in pd_test_ndframe_to_numpy() : size" << std::endl;
16768                throw std::runtime_error("pd_test_ndframe_to_numpy failed: size");
16769            }
16770
16771            passed = arr.getElementAt({0}) == 10 && arr.getElementAt({1}) == 20 && arr.getElementAt({2}) == 30;
16772            if (!passed) {
16773                std::cout << "  [FAIL] : in pd_test_ndframe_to_numpy() : values" << std::endl;
items (pd_test_1_all.cpp:16554)
16544        // =====================================================================
16545        // Iteration Tests (items, keys)
16546        // =====================================================================
16547
16548        void pd_test_ndframe_items_keys() {
16549            std::cout << "========= items/keys ===========================================" << std::endl;
16550
16551            pandas::Series<int> s({10, 20, 30});
16552
16553            // Test items()
16554            std::vector<std::string> collected_keys;
16555            std::vector<int> collected_values;
16556
16557            s.items([&](const std::string& key, int value) {
16558                collected_keys.push_back(key);
16559                collected_values.push_back(value);
16560            });
16561
16562            bool passed = collected_keys.size() == 3;
16563            if (!passed) {
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;
isin (pd_test_1_all.cpp:5938)
5928    std::cout << " -> tests passed" << std::endl;
5929}
5930
5931void pd_test_categorical_index_isin() {
5932    std::cout << "========= inherited isin ==============================";
5933
5934    pandas::CategoricalArray arr({"a", "b", "c", "d"});
5935    pandas::CategoricalIndex idx(arr);
5936
5937    std::vector<std::string> values = {"a", "c"};
5938    numpy::NDArray<numpy::bool_> mask = idx.isin(values);
5939
5940    bool passed = (mask.getSize() == 4 &&
5941                   mask.getElementAt({0}) == true &&   // a
5942                   mask.getElementAt({1}) == false &&  // b
5943                   mask.getElementAt({2}) == true &&   // c
5944                   mask.getElementAt({3}) == false);   // d
5945    if (!passed) {
5946        std::cout << "  [FAIL] : in pd_test_categorical_index_isin()" << std::endl;
5947        throw std::runtime_error("pd_test_categorical_index_isin failed");
5948    }
all (pd_test_1_all.cpp:247)
237        pandas::BooleanArray has_true({
238            std::optional<bool>(false),
239            std::optional<bool>(true)
240        });
241        any_result = has_true.any();
242        if (!any_result.has_value() || !any_result.value()) {
243            std::cout << "  [FAIL] : in pd_test_boolean_array_reductions() : any() with True" << std::endl;
244            throw std::runtime_error("pd_test_boolean_array_reductions failed: any() with True");
245        }
246
247        // Test all()
248        pandas::BooleanArray all_true({
249            std::optional<bool>(true),
250            std::optional<bool>(true)
251        });
252        auto all_result = all_true.all();
253        if (!all_result.has_value() || !all_result.value()) {
254            std::cout << "  [FAIL] : in pd_test_boolean_array_reductions() : all() of all True" << std::endl;
255            throw std::runtime_error("pd_test_boolean_array_reductions failed: all() all True");
256        }
any (pd_test_1_all.cpp:226)
216            std::cout << "  [FAIL] : in pd_test_boolean_array_kleene_not() : ~NA should be NA" << std::endl;
217            throw std::runtime_error("pd_test_boolean_array_kleene_not failed: ~NA");
218        }
219
220        std::cout << " -> tests passed" << std::endl;
221    }
222
223    void pd_test_boolean_array_reductions() {
224        std::cout << "========= BooleanArray: reductions ======================= ";
225
226        // Test any()
227        pandas::BooleanArray all_false({
228            std::optional<bool>(false),
229            std::optional<bool>(false)
230        });
231        auto any_result = all_false.any();
232        if (!any_result.has_value() || any_result.value()) {
233            std::cout << "  [FAIL] : in pd_test_boolean_array_reductions() : any() of all False" << std::endl;
234            throw std::runtime_error("pd_test_boolean_array_reductions failed: any() all False");
235        }
dtype_name (pd_test_1_all.cpp:10104)
10094}
10095
10096void pd_test_extension_index_array_constructor() {
10097    std::cout << "========= array constructor =========================";
10098
10099    pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"});
10100    pandas::CategoricalIndex idx(arr, "fruits");
10101
10102    bool passed = (idx.size() == 4 && !idx.empty() &&
10103                   idx.name().has_value() && *idx.name() == "fruits" &&
10104                   idx.dtype_name() == "category");
10105    if (!passed) {
10106        std::cout << "  [FAIL] : in pd_test_extension_index_array_constructor() : array constructor check failed" << std::endl;
10107        throw std::runtime_error("pd_test_extension_index_array_constructor failed");
10108    }
10109
10110    std::cout << " -> tests passed" << std::endl;
10111}
10112
10113void pd_test_extension_index_copy_constructor() {
10114    std::cout << "========= copy constructor =========================";
flags (pd_test_1_all.cpp:16397)
16387        // =====================================================================
16388        // Series Flags Integration Tests
16389        // =====================================================================
16390
16391        void pd_test_ndframe_series_flags() {
16392            std::cout << "========= series flags integration =============================" << std::endl;
16393
16394            pandas::Series<int> s({1, 2, 3});
16395
16396            // Test default flags
16397            bool passed = s.flags().allows_duplicate_labels == true;
16398            if (!passed) {
16399                std::cout << "  [FAIL] : in pd_test_ndframe_series_flags() : default allows_duplicate_labels" << std::endl;
16400                throw std::runtime_error("pd_test_ndframe_series_flags failed: default allows_duplicate_labels");
16401            }
16402
16403            passed = s.flags().copy_on_write == false;
16404            if (!passed) {
16405                std::cout << "  [FAIL] : in pd_test_ndframe_series_flags() : default copy_on_write" << std::endl;
16406                throw std::runtime_error("pd_test_ndframe_series_flags failed: default copy_on_write");
16407            }
func (pd_test_3_all.cpp:13837)
13827// ============================================================================
13828// Read Stubs Tests (verify they throw correctly)
13829// ============================================================================
13830
13831void pd_test_top_level_read_stubs() {
13832    std::cout << "========= read_* stubs ================================";
13833
13834    // Test that read functions throw as expected (they are stubs)
13835    auto test_throws = [](const std::string& name, auto func) {
13836        try {
13837            func();
13838            std::cout << "  [FAIL] : " << name << " should throw" << std::endl;
13839            return false;
13840        } catch (const std::exception&) {
13841            return true;
13842        }
13843    };
13844
13845    bool all_passed = true;
13846    all_passed &= test_throws("read_clipboard", []() { pandas::read_clipboard(); });
13847    all_passed &= test_throws("read_excel", []() { pandas::read_excel("test.xlsx"); });
self (pd_test_3_all.cpp:1747)
1737void pd_test_3_all_dataframe_unstack() {
1738    std::cout << "========= DataFrame.unstack() ========================";
1739
1740    std::map<std::string, std::vector<double>> data = {
1741        {"A", {1.0, 2.0, 3.0}},
1742        {"B", {4.0, 5.0, 6.0}}
1743    };
1744    pandas::DataFrame df(data);
1745
1746    // Without MultiIndex, unstack() returns self (matches pandas behavior)
1747    pandas::DataFrame result = df.unstack();
1748
1749    if (result.nrows() != 3 || result.ncols() != 2) {
1750        std::cout << "  [FAIL] : in pd_test_3_all_dataframe_unstack() : shape mismatch" << std::endl;
1751        throw std::runtime_error("pd_test_3_all_dataframe_unstack failed: shape");
1752    }
1753
1754    // Single column without MultiIndex also returns self
1755    std::map<std::string, std::vector<double>> single_col = {
1756        {"A", {1.0, 2.0, 3.0}}
self (pd_test_3_all.cpp:1747)
1737void pd_test_3_all_dataframe_unstack() {
1738    std::cout << "========= DataFrame.unstack() ========================";
1739
1740    std::map<std::string, std::vector<double>> data = {
1741        {"A", {1.0, 2.0, 3.0}},
1742        {"B", {4.0, 5.0, 6.0}}
1743    };
1744    pandas::DataFrame df(data);
1745
1746    // Without MultiIndex, unstack() returns self (matches pandas behavior)
1747    pandas::DataFrame result = df.unstack();
1748
1749    if (result.nrows() != 3 || result.ncols() != 2) {
1750        std::cout << "  [FAIL] : in pd_test_3_all_dataframe_unstack() : shape mismatch" << std::endl;
1751        throw std::runtime_error("pd_test_3_all_dataframe_unstack failed: shape");
1752    }
1753
1754    // Single column without MultiIndex also returns self
1755    std::map<std::string, std::vector<double>> single_col = {
1756        {"A", {1.0, 2.0, 3.0}}
self (pd_test_3_all.cpp:1747)
1737void pd_test_3_all_dataframe_unstack() {
1738    std::cout << "========= DataFrame.unstack() ========================";
1739
1740    std::map<std::string, std::vector<double>> data = {
1741        {"A", {1.0, 2.0, 3.0}},
1742        {"B", {4.0, 5.0, 6.0}}
1743    };
1744    pandas::DataFrame df(data);
1745
1746    // Without MultiIndex, unstack() returns self (matches pandas behavior)
1747    pandas::DataFrame result = df.unstack();
1748
1749    if (result.nrows() != 3 || result.ncols() != 2) {
1750        std::cout << "  [FAIL] : in pd_test_3_all_dataframe_unstack() : shape mismatch" << std::endl;
1751        throw std::runtime_error("pd_test_3_all_dataframe_unstack failed: shape");
1752    }
1753
1754    // Single column without MultiIndex also returns self
1755    std::map<std::string, std::vector<double>> single_col = {
1756        {"A", {1.0, 2.0, 3.0}}
set_flags (pd_test_1_all.cpp:16410)
16400                throw std::runtime_error("pd_test_ndframe_series_flags failed: default allows_duplicate_labels");
16401            }
16402
16403            passed = s.flags().copy_on_write == false;
16404            if (!passed) {
16405                std::cout << "  [FAIL] : in pd_test_ndframe_series_flags() : default copy_on_write" << std::endl;
16406                throw std::runtime_error("pd_test_ndframe_series_flags failed: default copy_on_write");
16407            }
16408
16409            // Test set_flags
16410            s.set_flags(pandas::Flags(false, true));
16411            passed = s.flags().allows_duplicate_labels == false;
16412            if (!passed) {
16413                std::cout << "  [FAIL] : in pd_test_ndframe_series_flags() : set allows_duplicate_labels" << std::endl;
16414                throw std::runtime_error("pd_test_ndframe_series_flags failed: set allows_duplicate_labels");
16415            }
16416
16417            passed = s.flags().copy_on_write == true;
16418            if (!passed) {
16419                std::cout << "  [FAIL] : in pd_test_ndframe_series_flags() : set copy_on_write" << std::endl;
16420                throw std::runtime_error("pd_test_ndframe_series_flags failed: set copy_on_write");