NDFrameBase#

class numpy::NDFrameBase#

numpy C++ class.

Example#

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

// Use NDFrameBase
NDFrameBase obj;
// ... operations ...

Array Creation#

Signature

Return Type

Location

Example

virtual bool empty() const = 0

virtual bool

df_ndframe_base.h:73

View

Indexing / Selection#

Signature

Return Type

Location

Example

virtual const Attrs& attrs() const = 0

virtual const Attrs&

df_ndframe_base.h:107

virtual Attrs& attrs() = 0

virtual Attrs&

df_ndframe_base.h:113

virtual bool get_value_bool(size_t idx) const = 0

virtual bool

df_ndframe_base.h:236

virtual std::string get_value_str(size_t idx) const = 0

virtual std::string

df_ndframe_base.h:187

Logical#

Signature

Return Type

Location

Example

virtual bool all(bool skipna = true) const = 0

virtual bool

df_ndframe_base.h:270

View

virtual bool any(bool skipna = true) const = 0

virtual bool

df_ndframe_base.h:277

View

I/O#

Signature

Return Type

Location

Example

virtual std::string to_string() const = 0

virtual std::string

df_ndframe_base.h:297

View

virtual std::vector<std::string> to_string_vector() const = 0

virtual std::vector<std::string>

df_ndframe_base.h:193

Type Handling#

Signature

Return Type

Location

Example

virtual void copy_value_from(size_t src_idx, size_t dst_idx) = 0

virtual void

df_ndframe_base.h:259

Type Checking#

Signature

Return Type

Location

Example

virtual bool is_na_at(size_t idx) const = 0

virtual bool

df_ndframe_base.h:216

Other Methods#

Signature

Return Type

Location

Example

virtual std::vector<const IndexBase\*> axes() const = 0

virtual std::vector<const IndexBase*>

df_ndframe_base.h:176

View

size_t cache_memory_usage() const override

size_t

df_ndframe_base.h:322

void clear_cache() const override = 0

void

df_ndframe_base.h:312

virtual std::unique_ptr<NDFrameBase> clone() const = 0

virtual std::unique_ptr<NDFrameBase>

df_ndframe_base.h:287

View

virtual size_t count() const = 0

virtual size_t

df_ndframe_base.h:209

View

virtual std::string dtype_name() const = 0

virtual std::string

df_ndframe_base.h:79

View

virtual void fillna_double(double value) = 0

virtual void

df_ndframe_base.h:222

virtual const Flags& flags() const = 0

virtual const Flags&

df_ndframe_base.h:125

View

bool has_cached_values() const override = 0

bool

df_ndframe_base.h:317

virtual bool hasnans() const = 0

virtual bool

df_ndframe_base.h:203

virtual const IndexBase& index() const = 0

virtual const IndexBase&

df_ndframe_base.h:157

View

virtual std::optional<std::string> name() const

virtual std::optional<std::string>

df_ndframe_base.h:141

View

virtual size_t nbytes() const = 0

virtual size_t

df_ndframe_base.h:97

View

virtual size_t ndim() const = 0

virtual size_t

df_ndframe_base.h:91

View

virtual void replace_value(double to_replace, double value) = 0

virtual void

df_ndframe_base.h:229

virtual std::string repr() const = 0

virtual std::string

df_ndframe_base.h:303

virtual void reset_index(bool drop = true) = 0

virtual void

df_ndframe_base.h:170

virtual void set_attrs(const Attrs& attrs) = 0

virtual void

df_ndframe_base.h:119

virtual void set_flags(const Flags& flags) = 0

virtual void

df_ndframe_base.h:131

virtual void set_index(std::unique_ptr<IndexBase> new_index) = 0

virtual void

df_ndframe_base.h:164

virtual void set_name(const std::optional<std::string>& /\*name\*/)

virtual void

df_ndframe_base.h:147

virtual void set_value_double(size_t idx, double value) = 0

virtual void

df_ndframe_base.h:249

virtual void set_value_nan(size_t idx) = 0

virtual void

df_ndframe_base.h:242

virtual std::vector<size_t> shape() const = 0

