NDFrame#

class numpy::NDFrame#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

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

Constructors#

Signature

Location

Example

NDFrame() = default

df_ndframe.h:102

NDFrame(const NDFrame& other)

df_ndframe.h:106

NDFrame(NDFrame&& other) noexcept

df_ndframe.h:111

Indexing / Selection#

Signature

Return Type

Location

Example

const Attrs& attrs() const override

const Attrs&

df_ndframe.h:151

Attrs& attrs() override

Attrs&

df_ndframe.h:156

void items(Func&& func) const

void

df_ndframe.h:317

Shape Manipulation#

Signature

Return Type

Location

Example

Derived T\_() const

Derived

df_ndframe.h:345

Derived transpose() const

Derived

df_ndframe.h:353

View

Logical#

Signature

Return Type

Location

Example

bool all(bool skipna = true) const override

bool

df_ndframe.h:182

View

bool any(bool skipna = true) const override

bool

df_ndframe.h:211

View

Set Operations#

Signature

Return Type

Location

Example

numpy::NDArray<numpy::bool\_> isin(const std::vector<T>& values) const

numpy::NDArray<numpy::bool_>

df_ndframe.h:287

View

I/O#

Signature

Return Type

Location

Example

numpy::NDArray<T> to_numpy() const

numpy::NDArray<T>

df_ndframe.h:365

View

Type Handling#

Signature

Return Type

Location

Example

void copy_metadata_from(const NDFrame& other)

void

df_ndframe.h:378

Other Methods#

Signature

Return Type

Location

Example

std::string dtype_name() const override

std::string

df_ndframe.h:140

View

const Flags& flags() const override

const Flags&

df_ndframe.h:166

View

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

df_ndframe.h:321

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

std::vector<std::string>

df_ndframe.h:329

View

\* Note: This renames the index name (e.g., index.name = "my_index"),

* Note: This renames the index

df_ndframe.h:258

View

void propagate_attrs(Derived& result) const

void

df_ndframe.h:387

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

Derived

df_ndframe.h:261

