SparseAccessor#
-
class pandas::SparseAccessor#
pandas C++ class.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Use SparseAccessor
SparseAccessor obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
pd_sparse_accessor.h:29 |
I/O#
Type Checking#
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
double |
pd_sparse_accessor.h:75 |
|
|
double |
pd_sparse_accessor.h:52 |
|
|
size_t |
pd_sparse_accessor.h:60 |
|
|
const ParentType& |
pd_sparse_accessor.h:32 |
|
|
std::vector<size_t> |
pd_sparse_accessor.h:104 |
|
|
numpy::NDArray<double> |
pd_sparse_accessor.h:84 |
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) {
is_fill (pd_test_1_all.cpp:3314)
3304 throw std::runtime_error("pd_test_sparse_array_element_access failed: arr[3] != 10.0");
3305 }
3306
3307 // Access fill values
3308 if (arr[0] != 0.0) {
3309 std::cout << " [FAIL] : in pd_test_sparse_array_element_access() : arr[0] != fill_value" << std::endl;
3310 throw std::runtime_error("pd_test_sparse_array_element_access failed: arr[0] != fill_value");
3311 }
3312
3313 // Test is_fill
3314 if (!arr.is_fill(0)) {
3315 std::cout << " [FAIL] : in pd_test_sparse_array_element_access() : is_fill(0) should be true" << std::endl;
3316 throw std::runtime_error("pd_test_sparse_array_element_access failed: is_fill(0) should be true");
3317 }
3318
3319 if (arr.is_fill(1)) {
3320 std::cout << " [FAIL] : in pd_test_sparse_array_element_access() : is_fill(1) should be false" << std::endl;
3321 throw std::runtime_error("pd_test_sparse_array_element_access failed: is_fill(1) should be false");
3322 }
3323
3324 std::cout << " -> tests passed" << std::endl;
is_sparse (pd_test_3_all.cpp:20749)
20739 if (std::abs(dense[1] - 1.0) > 0.01 || std::abs(dense[3] - 2.0) > 0.01) {
20740 std::cout << " [FAIL] : to_dense() values failed" << std::endl;
20741 throw std::runtime_error("pd_test_sparse_to_dense: to_dense() values failed");
20742 }
20743
20744 std::cout << " -> tests passed" << std::endl;
20745}
20746
20747// ============================================================================
20748// Test sparse().is_sparse()
20749// ============================================================================
20750
20751void pd_test_sparse_is_sparse() {
20752 std::cout << "========= Series.sparse().is_sparse() ====================";
20753
20754 // Sparse series (more than 50% zeros)
20755 pandas::Series<numpy::float64> sparse_s({0.0, 0.0, 1.0, 0.0, 0.0});
20756 if (sparse_s.sparse(0.0).is_sparse() != true) {
20757 std::cout << " [FAIL] : is_sparse() for sparse data failed" << std::endl;
20758 throw std::runtime_error("pd_test_sparse_is_sparse: is_sparse() for sparse data failed");
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();
fill_value (pd_test_1_all.cpp:3229)
3219 std::cout << " -> tests passed" << std::endl;
3220 }
3221
3222 void pd_test_sparse_array_fill_value_property() {
3223 std::cout << "========= SparseArray: fill_value property ======================= ";
3224
3225 std::vector<numpy::int64> data = {-1, 5, -1, 10, -1};
3226 pandas::SparseArray<numpy::int64> arr(data, static_cast<numpy::int64>(-1));
3227
3228 if (arr.fill_value() != -1) {
3229 std::cout << " [FAIL] : in pd_test_sparse_array_fill_value_property() : fill_value != -1" << std::endl;
3230 throw std::runtime_error("pd_test_sparse_array_fill_value_property failed: fill_value != -1");
3231 }
3232
3233 // Test default fill_value for float (NaN)
3234 pandas::SparseArray<numpy::float64> arr_float;
3235 if (!std::isnan(arr_float.fill_value())) {
3236 std::cout << " [FAIL] : in pd_test_sparse_array_fill_value_property() : default float fill_value should be NaN" << std::endl;
3237 throw std::runtime_error("pd_test_sparse_array_fill_value_property failed: default float fill_value should be NaN");
3238 }
npoints (pd_test_1_all.cpp:3171)
3161 dense.setElementAt({i}, (i == 3 || i == 7) ? 5.0 : 0.0);
3162 }
3163
3164 auto sparse = pandas::SparseArray<numpy::float64>::from_dense(dense, 0.0);
3165
3166 if (sparse.size() != 10) {
3167 std::cout << " [FAIL] : in pd_test_sparse_array_from_dense() : size != 10" << std::endl;
3168 throw std::runtime_error("pd_test_sparse_array_from_dense failed: size != 10");
3169 }
3170
3171 if (sparse.npoints() != 2) {
3172 std::cout << " [FAIL] : in pd_test_sparse_array_from_dense() : npoints != 2" << std::endl;
3173 throw std::runtime_error("pd_test_sparse_array_from_dense failed: npoints != 2");
3174 }
3175
3176 std::cout << " -> tests passed" << std::endl;
3177 }
3178
3179 void pd_test_sparse_array_sp_values_property() {
3180 std::cout << "========= SparseArray: sp_values property ======================= ";
sp_index (pd_test_1_all.cpp:3207)
3197 std::cout << " -> tests passed" << std::endl;
3198 }
3199
3200 void pd_test_sparse_array_sp_index_property() {
3201 std::cout << "========= SparseArray: sp_index property ======================= ";
3202
3203 std::vector<numpy::float64> data = {0.0, 1.0, 0.0, 2.0, 0.0, 3.0};
3204 pandas::SparseArray<numpy::float64> arr(data, 0.0);
3205
3206 const auto& sp_idx = arr.sp_index();
3207 if (sp_idx.getSize() != 3) {
3208 std::cout << " [FAIL] : in pd_test_sparse_array_sp_index_property() : sp_index size != 3" << std::endl;
3209 throw std::runtime_error("pd_test_sparse_array_sp_index_property failed: sp_index size != 3");
3210 }
3211
3212 if (sp_idx.getElementAt({0}) != 1 ||
3213 sp_idx.getElementAt({1}) != 3 ||
3214 sp_idx.getElementAt({2}) != 5) {
3215 std::cout << " [FAIL] : in pd_test_sparse_array_sp_index_property() : sp_index content mismatch" << std::endl;
3216 throw std::runtime_error("pd_test_sparse_array_sp_index_property failed: sp_index content mismatch");
sp_values (pd_test_1_all.cpp:3185)
3175 std::cout << " -> tests passed" << std::endl;
3176 }
3177
3178 void pd_test_sparse_array_sp_values_property() {
3179 std::cout << "========= SparseArray: sp_values property ======================= ";
3180
3181 std::vector<numpy::float64> data = {0.0, 1.0, 0.0, 2.0, 0.0, 3.0};
3182 pandas::SparseArray<numpy::float64> arr(data, 0.0);
3183
3184 const auto& sp_vals = arr.sp_values();
3185 if (sp_vals.getSize() != 3) {
3186 std::cout << " [FAIL] : in pd_test_sparse_array_sp_values_property() : sp_values size != 3" << std::endl;
3187 throw std::runtime_error("pd_test_sparse_array_sp_values_property failed: sp_values size != 3");
3188 }
3189
3190 if (sp_vals.getElementAt({0}) != 1.0 ||
3191 sp_vals.getElementAt({1}) != 2.0 ||
3192 sp_vals.getElementAt({2}) != 3.0) {
3193 std::cout << " [FAIL] : in pd_test_sparse_array_sp_values_property() : sp_values content mismatch" << std::endl;
3194 throw std::runtime_error("pd_test_sparse_array_sp_values_property failed: sp_values content mismatch");