virtual std::vector<size_t>

df_ndframe_base.h:85

View

virtual size_t size() const = 0

virtual size_t

df_ndframe_base.h:67

View

Code Examples#

The following examples are extracted from the test suite.

empty (np_test_1_all.cpp:6316)
6306}
6307
6308void test_data_generator_emptyBenchmarkSorting() {
6309    std::cout << "========= test_data_generator_empty =======================";
6310
6311    DataGenerator<int> gen(42);
6312
6313    // Test empty data generation
6314    std::vector<int> data = gen.generate(0, DataPattern::RANDOM);
6315
6316    if (!(data.empty())) {
6317        std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())";
6318        std::cout << std::string("[FAIL] ") + description << std::endl;
6319        throw std::runtime_error(description);
6320    }
6321
6322    // std::cout << "Empty data generation test passed" << std::endl;
6323
6324    std::cout << " -> tests passed" << std::endl;
6325}
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() {
to_string (np_test_1_all.cpp:454)
444    // Modify through different views
445    view1.setElementAt({0, 0}, 100);
446    view2.setElementAt({2, 1}, 200);  // This is (1, 2) in original
447    view3.setElementAt({0, 0}, 300);  // This is (1, 1) in original
448
449    // std::cout << "After modifications through multiple views:" << std::endl;
450    //original.printArray();
451
452    // Verify all changes are reflected
453    if (!(original.getElementAt({0, 0}) == 100)) {
454        std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)";
455        std::cout << std::string("[FAIL] ") + description << std::endl;
456        throw std::runtime_error(description);
457    }
458    if (!(original.getElementAt({1, 2}) == 200)) {
459        std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)";
460        std::cout << std::string("[FAIL] ") + description << std::endl;
461        throw std::runtime_error(description);
462    }
463    if (!(original.getElementAt({1, 1}) == 300)) {
464        std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)";
axes (np_test_5_all.cpp:16631)
16621        return 0;
16622    } else {
16623        std::cout << "  [FAIL] reshape() failed" << std::endl;
16624        return 1;
16625    }
16626}
16627
16628// Test 15: transpose() - Transpose array (SKIPPED - needs implementation)
16629int test_transpose() {
16630    std::cout << "\n[TEST] ma.transpose() - SKIPPED (needs correct transpose API)" << std::endl;
16631    return 0;  // Skip for now
16632}
16633
16634// Test 16: swapaxes() - Swap two axes (SKIPPED - needs implementation)
16635int test_swapaxes() {
16636    std::cout << "\n[TEST] ma.swapaxes() - SKIPPED (needs correct swapaxes API)" << std::endl;
16637    return 0;  // Skip for now
16638}
16639
16640// Test 17: repeat() - Repeat elements
16641int test_repeat() {
clone (np_test_1_all.cpp:24942)
24932      }
24933
24934      // Test reproducibility
24935      numpy::random::PCG64DXSM rng2(11111);
24936      if (rng2.next_uint64() != val) {
24937        std::cout << "  [FAIL] : in np_test_bitgen_pcg64dxsm() : not reproducible";
24938        throw std::runtime_error("np_test_bitgen_pcg64dxsm failed");
24939      }
24940
24941      // Test clone
24942      auto cloned = rng.clone();
24943      if (cloned->name() != "PCG64DXSM") {
24944        std::cout << "  [FAIL] : in np_test_bitgen_pcg64dxsm() : clone name incorrect";
24945        throw std::runtime_error("np_test_bitgen_pcg64dxsm failed");
24946      }
24947
24948      std::cout << " -> tests passed" << std::endl;
24949    }
24950
24951    // ============================================================================
24952    // PHILOX TESTS
count (np_test_1_all.cpp:3616)
3606    // Create larger arrays for performance testing
3607    auto large_arr = NDArray<double>::createOnes({100, 100});
3608    auto broadcast_arr = NDArray<double>::createOnes({1, 100});
3609
3610    // Time the operation (basic timing)
3611    auto start = std::chrono::high_resolution_clock::now();
3612    auto result = large_arr.addArrays(broadcast_arr);
3613    auto end = std::chrono::high_resolution_clock::now();
3614
3615    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
3616    // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl;
3617
3618    // Verify result shape
3619    if (!((result.getShape() == std::vector<size_t>{100, 100}))) {
3620        std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector<size_t>{100, 100}))";
3621        std::cout << std::string("[FAIL] ") + description << std::endl;
3622        throw std::runtime_error(description);
3623    }
3624    if (!(result.getElementAt({50, 50}) == 2.0)) {
3625        std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({50, 50}) == 2.0)";
3626        std::cout << std::string("[FAIL] ") + description << 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      }
index (np_test_5_all.cpp:1283)
1273    // ============================================================================
1274    // SEARCH OPERATION TESTS
1275    // ============================================================================
1276
1277    void np_test_string_index() {
1278      std::cout << "========= index search operation =======================";
1279
1280      std::vector<std::string> strings = { "hello world", "tomorrow" };
1281      auto arr = numpy::char_::array<32>(strings);
1282      auto indices = numpy::char_::index(arr, "o");
1283
1284      bool passed = (indices.getSize() == arr.size());
1285
1286      if (!passed) {
1287        std::cout << "  [FAIL] : in np_test_string_index() : indices size mismatch";
1288        throw std::runtime_error("np_test_string_index failed");
1289      }
1290
1291      std::cout << " -> tests passed" << std::endl;
1292    }
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    // ============================================================================
nbytes (np_test_2_all.cpp:3547)
3537      }
3538
3539      // Test itemsize
3540      if (!(itemsize(arr) == sizeof(double))) {
3541          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))";
3542          std::cout << std::string("[FAIL] ") + description << std::endl;
3543          throw std::runtime_error(description);
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;
ndim (np_test_2_all.cpp:3526)
3516    void testArrayInfo() {
3517      std::cout << "Testing array information functions...\n";
3518
3519      // Test basic info functions
3520      auto arr = createFloat64Array({ 2, 3, 4 });
3521      for (size_t i = 0; i < arr.getSize(); ++i) {
3522        arr.setElementAt({ i / 12, (i / 4) % 3, i % 4 }, static_cast<double>(i));
3523      }
3524
3525      // Test ndim
3526      if (!(ndim(arr) == 3)) {
3527          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ndim(arr) == 3)";
3528          std::cout << std::string("[FAIL] ") + description << std::endl;
3529          throw std::runtime_error(description);
3530      }
3531
3532      // Test size
3533      if (!(size(arr) == 24)) {
3534          std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)";
3535          std::cout << std::string("[FAIL] ") + description << std::endl;
3536          throw std::runtime_error(description);
shape (np_test_1_all.cpp:3751)
3741    }
3742
3743    for (size_t j = 0; j < 3; ++j) {
3744        arr3.setElementAt({0, j}, static_cast<double>((j + 1) * 100));  // [100, 200, 300]
3745    }
3746
3747    // Broadcast all arrays together
3748    std::vector<NDArray<double>> arrays = {arr1, arr2, arr3};
3749    auto broadcasted = broadcast_arrays(arrays);
3750
3751    // All should have shape (2, 3)
3752    for (const auto& arr : broadcasted) {
3753        if (!((arr.getShape() == std::vector<size_t>{2, 3}))) {
3754            std::string description = std::string("test_broadcast_arrays():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((arr.getShape() == std::vector<size_t>{2, 3}))";
3755            std::cout << std::string("[FAIL] ") + description << std::endl;
3756            throw std::runtime_error(description);
3757        }
3758    }
3759
3760    // Check values
3761    if (!(broadcasted[0].getElementAt({0, 1}) == 2.0)) {
size (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43    const double TOLERANCE = 1e-10;
44
45    bool allTrue(const NDArray<bool>& arr) {
46        std::vector<size_t> indices(arr.getShape().size(), 0);
47        do {
48            if (!arr.getElementAt(indices)) {
49                return false;
50            }
51        } while (incrementIndices(indices, arr.getShape()));
52        return true;
53    }
54
55    bool allFalse(const NDArray<bool>& arr) {
56        std::vector<size_t> indices(arr.getShape().size(), 0);