StructuredDType#

class numpy::StructuredDType#

numpy C++ class.

Example#

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

// Use StructuredDType
StructuredDType obj;
// ... operations ...

Constructors#

Signature

Location

Example

StructuredDType(const std::vector<std::pair<std::string,DType>>&field_specs, boolpacked = false)

NP_STRUCTURED_DTYPE.H:104

StructuredDType(const std::vector<FieldDescriptor>&fields, boolpacked = false)

NP_STRUCTURED_DTYPE.H:117

Indexing / Selection#

Signature

Return Type

Location

Example

size_t getAlignment()

size_t

NP_STRUCTURED_DTYPE.H:148

const FieldDescriptor & getField(const std::string &name)

const FieldDescriptor &

NP_STRUCTURED_DTYPE.H:127

View

const FieldDescriptor & getField(size_tindex)

const FieldDescriptor &

NP_STRUCTURED_DTYPE.H:135

View

size_t getFieldAlignment(DTypedtype)

size_t

NP_STRUCTURED_DTYPE.H:90

size_t getFieldCount()

size_t

NP_STRUCTURED_DTYPE.H:146

View

size_t getFieldIndex(const std::string &name)

size_t

NP_STRUCTURED_DTYPE.H:160

const std::vector<FieldDescriptor>& getFields()

const std::vector<FieldDescriptor>&

NP_STRUCTURED_DTYPE.H:125

size_t getTotalSize()

size_t

NP_STRUCTURED_DTYPE.H:147

Other Methods#

Signature

Return Type

Location

Example

void calculateLayout()

void

NP_STRUCTURED_DTYPE.H:65

std::string dtypeToString(DTypedtype)

std::string

NP_STRUCTURED_DTYPE.H:190

bool hasField(const std::string &name)

bool

NP_STRUCTURED_DTYPE.H:142

View

bool isPacked()

bool

NP_STRUCTURED_DTYPE.H:149

std::string toString()

std::string

NP_STRUCTURED_DTYPE.H:168

View

Code Examples#

The following examples are extracted from the test suite.

getField (np_test_2_all.cpp:18700)
18690    void np_test_io_extensions_recfromcsv_dtype_detection() {
18691      std::cout << "========= recfromcsv: automatic dtype detection =======================";
18692
18693      create_csv_file("test_dtypes.csv");
18694
18695      auto data = numpy::io::recfromcsv(csv_test_dir + "test_dtypes.csv");
18696      auto dtype = data.getDType();
18697
18698      // Verify string field detected
18699      auto name_dtype = dtype.getField("name").dtype;
18700      if (name_dtype != numpy::DType::STR32 && name_dtype != numpy::DType::STR16 &&
18701        name_dtype != numpy::DType::STR8 && name_dtype != numpy::DType::STR64) {
18702        std::cout << "  [FAIL] : Expected STRING type for name field" << std::endl;
18703        throw std::runtime_error("np_test_io_extensions_recfromcsv_dtype_detection failed: wrong name dtype");
18704      }
18705
18706      // Verify integer field detected
18707      auto age_dtype = dtype.getField("age").dtype;
18708      if (age_dtype != numpy::DType::INT64 && age_dtype != numpy::DType::INT32) {
18709        std::cout << "  [FAIL] : Expected INT type for age field";
getField (np_test_2_all.cpp:18700)
18690    void np_test_io_extensions_recfromcsv_dtype_detection() {
18691      std::cout << "========= recfromcsv: automatic dtype detection =======================";
18692
18693      create_csv_file("test_dtypes.csv");
18694
18695      auto data = numpy::io::recfromcsv(csv_test_dir + "test_dtypes.csv");
18696      auto dtype = data.getDType();
18697
18698      // Verify string field detected
18699      auto name_dtype = dtype.getField("name").dtype;
18700      if (name_dtype != numpy::DType::STR32 && name_dtype != numpy::DType::STR16 &&
18701        name_dtype != numpy::DType::STR8 && name_dtype != numpy::DType::STR64) {
18702        std::cout << "  [FAIL] : Expected STRING type for name field" << std::endl;
18703        throw std::runtime_error("np_test_io_extensions_recfromcsv_dtype_detection failed: wrong name dtype");
18704      }
18705
18706      // Verify integer field detected
18707      auto age_dtype = dtype.getField("age").dtype;
18708      if (age_dtype != numpy::DType::INT64 && age_dtype != numpy::DType::INT32) {
18709        std::cout << "  [FAIL] : Expected INT type for age field";
getFieldCount (np_test_2_all.cpp:18627)
18617      // Verify shape
18618      if (data.getShape()[0] != 3) {
18619        std::cout << "  [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Expected 3 rows, got "
18620          << data.getShape()[0] << std::endl;
18621        throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: wrong row count");
18622      }
18623
18624      // Verify field count
18625      auto dtype = data.getDType();
18626      if (dtype.getFieldCount() != 5) {
18627        std::cout << "  [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Expected 5 fields, got "
18628          << dtype.getFieldCount() << std::endl;
18629        throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: wrong field count");
18630      }
18631
18632      // Verify field names
18633      if (!dtype.hasField("name") || !dtype.hasField("age") || !dtype.hasField("salary")) {
18634        std::cout << "  [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Missing expected fields";
18635        throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: missing fields");
18636      }
hasField (np_test_2_all.cpp:18634)
18624      // Verify field count
18625      auto dtype = data.getDType();
18626      if (dtype.getFieldCount() != 5) {
18627        std::cout << "  [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Expected 5 fields, got "
18628          << dtype.getFieldCount() << std::endl;
18629        throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: wrong field count");
18630      }
18631
18632      // Verify field names
18633      if (!dtype.hasField("name") || !dtype.hasField("age") || !dtype.hasField("salary")) {
18634        std::cout << "  [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Missing expected fields";
18635        throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: missing fields");
18636      }
18637
18638      // Verify data values
18639      auto name = data.getFieldValue<numpy::str32>({ 0 }, "name");
18640      if (std::string(name) != "Alice") {
18641        std::cout << "  [FAIL] : Expected 'Alice', got '" << std::string(name) << "'";
18642        throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: wrong name");
18643      }
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");