DataFrameSparseAccessor ======================= .. cpp:class:: pandas::DataFrameSparseAccessor pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use DataFrameSparseAccessor DataFrameSparseAccessor obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit DataFrameSparseAccessor(const DataFrame& parent)`` - pd_sparse_accessor.h:181 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static DataFrame from_spmatrix( const std::vector& data, const std::vector& row_idx, const std::vector& col_idx, size_t nrows, size_t ncols, const std::vector& index = {}, const std::vector& columns = {})`` - static DataFrame - pd_sparse_accessor.h:208 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``to_coo() const`` - - pd_sparse_accessor.h:202 - :ref:`View ` * - ``DataFrame to_dense() const`` - DataFrame - pd_sparse_accessor.h:194 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``double density() const`` - double - pd_sparse_accessor.h:188 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-dataframesparseaccessor-to_coo-0: .. dropdown:: to_coo (pd_test_3_all.cpp:21918) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21908 :emphasize-lines: 11 throw std::runtime_error("SparseAccessor.to_dense(): wrong size"); } if (dense[0] != 0.0 || dense[1] != 1.0 || dense[2] != 0.0 || dense[3] != 2.0) { throw std::runtime_error("SparseAccessor.to_dense(): wrong values"); } std::cout << " -> tests passed" << std::endl; } void test_sparse_to_coo() { std::cout << "========= SparseAccessor.to_coo() =================="; pandas::Series s({0.0, 1.0, 0.0, 2.0, 0.0}); auto sparse = s.sparse(); auto [data, rows, cols, shape] = sparse.to_coo(); if (data.size() != 2) { throw std::runtime_error("SparseAccessor.to_coo(): expected 2 data points"); } if (data[0] != 1.0 || data[1] != 2.0) { throw std::runtime_error("SparseAccessor.to_coo(): wrong data values"); .. _example-dataframesparseaccessor-to_dense-1: .. dropdown:: to_dense (pd_test_1_all.cpp:3272) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3262 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_sparse_array_to_dense() { std::cout << "========= SparseArray: to_dense ======================= "; std::vector data = {0.0, 1.0, 0.0, 2.0, 0.0}; pandas::SparseArray arr(data, 0.0); auto dense = arr.to_dense(); if (dense.getSize() != 5) { std::cout << " [FAIL] : in pd_test_sparse_array_to_dense() : dense size != 5" << std::endl; throw std::runtime_error("pd_test_sparse_array_to_dense failed: dense size != 5"); } if (dense.getElementAt({0}) != 0.0 || dense.getElementAt({1}) != 1.0 || dense.getElementAt({2}) != 0.0 || dense.getElementAt({3}) != 2.0 || dense.getElementAt({4}) != 0.0) { .. _example-dataframesparseaccessor-density-2: .. dropdown:: density (pd_test_1_all.cpp:3247) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3237 :emphasize-lines: 11 std::cout << " [FAIL] : in pd_test_sparse_array_fill_value_property() : default float fill_value should be NaN" << std::endl; throw std::runtime_error("pd_test_sparse_array_fill_value_property failed: default float fill_value should be NaN"); } std::cout << " -> tests passed" << std::endl; } void pd_test_sparse_array_density() { std::cout << "========= SparseArray: density ======================= "; // 20% density (2 non-fill out of 10) std::vector data = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0}; pandas::SparseArray arr(data, 0.0); double density = arr.density(); if (std::abs(density - 0.2) > 0.001) { std::cout << " [FAIL] : in pd_test_sparse_array_density() : density != 0.2, got " << density << std::endl; throw std::runtime_error("pd_test_sparse_array_density failed: density != 0.2"); } double sparsity = arr.sparsity();