DataFrameSparseAccessor#

class pandas::DataFrameSparseAccessor#

pandas C++ class.

Example#

#include <pandas/pandas.h>
using namespace pandas;

// Use DataFrameSparseAccessor
DataFrameSparseAccessor obj;
// ... operations ...

Constructors#

Signature

Location

Example

explicit DataFrameSparseAccessor(const DataFrame& parent)

pd_sparse_accessor.h:181

Construction#

Signature

Return Type

Location

Example

static DataFrame from_spmatrix( const std::vector<double>& data, const std::vector<size_t>& row_idx, const std::vector<size_t>& col_idx, size_t nrows, size_t ncols, const std::vector<std::string>& index = {}, const std::vector<std::string>& columns = {})

static DataFrame

pd_sparse_accessor.h:208

I/O#

Signature

Return Type

Location

Example

to_coo() const

pd_sparse_accessor.h:202

View

DataFrame to_dense() const

DataFrame

pd_sparse_accessor.h:194

View

Other Methods#

Signature

Return Type

Location

Example

double density() const

double

pd_sparse_accessor.h:188

View

Code Examples#

The following examples are extracted from the test suite.

to_coo (pd_test_3_all.cpp:21918)
21908        throw std::runtime_error("SparseAccessor.to_dense(): wrong size");
21909    }
21910    if (dense[0] != 0.0 || dense[1] != 1.0 || dense[2] != 0.0 || dense[3] != 2.0) {
21911        throw std::runtime_error("SparseAccessor.to_dense(): wrong values");
21912    }
21913
21914    std::cout << " -> tests passed" << std::endl;
21915}
21916
21917void test_sparse_to_coo() {
21918    std::cout << "========= SparseAccessor.to_coo() ==================";
21919
21920    pandas::Series<numpy::float64> s({0.0, 1.0, 0.0, 2.0, 0.0});
21921    auto sparse = s.sparse();
21922
21923    auto [data, rows, cols, shape] = sparse.to_coo();
21924    if (data.size() != 2) {
21925        throw std::runtime_error("SparseAccessor.to_coo(): expected 2 data points");
21926    }
21927    if (data[0] != 1.0 || data[1] != 2.0) {
21928        throw std::runtime_error("SparseAccessor.to_coo(): wrong data values");
to_dense (pd_test_1_all.cpp:3272)
3262        std::cout << " -> tests passed" << std::endl;
3263    }
3264
3265    void pd_test_sparse_array_to_dense() {
3266        std::cout << "========= SparseArray: to_dense ======================= ";
3267
3268        std::vector<numpy::float64> data = {0.0, 1.0, 0.0, 2.0, 0.0};
3269        pandas::SparseArray<numpy::float64> arr(data, 0.0);
3270
3271        auto dense = arr.to_dense();
3272        if (dense.getSize() != 5) {
3273            std::cout << "  [FAIL] : in pd_test_sparse_array_to_dense() : dense size != 5" << std::endl;
3274            throw std::runtime_error("pd_test_sparse_array_to_dense failed: dense size != 5");
3275        }
3276
3277        if (dense.getElementAt({0}) != 0.0 ||
3278            dense.getElementAt({1}) != 1.0 ||
3279            dense.getElementAt({2}) != 0.0 ||
3280            dense.getElementAt({3}) != 2.0 ||
3281            dense.getElementAt({4}) != 0.0) {
density (pd_test_1_all.cpp:3247)
3237            std::cout << "  [FAIL] : in pd_test_sparse_array_fill_value_property() : default float fill_value should be NaN" << std::endl;
3238            throw std::runtime_error("pd_test_sparse_array_fill_value_property failed: default float fill_value should be NaN");
3239        }
3240
3241        std::cout << " -> tests passed" << std::endl;
3242    }
3243
3244    void pd_test_sparse_array_density() {
3245        std::cout << "========= SparseArray: density ======================= ";
3246
3247        // 20% density (2 non-fill out of 10)
3248        std::vector<numpy::float64> data = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0};
3249        pandas::SparseArray<numpy::float64> arr(data, 0.0);
3250
3251        double density = arr.density();
3252        if (std::abs(density - 0.2) > 0.001) {
3253            std::cout << "  [FAIL] : in pd_test_sparse_array_density() : density != 0.2, got " << density << std::endl;
3254            throw std::runtime_error("pd_test_sparse_array_density failed: density != 0.2");
3255        }
3256
3257        double sparsity = arr.sparsity();