\* Series<int> s(

* Series<int>

df_ndframe.h:283

Derived& self()

Derived&

df_ndframe.h:90

View

const Derived& self() const

const Derived&

df_ndframe.h:95

View

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

df_ndframe.h:246

View

void set_attrs(const Attrs& attrs) override

void

df_ndframe.h:161

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

Derived&

df_ndframe.h:244

void set_flags(const Flags& flags) override

void

df_ndframe.h:171

\* For 1D structures (Series), returns a copy since transpose is a no-op.

* For 1D

df_ndframe.h:340

\* For 2D structures (DataFrame), swaps rows and columns.

* For 2D

df_ndframe.h:341

Code Examples#

The following examples are extracted from the test suite.

transpose (np_test_2_all.cpp:4973)
4963    }
4964
4965    /**
4966     * Test matrix properties and methods
4967     */
4968    void testMatrixProperties() {
4969      std::cout << "========= testMatrixProperties =======================";
4970
4971      // Test transpose
4972      numpy::Matrix<double> m("1 2 3; 4 5 6");
4973      auto mt = m.transpose();
4974      assert_test(mt.rows() == 3, "Transpose rows");
4975      assert_test(mt.cols() == 2, "Transpose cols");
4976      assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element");
4977      assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element");
4978
4979      // Test trace for square matrix
4980      numpy::Matrix<double> square("1 2; 3 4");
4981      double tr = square.trace();
4982      assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace");
all (np_test_4_all.cpp:23928)
23918                        }
23919                    }
23920                }
23921
23922                auto result = numpy::einsum<numpy::float64>("ijk->ik", {A});
23923
23924                if (result.getShape()[0] != 2 || result.getShape()[1] != 2) {
23925                    throw std::runtime_error("einsum partial sum shape wrong");
23926                }
23927
23928                // Sum over j: 1+2+3 = 6 for all (i,k) positions
23929                if (std::abs(result.getElementAt({0, 0}) - 6.0) > 1e-10 ||
23930                    std::abs(result.getElementAt({1, 1}) - 6.0) > 1e-10) {
23931                    throw std::runtime_error("einsum partial sum values wrong");
23932                }
23933
23934                // std::cout << "  OK: Partial sum: 'ijk->ik'\n";
23935            }
23936
23937            // Test 5: Size-1 dimension handling
23938            {
any (np_test_2_all.cpp:16758)
16748    // ANY() TESTS - SCALAR RESULT
16749    // ============================================================================
16750
16751    void np_test_logic_any_scalar_all_false() {
16752      std::cout << "========= any: all false elements =======================";
16753
16754      // Create array with all false/zero elements
16755      std::vector<double> data = { 0.0, 0.0, 0.0 };
16756      numpy::NDArray<double> arr = numpy::createArrayFromVector<double>({ 3 }, data);
16757
16758      bool result = numpy::any(arr);
16759
16760      if (result != false) {
16761        std::cout << "  [FAIL] : in np_test_logic_any_scalar_all_false() : expected false for all-zero array";
16762        throw std::runtime_error("np_test_logic_any_scalar_all_false failed: expected false");
16763      }
16764
16765      std::cout << " -> tests passed" << std::endl;
16766    }
16767
16768    void np_test_logic_any_scalar_all_true() {
isin (np_test_1_all.cpp:19434)
19424        std::cout << std::string("[FAIL] ") + description << std::endl;
19425        throw std::runtime_error(description);
19426    }
19427    if (!(membership.getElementAt({ 5 }) == false)) {
19428        std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(membership.getElementAt({ 5 }) == false)";
19429        std::cout << std::string("[FAIL] ") + description << std::endl;
19430        throw std::runtime_error(description);
19431    }
19432    // std::cout << "[OK] Membership testing (in1d) works correctly\n";
19433
19434    // Test isin (alias for in1d)
19435    auto isin_result = isin(test_array, test_values);
19436
19437    // Should be identical to in1d result
19438    for (size_t i = 0; i < membership.getSize(); ++i) {
19439      if (!(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))) {
19440          std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))";
19441          std::cout << std::string("[FAIL] ") + description << std::endl;
19442          throw std::runtime_error(description);
19443      }
19444    }
to_numpy (np_test_5_all.cpp:21373)
21363    if (errors == 0) {
21364        std::cout << "np_test_timedelta_components -> tests passed" << std::endl;
21365    }
21366    return errors;
21367}
21368
21369// =============================================================================
21370// Test 4: Total Conversion Properties
21371// =============================================================================
21372int np_test_timedelta_total_conversions() {
21373    int errors = 0;
21374
21375    numpy::Timedelta td(1, 12, 0);  // 1 day 12 hours = 1.5 days = 36 hours
21376
21377    // total_seconds
21378    {
21379        double secs = td.total_seconds();
21380        double expected = (24 + 12) * 3600.0;
21381        if (std::abs(secs - expected) > 0.0001) {
21382            std::cout << "[FAIL] np_test_timedelta_total_conversions: total_seconds expected "
21383                      << expected << ", got " << secs << std::endl;
dtype_name (np_test_2_all.cpp:3567)
3557          std::cout << std::string("[FAIL] ") + description << std::endl;
3558          throw std::runtime_error(description);
3559      }
3560      if (!(flags_info.writeable == true)) {
3561          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.writeable == true)";
3562          std::cout << std::string("[FAIL] ") + description << std::endl;
3563          throw std::runtime_error(description);
3564      }
3565
3566      // Test dtype functions
3567      if (!(dtype_name(arr) == "float64")) {
3568          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(dtype_name(arr) == \"float64\")";
3569          std::cout << std::string("[FAIL] ") + description << std::endl;
3570          throw std::runtime_error(description);
3571      }
3572      if (!(dtype_itemsize(arr) == sizeof(double))) {
3573          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(dtype_itemsize(arr) == sizeof(double))";
3574          std::cout << std::string("[FAIL] ") + description << std::endl;
3575          throw std::runtime_error(description);
3576      }
flags (np_test_2_all.cpp:3554)
3544      }
3545
3546      // Test nbytes
3547      if (!(nbytes(arr) == 24 * sizeof(double))) {
3548          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))";
3549          std::cout << std::string("[FAIL] ") + description << std::endl;
3550          throw std::runtime_error(description);
3551      }
3552
3553      // Test flags
3554      auto flags_info = flags(arr);
3555      if (!(flags_info.owndata == true)) {
3556          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.owndata == true)";
3557          std::cout << std::string("[FAIL] ") + description << std::endl;
3558          throw std::runtime_error(description);
3559      }
3560      if (!(flags_info.writeable == true)) {
3561          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.writeable == true)";
3562          std::cout << std::string("[FAIL] ") + description << std::endl;
3563          throw std::runtime_error(description);
3564      }
keys (np_test_2_all.cpp:8614)
8604          if (!indices.empty()) {
8605            std::cout << "   IPP empty array test:                                                                   -> [FAIL]";
8606            throw std::runtime_error("IPP empty array test: FAILED - should return empty");
8607          }
8608          // std::cout << "[OK] IPP empty array test: PASSED" << std::endl;
8609        }
8610
8611        // Test 6: Large array performance
8612        {
8613          const size_t n = 10000;
8614          std::vector<std::vector<int>> keys(2);
8615          keys[0].resize(n);
8616          keys[1].resize(n);
8617
8618          std::mt19937 gen(42);
8619          std::uniform_int_distribution<int> dist(0, 1000);
8620
8621          for (size_t i = 0; i < n; ++i) {
8622            keys[0][i] = dist(gen);
8623            keys[1][i] = dist(gen);
8624          }
name (np_test_1_all.cpp:24865)
24855      uint64_t before_restore = rng.next_uint64();
24856      rng.set_state(state);
24857      uint64_t after_restore = rng.next_uint64();
24858
24859      if (before_restore != after_restore) {
24860        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : state restore failed";
24861        throw std::runtime_error("np_test_bitgen_mt19937 failed: state");
24862      }
24863
24864      // Test name
24865      if (rng.name() != "MT19937") {
24866        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : incorrect name";
24867        throw std::runtime_error("np_test_bitgen_mt19937 failed: name");
24868      }
24869
24870      std::cout << " -> tests passed" << std::endl;
24871    }
24872
24873    // ============================================================================
24874    // PCG64 TESTS
24875    // ============================================================================
self (np_test_1_all.cpp:2535)
2525        throw std::runtime_error(description);
2526    }
2527    if (!(result.getElementAt({2}) == 0xFFFFFFFF)) {
2528        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({2}) == 0xFFFFFFFF)";
2529        std::cout << std::string("[FAIL] ") + description << std::endl;
2530        throw std::runtime_error(description);
2531    }
2532
2533    // std::cout << "[OK] Bitwise XOR creates expected patterns\n";
2534
2535    // Test XOR with self (should be zero)
2536    auto self_xor = bitwise_xor(a, a);
2537    if (!(self_xor.getElementAt({0}) == 0)) {
2538        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(self_xor.getElementAt({0}) == 0)";
2539        std::cout << std::string("[FAIL] ") + description << std::endl;
2540        throw std::runtime_error(description);
2541    }
2542    if (!(self_xor.getElementAt({1}) == 0)) {
2543        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(self_xor.getElementAt({1}) == 0)";
2544        std::cout << std::string("[FAIL] ") + description << std::endl;
2545        throw std::runtime_error(description);
self (np_test_1_all.cpp:2535)
2525        throw std::runtime_error(description);
2526    }
2527    if (!(result.getElementAt({2}) == 0xFFFFFFFF)) {
2528        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({2}) == 0xFFFFFFFF)";
2529        std::cout << std::string("[FAIL] ") + description << std::endl;
2530        throw std::runtime_error(description);
2531    }
2532
2533    // std::cout << "[OK] Bitwise XOR creates expected patterns\n";
2534
2535    // Test XOR with self (should be zero)
2536    auto self_xor = bitwise_xor(a, a);
2537    if (!(self_xor.getElementAt({0}) == 0)) {
2538        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(self_xor.getElementAt({0}) == 0)";
2539        std::cout << std::string("[FAIL] ") + description << std::endl;
2540        throw std::runtime_error(description);
2541    }
2542    if (!(self_xor.getElementAt({1}) == 0)) {
2543        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(self_xor.getElementAt({1}) == 0)";
2544        std::cout << std::string("[FAIL] ") + description << std::endl;
2545        throw std::runtime_error(description);
self (np_test_1_all.cpp:2535)
2525        throw std::runtime_error(description);
2526    }
2527    if (!(result.getElementAt({2}) == 0xFFFFFFFF)) {
2528        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({2}) == 0xFFFFFFFF)";
2529        std::cout << std::string("[FAIL] ") + description << std::endl;
2530        throw std::runtime_error(description);
2531    }
2532
2533    // std::cout << "[OK] Bitwise XOR creates expected patterns\n";
2534
2535    // Test XOR with self (should be zero)
2536    auto self_xor = bitwise_xor(a, a);
2537    if (!(self_xor.getElementAt({0}) == 0)) {
2538        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(self_xor.getElementAt({0}) == 0)";
2539        std::cout << std::string("[FAIL] ") + description << std::endl;
2540        throw std::runtime_error(description);
2541    }
2542    if (!(self_xor.getElementAt({1}) == 0)) {
2543        std::string description = std::string("testBitwiseXor():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(self_xor.getElementAt({1}) == 0)";
2544        std::cout << std::string("[FAIL] ") + description << std::endl;
2545        throw std::runtime_error(description);