RecordArray#

class numpy::RecordArray#

numpy C++ class.

Example#

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

// Use RecordArray
RecordArray obj;
// ... operations ...

Constructors#

Signature

Location

Example

RecordArray(std::shared_ptr<StructuredDType>dtype, const std::vector<size_t>&shape)

NP_RECARRAY.H:83

View

Operators#

Signature

Return Type

Location

Example

RecordProxy operator[](const std::vector<size_t>&indices)

RecordProxy

NP_RECARRAY.H:88

const RecordProxy operator[](const std::vector<size_t>&indices)

const RecordProxy

NP_RECARRAY.H:92

RecordProxy operator[](size_tindex)

RecordProxy

NP_RECARRAY.H:96

const RecordProxy operator[](size_tindex)

const RecordProxy

NP_RECARRAY.H:103

Sorting#

Signature

Return Type

Location

Example

void sortByField(const std::string &field_name, boolascending = true)

void

NP_RECARRAY.H:294

View

Joining / Splitting#

Signature

Return Type

Location

Example

RecordArray concatenate(const RecordArray &other, size_taxis = 0)

RecordArray

NP_RECARRAY.H:232

View

Type Handling#

Signature

Return Type

Location

Example

RecordArray copy()

RecordArray

NP_RECARRAY.H:175

View

Other Methods#

Signature

Return Type

Location

Example

void printArray(std::ostream &os = std::cout)

void

NP_RECARRAY.H:353

View

void setFieldFromTypedArray(const std::string &field_name, const NDArray<int32>&values)

void

NP_RECARRAY.H:119

void setFieldFromTypedArray(const std::string &field_name, const NDArray<float64>&values)

void

NP_RECARRAY.H:147

void setupFieldAccessors()

void

NP_RECARRAY.H:74

std::string shapeToString(const std::vector<size_t>&shape)

std::string

NP_RECARRAY.H:358

RecordArray slice(const std::vector<std::pair<size_t,size_t>>&ranges)

RecordArray

NP_RECARRAY.H:198

View

std::string toString()

std::string

NP_RECARRAY.H:340

View

Code Examples#

The following examples are extracted from the test suite.

