HDF5Store ========= .. cpp:class:: pandas::HDF5Store pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use HDF5Store HDF5Store obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit HDF5Store( const std::string& path, const std::string& mode = "a", std::optional complevel = std::nullopt, std::optional complib = std::nullopt, bool fletcher32 = false)`` - pd_hdf5.h:74 - * - ``HDF5Store(const HDF5Store&) = delete`` - pd_hdf5.h:111 - * - ``HDF5Store(HDF5Store&&) = default`` - pd_hdf5.h:115 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``HighFive::Group create_group(const std::string& name)`` - HighFive::Group - pd_hdf5.h:188 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``HighFive::Group get_group(const std::string& name)`` - HighFive::Group - pd_hdf5.h:198 - :ref:`View ` Combining --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void append( const std::string& key, const DataFrame& value, std::optional format = std::nullopt, std::optional> axes = std::nullopt, bool index = true, bool append_mode = true, std::optional> data_columns = std::nullopt, std::optional> min_itemsize = std::nullopt, std::optional nan_rep = std::nullopt, std::optional chunksize = std::nullopt, std::optional expectedrows = std::nullopt, bool dropna = false, bool track_times = true)`` - void - pd_hdf5.h:670 - :ref:`View ` Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector keys() const`` - std::vector - pd_hdf5.h:173 - :ref:`View ` * - ``std::vector keys(const std::string& group_name) const`` - std::vector - pd_hdf5.h:180 - :ref:`View ` * - ``std::vector keys_pandas(const std::string& include = "pandas") const`` - std::vector - pd_hdf5.h:369 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_open() const`` - bool - pd_hdf5.h:336 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void close()`` - void - pd_hdf5.h:328 - :ref:`View ` * - ``bool contains(const std::string& key) const`` - bool - pd_hdf5.h:352 - :ref:`View ` * - ``bool exists(const std::string& name) const`` - bool - pd_hdf5.h:166 - :ref:`View ` * - ``HighFive::File& file()`` - HighFive::File& - pd_hdf5.h:317 - :ref:`View ` * - ``const HighFive::File& file() const`` - const HighFive::File& - pd_hdf5.h:318 - :ref:`View ` * - ``std::string filename() const`` - std::string - pd_hdf5.h:344 - :ref:`View ` * - ``complib_(complib), fletcher32_(fletcher32)`` - complib_(complib), - pd_hdf5.h:81 - * - ``void flush()`` - void - pd_hdf5.h:310 - * - ``std::vector groups() const`` - std::vector - pd_hdf5.h:520 - :ref:`View ` * - ``bool has_attribute(const std::string& dataset_name, const std::string& attr_name)`` - bool - pd_hdf5.h:279 - :ref:`View ` * - ``std::string info() const`` - std::string - pd_hdf5.h:483 - :ref:`View ` * - ``std::string path() const`` - std::string - pd_hdf5.h:305 - :ref:`View ` * - ``void put( const std::string& key, const DataFrame& value, std::optional format = std::nullopt, bool index = true, bool append_mode = false, std::optional> data_columns = std::nullopt, std::optional encoding = std::nullopt, bool dropna = false, bool track_times = true)`` - void - pd_hdf5.h:632 - * - ``std::string read_attribute(const std::string& dataset_name, const std::string& attr_name)`` - std::string - pd_hdf5.h:232 - :ref:`View ` * - ``T read_attribute_as(const std::string& dataset_name, const std::string& attr_name)`` - T - pd_hdf5.h:256 - :ref:`View ` * - ``std::vector> read_dataset_2d(const std::string& name)`` - std::vector> - pd_hdf5.h:156 - :ref:`View ` * - ``void remove(const std::string& name)`` - void - pd_hdf5.h:296 - :ref:`View ` * - ``void remove_pandas( const std::string& key, [[maybe_unused]] std::optional where = std::nullopt, [[maybe_unused]] std::optional start = std::nullopt, [[maybe_unused]] std::optional stop = std::nullopt)`` - void - pd_hdf5.h:462 - :ref:`View ` * - ``size_t size() const`` - size_t - pd_hdf5.h:360 - :ref:`View ` * - ``std::ifstream test(path)`` - std::ifstream - pd_hdf5.h:93 - :ref:`View ` * - ``walk(const std::string& where = "/") const`` - - pd_hdf5.h:558 - :ref:`View ` * - ``walk_fn(subgroup, full_path)`` - - pd_hdf5.h:584 - * - ``walk_fn(subgroup, full_path)`` - - pd_hdf5.h:611 - * - ``walk_fn(grp, where)`` - - pd_hdf5.h:616 - * - ``walk_group(subgroup, full_path)`` - - pd_hdf5.h:409 - * - ``walk_group(subgroup, full_path)`` - - pd_hdf5.h:449 - * - ``walk_group(subgroup, full_path)`` - - pd_hdf5.h:532 - * - ``walk_group(subgroup, full_path)`` - - pd_hdf5.h:545 - * - ``void write_attribute(const std::string& dataset_name, const std::string& attr_name, const std::string& value)`` - void - pd_hdf5.h:205 - :ref:`View ` * - ``void write_attribute(const std::string& dataset_name, const std::string& attr_name, const T& value)`` - void - pd_hdf5.h:219 - :ref:`View ` * - ``void write_dataset(const std::string& name, const std::vector& data)`` - void - pd_hdf5.h:122 - :ref:`View ` * - ``void write_dataset_2d(const std::string& name, const std::vector>& data)`` - void - pd_hdf5.h:133 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-hdf5store-create_group-0: .. dropdown:: create_group (pd_test_3_all.cpp:19278) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19268 :emphasize-lines: 11 std::remove(path.c_str()); std::cout << " test_write_read_2d passed\n"; } void test_groups() { std::string path = "test_hdf5_groups.h5"; { pandas::HDF5Store store(path, "w"); HighFive::Group hdf_grp = store.create_group("dataframe1"); hdf_grp.createDataSet("col_a", std::vector{1, 2, 3}); hdf_grp.createDataSet("col_b", std::vector{1.1, 2.2, 3.3}); } { pandas::HDF5Store store(path, "r"); auto keys = store.keys(); if (keys.size() != 1 || keys[0] != "dataframe1") throw std::runtime_error("test_groups: root keys mismatch"); .. _example-hdf5store-get_group-1: .. dropdown:: get_group (pd_test_2_all.cpp:20487) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20477 :emphasize-lines: 11 ++g_fail; } } static bool approx_eq(double a, double b, double tol = 1e-9) { if (std::isnan(a) && std::isnan(b)) return true; return std::abs(a - b) < tol; } // ===================================================================== // Test: get_group() with exclude_cols removes groupby columns // ===================================================================== void pd_test_groupby_apply_get_group_exclude() { std::cout << " -- pd_test_groupby_apply_get_group_exclude --" << std::endl; pandas::DataFrame df; df.add_column("key", std::vector{"a", "a", "b", "b"}); df.add_column("val1", std::vector{1.0, 2.0, 3.0, 4.0}); df.add_column("val2", std::vector{10.0, 20.0, 30.0, 40.0}); .. _example-hdf5store-append-2: .. dropdown:: append (pd_test_1_all.cpp:10650) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10640 :emphasize-lines: 11 std::cout << "========= append ========================="; // Use same categories for both arrays (required by CategoricalArray::concat) std::vector cats = {"a", "b", "c", "d"}; pandas::CategoricalArray arr1({"a", "b"}, cats); pandas::CategoricalIndex idx1(arr1); pandas::CategoricalArray arr2({"c", "d"}, cats); pandas::CategoricalIndex idx2(arr2); auto appended = idx1.append(idx2); bool passed = (appended.size() == 4); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_append() : append check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_append failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-hdf5store-keys-3: .. dropdown:: keys (pd_test_1_all.cpp:16319) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16309 :emphasize-lines: 11 } // Test default value passed = attrs.get("missing", 99) == 99; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : default value" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: default value"); } // Test keys auto keys = attrs.keys(); passed = keys.size() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : keys()" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: keys()"); } // Test remove passed = attrs.remove("count") && !attrs.contains("count"); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl; .. _example-hdf5store-keys-4: .. dropdown:: keys (pd_test_1_all.cpp:16319) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16309 :emphasize-lines: 11 } // Test default value passed = attrs.get("missing", 99) == 99; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : default value" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: default value"); } // Test keys auto keys = attrs.keys(); passed = keys.size() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : keys()" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: keys()"); } // Test remove passed = attrs.remove("count") && !attrs.contains("count"); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl; .. _example-hdf5store-keys_pandas-5: .. dropdown:: keys_pandas (pd_test_3_all.cpp:19568) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19558 :emphasize-lines: 11 HighFive::Group df_grp2 = store.create_group("dataframe2"); std::vector col2{3.0, 4.0}; df_grp2.createDataSet("column_b", col2); // Write raw dataset (not a pandas object) std::vector raw{1, 2, 3}; store.write_dataset("raw_data", raw); // Test keys with "pandas" filter - should find groups with data auto pandas_keys = store.keys_pandas("pandas"); if (pandas_keys.size() < 2) { throw std::runtime_error("keys_pandas('pandas') should find at least 2 DataFrame groups"); } // Test keys with "native" filter - should find everything auto native_keys = store.keys_pandas("native"); if (native_keys.size() < 3) { throw std::runtime_error("keys_pandas('native') should find at least 3 objects"); } } .. _example-hdf5store-is_open-6: .. dropdown:: is_open (pd_test_2_all.cpp:4092) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4082 :emphasize-lines: 11 } if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") { std::cout << " [FAIL] : datetime_format property incorrect" << std::endl; throw std::runtime_error("datetime_format property incorrect"); } if (writer.if_sheet_exists() != "replace") { std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl; throw std::runtime_error("if_sheet_exists property incorrect"); } if (!writer.is_open()) { std::cout << " [FAIL] : is_open should be true" << std::endl; throw std::runtime_error("is_open should be true"); } writer.close(); if (writer.is_open()) { std::cout << " [FAIL] : is_open should be false after close" << std::endl; throw std::runtime_error("is_open should be false after close"); } .. _example-hdf5store-close-7: .. dropdown:: close (pd_test_2_all.cpp:3486) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3476 :emphasize-lines: 11 // Check ZIP signature (PK..) file.seekg(0, std::ios::beg); char sig[4]; file.read(sig, 4); if (sig[0] != 'P' || sig[1] != 'K' || sig[2] != 0x03 || sig[3] != 0x04) { std::cout << " [FAIL] : in pd_test_excel_basic() : Invalid ZIP signature" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: invalid ZIP signature"); } file.close(); std::cout << " -> tests passed" << std::endl; } // Test Excel export with custom sheet name void pd_test_excel_sheet_name() { std::cout << "========= Excel export with sheet name ============="; std::map> data = { {"X", {1.1, 2.2, 3.3}}, {"Y", {4.4, 5.5, 6.6}} .. _example-hdf5store-contains-8: .. dropdown:: contains (pd_test_1_all.cpp:2200) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2190 :emphasize-lines: 11 // Test: contains method // ============================================================================ void test_contains() { std::cout << "========= IntervalArray: contains ======================= "; std::vector breaks = {0.0, 1.0, 2.0, 3.0}; // Right-closed intervals: (0, 1], (1, 2], (2, 3] auto arr_right = pandas::IntervalArrayFloat64::from_breaks(breaks, pandas::IntervalClosed::Right); // Test contains(1.0) - should be in interval 0 but not 1 (since 1 is exclusive on left of interval 1) auto contains_1 = arr_right.contains(1.0); // (0, 1] contains 1: yes, (1, 2] contains 1: no (open on left), (2, 3] contains 1: no if (contains_1[0].value_or(false) != true || contains_1[1].value_or(true) != false || contains_1[2].value_or(true) != false) { std::cout << "[FAIL] : in test_contains() : right-closed contains 1.0" << std::endl; return; } // Left-closed intervals: [0, 1), [1, 2), [2, 3) .. _example-hdf5store-exists-9: .. dropdown:: exists (pd_test_1_all.cpp:13934) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13924 :emphasize-lines: 11 // Check B: should be [40, 5, 60] const auto& b_col = result["B"]; if (!approx_equal(std::stod(b_col.get_value_str(0)), 40.0) || !approx_equal(std::stod(b_col.get_value_str(1)), 5.0) || !approx_equal(std::stod(b_col.get_value_str(2)), 60.0)) { passed = false; std::cout << " [FAIL] : in pd_test_joining_combine_first() : column B wrong" << std::endl; throw std::runtime_error("pd_test_joining_combine_first failed: col B"); } // Check C exists (from right only) if (!result.has_column("C")) { passed = false; std::cout << " [FAIL] : in pd_test_joining_combine_first() : missing column C" << std::endl; throw std::runtime_error("pd_test_joining_combine_first failed: col C missing"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== .. _example-hdf5store-file-10: .. dropdown:: file (pd_test_2_all.cpp:3463) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3453 :emphasize-lines: 11 {"C", {100, 200, 300, 400, 500}} }; pandas::DataFrame df(data); // Export to Excel std::string filepath = "temp/pd_test_excel_basic.xlsx"; df.to_excel(filepath); // Verify file was created std::ifstream file(filepath, std::ios::binary); if (!file.good()) { std::cout << " [FAIL] : in pd_test_excel_basic() : File was not created" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: file not created"); } // Check file size is reasonable (valid XLSX should be > 1KB) file.seekg(0, std::ios::end); auto size = file.tellg(); if (size < 1000) { std::cout << " [FAIL] : in pd_test_excel_basic() : File size too small: " << size << std::endl; .. _example-hdf5store-file-11: .. dropdown:: file (pd_test_2_all.cpp:3463) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3453 :emphasize-lines: 11 {"C", {100, 200, 300, 400, 500}} }; pandas::DataFrame df(data); // Export to Excel std::string filepath = "temp/pd_test_excel_basic.xlsx"; df.to_excel(filepath); // Verify file was created std::ifstream file(filepath, std::ios::binary); if (!file.good()) { std::cout << " [FAIL] : in pd_test_excel_basic() : File was not created" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: file not created"); } // Check file size is reasonable (valid XLSX should be > 1KB) file.seekg(0, std::ios::end); auto size = file.tellg(); if (size < 1000) { std::cout << " [FAIL] : in pd_test_excel_basic() : File size too small: " << size << std::endl; .. _example-hdf5store-filename-12: .. dropdown:: filename (pd_test_3_all.cpp:19497) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19487 :emphasize-lines: 11 { pandas::HDF5Store store(path, "w"); // Test is_open if (!store.is_open()) { throw std::runtime_error("Store should be open initially"); } // Test filename if (store.filename() != path) { throw std::runtime_error("filename() should return path"); } // Test close store.close(); if (store.is_open()) { throw std::runtime_error("Store should be closed after close()"); } } .. _example-hdf5store-groups-13: .. dropdown:: groups (pd_test_2_all.cpp:20864) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20854 :emphasize-lines: 11 // ===================================================================== // Per-group expanding tests // ===================================================================== void test_series_groupby_expanding_sum() { std::cout << " -- test_series_groupby_expanding_sum --" << std::endl; // Two groups: A=[1,2,3], B=[10,20] std::vector vals = {1.0, 10.0, 2.0, 20.0, 3.0}; pandas::Series data(vals); pandas::Series groups({"A", "B", "A", "B", "A"}); auto sgb = data.groupby(groups); pandas::SeriesGroupByExpandingWindow ew(sgb, 1); auto result = ew.sum(); check(result.size() == 5, "size_5"); // A group: expanding sum = 1, 3, 6 // B group: expanding sum = 10, 30 // Original order: [A:1, B:10, A:3, B:30, A:6] check(approx_eq(result[0], 1.0), "A_exp_sum_0"); .. _example-hdf5store-has_attribute-14: .. dropdown:: has_attribute (pd_test_3_all.cpp:19325) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19315 :emphasize-lines: 11 { pandas::HDF5Store store(path, "r"); auto desc = store.read_attribute("data", "description"); if (desc != "Test dataset") throw std::runtime_error("test_attributes: description mismatch"); auto ver = store.read_attribute_as("data", "version"); if (ver != 1) throw std::runtime_error("test_attributes: version mismatch"); if (!store.has_attribute("data", "description")) throw std::runtime_error("test_attributes: has_attribute failed"); } std::remove(path.c_str()); std::cout << " test_attributes passed\n"; } void test_integer_types() { std::string path = "test_hdf5_int.h5"; .. _example-hdf5store-info-15: .. dropdown:: info (pd_test_1_all.cpp:7122) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7112 :emphasize-lines: 11 } if (!empty_params_error) { std::cout << " [FAIL] : select_dtypes empty params should throw" << std::endl; throw std::runtime_error("pd_test_dataframe_select_dtypes failed: empty params error"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // Test: info() method // ===================================================================== void pd_test_dataframe_info() { std::cout << "========= info ========================"; // Test basic info() with stringstream std::map> data = { {"A", {1, 2, 3, 4, 5}}, {"B", {10, 20, 30, 40, 50}}, {"C", {100, 200, 300, 400, 500}} }; .. _example-hdf5store-path-16: .. dropdown:: path (pd_test_2_all.cpp:4071) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4061 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_excel_writer_properties() { std::cout << "========= ExcelWriter properties ==================="; pandas::ExcelWriter writer("temp/test_writer_props.xlsx", "xlsxwriter", "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace"); if (writer.path() != "temp/test_writer_props.xlsx") { std::cout << " [FAIL] : path property incorrect" << std::endl; throw std::runtime_error("path property incorrect"); } if (writer.engine() != "xlsxwriter") { std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl; throw std::runtime_error("engine property incorrect"); } if (writer.date_format() != "YYYY/MM/DD") { std::cout << " [FAIL] : date_format property incorrect" << std::endl; throw std::runtime_error("date_format property incorrect"); .. _example-hdf5store-read_attribute-17: .. dropdown:: read_attribute (pd_test_3_all.cpp:19317) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19307 :emphasize-lines: 11 { pandas::HDF5Store store(path, "w"); store.write_dataset("data", std::vector{1, 2, 3}); store.write_attribute("data", "description", "Test dataset"); store.write_attribute("data", "version", 1); } { pandas::HDF5Store store(path, "r"); auto desc = store.read_attribute("data", "description"); if (desc != "Test dataset") throw std::runtime_error("test_attributes: description mismatch"); auto ver = store.read_attribute_as("data", "version"); if (ver != 1) throw std::runtime_error("test_attributes: version mismatch"); if (!store.has_attribute("data", "description")) throw std::runtime_error("test_attributes: has_attribute failed"); } .. _example-hdf5store-read_attribute_as-18: .. dropdown:: read_attribute_as (pd_test_3_all.cpp:19321) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19311 :emphasize-lines: 11 store.write_attribute("data", "description", "Test dataset"); store.write_attribute("data", "version", 1); } { pandas::HDF5Store store(path, "r"); auto desc = store.read_attribute("data", "description"); if (desc != "Test dataset") throw std::runtime_error("test_attributes: description mismatch"); auto ver = store.read_attribute_as("data", "version"); if (ver != 1) throw std::runtime_error("test_attributes: version mismatch"); if (!store.has_attribute("data", "description")) throw std::runtime_error("test_attributes: has_attribute failed"); } std::remove(path.c_str()); std::cout << " test_attributes passed\n"; } .. _example-hdf5store-read_dataset_2d-19: .. dropdown:: read_dataset_2d (pd_test_3_all.cpp:19260) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19250 :emphasize-lines: 11 std::vector> data = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} }; store.write_dataset_2d("matrix", data); } // Read { pandas::HDF5Store store(path, "r"); auto result = store.read_dataset_2d("matrix"); if (result.size() != 2) throw std::runtime_error("test_write_read_2d: row count mismatch"); if (result[0].size() != 3) throw std::runtime_error("test_write_read_2d: col count mismatch"); if (std::abs(result[1][2] - 6.0) > 1e-10) throw std::runtime_error("test_write_read_2d: value mismatch"); } std::remove(path.c_str()); std::cout << " test_write_read_2d passed\n"; .. _example-hdf5store-remove-20: .. dropdown:: remove (pd_test_1_all.cpp:16327) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16317 :emphasize-lines: 11 // Test keys auto keys = attrs.keys(); passed = keys.size() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : keys()" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: keys()"); } // Test remove passed = attrs.remove("count") && !attrs.contains("count"); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: remove"); } // Test has_type passed = attrs.has_type("author") && !attrs.has_type("author"); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : has_type" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: has_type"); .. _example-hdf5store-remove_pandas-21: .. dropdown:: remove_pandas (pd_test_3_all.cpp:19600) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19590 :emphasize-lines: 11 HighFive::Group hdf_grp = store.create_group("to_remove"); std::vector data{1.0, 2.0}; hdf_grp.createDataSet("col", data); // Verify it exists if (!store.contains("to_remove")) { throw std::runtime_error("Group should exist before remove"); } // Remove it store.remove_pandas("to_remove"); // Verify it's gone if (store.contains("to_remove")) { throw std::runtime_error("Group should not exist after remove"); } // Remove nonexistent (should not throw) store.remove_pandas("does_not_exist"); } std::cout << "passed\n"; .. _example-hdf5store-size-22: .. dropdown:: size (pd_test_1_all.cpp:22) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12 :emphasize-lines: 11 #include "../pandas/pd_boolean_array.h" namespace dataframe_tests { namespace dataframe_tests_boolean_array { void pd_test_boolean_array_constructors() { std::cout << "========= BooleanArray: constructors ======================= "; // Default constructor pandas::BooleanArray arr1; if (arr1.size() != 0) { std::cout << " [FAIL] : in pd_test_boolean_array_constructors() : default constructor size != 0" << std::endl; throw std::runtime_error("pd_test_boolean_array_constructors failed: default constructor size != 0"); } // Initializer list constructor pandas::BooleanArray arr2({ std::optional(true), std::optional(false), std::nullopt, std::optional(true) .. _example-hdf5store-test-23: .. dropdown:: test (pd_test_3_all.cpp:18161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18151 :emphasize-lines: 11 try { pd_test_combine_requires_func(); } catch (...) { failures++; } // Phase 23: Export methods verification try { pd_test_to_html_params(); } catch (...) { failures++; } try { pd_test_to_excel_params(); } catch (...) { failures++; } try { pd_test_to_feather_params(); } catch (...) { failures++; } try { pd_test_to_hdf_params(); } catch (...) { failures++; } try { pd_test_to_stata_params(); } catch (...) { failures++; } try { pd_test_to_gbq_throws(); } catch (...) { failures++; } if (failures > 0) { std::cout << "\n*** " << failures << " test(s) failed ***\n"; } return failures; } } // namespace dataframe_tests_io_params // Main entry point for pd_test_all.cpp int pd_test_dataframe_io_params_main() { std::cout << "====================================== running pd_test_dataframe_io_params ====================================== " << std::endl; .. _example-hdf5store-walk-24: .. dropdown:: walk (pd_test_3_all.cpp:19683) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19673 :emphasize-lines: 11 { pandas::HDF5Store store(path, "w"); // Create structure HighFive::Group hdf_grp = store.create_group("folder"); std::vector data{1.0, 2.0}; store.write_dataset("root_data", data); hdf_grp.createDataSet("nested_data", data); // Walk from root auto result = store.walk("/"); if (result.empty()) { throw std::runtime_error("walk() should return at least one entry"); } // First entry should be root auto& [path_str, subgroups, leaves] = result[0]; if (path_str != "/") { throw std::runtime_error("First entry should be root '/'"); } .. _example-hdf5store-write_attribute-25: .. dropdown:: write_attribute (pd_test_3_all.cpp:19311) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19301 :emphasize-lines: 11 std::remove(path.c_str()); std::cout << " test_groups passed\n"; } void test_attributes() { std::string path = "test_hdf5_attr.h5"; { pandas::HDF5Store store(path, "w"); store.write_dataset("data", std::vector{1, 2, 3}); store.write_attribute("data", "description", "Test dataset"); store.write_attribute("data", "version", 1); } { pandas::HDF5Store store(path, "r"); auto desc = store.read_attribute("data", "description"); if (desc != "Test dataset") throw std::runtime_error("test_attributes: description mismatch"); auto ver = store.read_attribute_as("data", "version"); .. _example-hdf5store-write_attribute-26: .. dropdown:: write_attribute (pd_test_3_all.cpp:19311) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19301 :emphasize-lines: 11 std::remove(path.c_str()); std::cout << " test_groups passed\n"; } void test_attributes() { std::string path = "test_hdf5_attr.h5"; { pandas::HDF5Store store(path, "w"); store.write_dataset("data", std::vector{1, 2, 3}); store.write_attribute("data", "description", "Test dataset"); store.write_attribute("data", "version", 1); } { pandas::HDF5Store store(path, "r"); auto desc = store.read_attribute("data", "description"); if (desc != "Test dataset") throw std::runtime_error("test_attributes: description mismatch"); auto ver = store.read_attribute_as("data", "version"); .. _example-hdf5store-write_dataset-27: .. dropdown:: write_dataset (pd_test_3_all.cpp:19225) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19215 :emphasize-lines: 11 namespace dataframe_tests { namespace dataframe_tests_hdf5_real { void test_write_read_1d() { std::string path = "test_hdf5_real.h5"; // Write { pandas::HDF5Store store(path, "w"); std::vector data = {1.0, 2.0, 3.0, 4.0, 5.0}; store.write_dataset("my_data", data); } // Read { pandas::HDF5Store store(path, "r"); auto result = store.read_dataset("my_data"); if (result.size() != 5) throw std::runtime_error("test_write_read_1d: size mismatch"); if (std::abs(result[0] - 1.0) > 1e-10) throw std::runtime_error("test_write_read_1d: value mismatch at [0]"); .. _example-hdf5store-write_dataset_2d-28: .. dropdown:: write_dataset_2d (pd_test_3_all.cpp:19254) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19244 :emphasize-lines: 11 void test_write_read_2d() { std::string path = "test_hdf5_2d.h5"; // Write { pandas::HDF5Store store(path, "w"); std::vector> data = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} }; store.write_dataset_2d("matrix", data); } // Read { pandas::HDF5Store store(path, "r"); auto result = store.read_dataset_2d("matrix"); if (result.size() != 2) throw std::runtime_error("test_write_read_2d: row count mismatch"); if (result[0].size() != 3) throw std::runtime_error("test_write_read_2d: col count mismatch");