RecordArray (np_test_5_all.cpp:8512)
8502    std::cout << " -> tests passed" << std::endl;
8503  }
8504
8505  // ===========================
8506  // RecordArray Tests
8507  // ===========================
8508
8509#endif  // End of StructuredArray tests
8510
8511#if 0  // TODO: Fix RecordArray (depends on StructuredArray)
8512  void np_test_toString_recarray_basic() {
8513    std::cout << "========= RecordArray::toString() basic =======================";
8514
8515    std::vector<numpy::StructuredField> fields = {
8516        {"id", numpy::DType::INT64, 0, 8},
8517        {"value", numpy::DType::FLOAT32, 8, 4}
8518    };
8519
8520    auto dtype = std::make_shared<numpy::StructuredDType>(fields);
8521    numpy::RecordArray arr(dtype, std::vector<size_t>{3});
sortByField (np_test_1_all.cpp:21704)
21694    //x_field.printArray();
21695
21696    auto y_field = arr.getFieldAsTypedArray<float64>("y");
21697    // std::cout << "Y field as array: ";
21698    //y_field.printArray();
21699
21700    auto copy = arr.copy();
21701    // std::cout << "Copied array:" << std::endl;
21702    //copy.printArray();
21703
21704    arr.sortByField("x", false);
21705    // std::cout << "Array sorted by x (descending):" << std::endl;
21706    //arr.printArray();
21707
21708    std::cout << " -> tests passed" << std::endl;
21709  }
21710
21711  void testRecFunctionsBasic() {
21712    std::cout << "========= testRecFunctionsBasic =======================";
21713
21714    auto dtype = std::make_shared<StructuredDType>(std::vector<std::pair<std::string, DType>>{
concatenate (np_test_1_all.cpp:5432)
5422    auto arr2 = NDArray<int32>::createRange(3, 6, 1);  // [3, 4, 5]
5423    auto arr3 = NDArray<int32>::createRange(6, 9, 1);  // [6, 7, 8]
5424
5425    // std::cout << "Array 1:" << std::endl;
5426    //arr1.printArray();
5427    // std::cout << "Array 2:" << std::endl;
5428    //arr2.printArray();
5429    // std::cout << "Array 3:" << std::endl;
5430    //arr3.printArray();
5431
5432    auto result = concatenate<int32>({arr1, arr2, arr3}, 0);
5433    // std::cout << "Concatenated (axis=0):";
5434    //result.printArray();
5435
5436    // Verify shape and content
5437    if (!(result.getShape()[0] == 9)) {
5438        std::string description = std::string("testConcatenateArrayUtils():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getShape()[0] == 9)";
5439        std::cout << std::string("[FAIL] ") + description << std::endl;
5440        throw std::runtime_error(description);
5441    }
5442    for (int i = 0; i < 9; ++i) {
copy (np_test_1_all.cpp:9812)
9802    //original.printArray();
9803
9804    // The modification should be at position (1,1) in original
9805    if (!(original.getElementAt({1, 1}) == 9999)) {
9806        std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)";
9807        std::cout << std::string("[FAIL] ") + description << std::endl;
9808        throw std::runtime_error(description);
9809    }
9810    // std::cout << "[OK] Slice view shares memory with original";
9811
9812    // Test slice copy (should be independent)
9813    auto slice_copy = original.sliceArray({{2, 4}, {2, 4}});
9814    // std::cout << "Slice copy [2:4, 2:4]:";
9815    //slice_copy.printArray();
9816
9817    slice_copy.setElementAt({0, 0}, 7777);
9818
9819    // std::cout << "After modifying slice copy at (0,0):" << std::endl;
9820    // std::cout << "Original array:" << std::endl;
9821    //original.printArray();
9822    // std::cout << "Slice copy:" << std::endl;
printArray (np_test_1_all.cpp:67)
57        std::vector<size_t> indices(arr.getShape().size(), 0);
58        do {
59            if (arr.getElementAt(indices)) {
60                return false;
61            }
62        } while (incrementIndices(indices, arr.getShape()));
63        return true;
64    }
65
66    template<typename T>
67    void printArray(const NDArray<T>& arr, const std::string& name) {
68        std::cout << name << ": ";
69        if (arr.getShape().size() == 1) {
70            for (size_t i = 0; i < arr.getShape()[0]; ++i) {
71                // std::cout << arr.getElementAt({i}) << " ";
72            }
73        }
74        // std::cout << std::endl;
75    }
76
77    // Helper functions for bitwise operations tests
slice (np_test_1_all.cpp:13009)
12999    // Test substr valid start but excessive length
13000    auto result3 = str1.substr(2, 10);  // start=2, len=10 on string of length 5
13001    if (!(result3.to_string() == "llo")) {
13002        std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result3.to_string() == \"llo\")";
13003        std::cout << std::string("[FAIL] ") + description << std::endl;
13004        throw std::runtime_error(description);
13005    }
13006    // std::cout << "[OK] vstring_ substr(2, 10) on 'hello' returns 'llo'\n";
13007
13008    // Test slice method with out-of-bounds
13009    auto result4 = str1.slice(10, 15);  // slice[10:15] on string of length 5
13010    if (!(result4.to_string() == "")) {
13011        std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result4.to_string() == \"\")";
13012        std::cout << std::string("[FAIL] ") + description << std::endl;
13013        throw std::runtime_error(description);
13014    }
13015    // std::cout << "[OK] vstring_ slice(10, 15) on 'hello' returns empty string\n";
13016
13017    // Test slice with negative indices
13018    auto result5 = str1.slice(-2, -1);  // slice[-2:-1] should be "l"
13019    if (!(result5.to_string() == "l")) {
toString (np_test_1_all.cpp:6826)
6816void testDatetime64CreationDateTime() {
6817    std::cout << "========= testDatetime64CreationDateTime =======================";
6818
6819    // Test default constructor
6820    datetime64 default_dt;
6821    if (!(default_dt.isNaT())) {
6822        std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6823        std::cout << std::string("[FAIL] ") + description << std::endl;
6824        throw std::runtime_error(description);
6825    }
6826    // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6827
6828    // Test value constructor
6829    datetime64 epoch_day(0, DateTimeUnit::Day);
6830    // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
6831
6832    // Test string constructor
6833    datetime64 date_from_string("2024-01-15");
6834    // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl;
6835
6836    datetime64 datetime_from_string("2024-01-15T10:30:45");