SeriesBase#
-
class pandas::SeriesBase#
Core data container class in the pandas namespace.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Use SeriesBase
SeriesBase obj;
// ... operations ...
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
virtual std::string |
pd_series_base.h:140 |
Data Manipulation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
virtual void |
pd_series_base.h:126 |
|
|
virtual void |
pd_series_base.h:114 |
Statistics#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
virtual size_t |
pd_series_base.h:159 |
I/O#
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
size_t |
pd_series_base.h:202 |
|
|
void |
pd_series_base.h:192 |
|
|
virtual std::unique_ptr<SeriesBase> |
pd_series_base.h:169 |
|
|
virtual std::string |
pd_series_base.h:74 |
|
|
virtual bool |
pd_series_base.h:69 |
|
|
bool |
pd_series_base.h:197 |
|
|
virtual bool |
pd_series_base.h:154 |
|
|
virtual const IndexBase& |
pd_series_base.h:108 |
|
|
virtual std::optional<std::string> |
pd_series_base.h:79 |
|
|
virtual size_t |
pd_series_base.h:99 |
|
|
virtual size_t |
pd_series_base.h:94 |
|
|
virtual std::string |
pd_series_base.h:183 |
|
|
virtual void |
pd_series_base.h:84 |
|
|
virtual std::vector<size_t> |
pd_series_base.h:89 |
|
|
virtual size_t |
pd_series_base.h:64 |
Code Examples#
The following examples are extracted from the test suite.
get_value_str (pd_test_1_all.cpp:4665)
4655 auto corr_df = df.corr();
4656
4657 // Check dimensions
4658 bool passed = corr_df.nrows() == 2 && corr_df.ncols() == 2;
4659 if (!passed) {
4660 std::cout << " [FAIL] : in pd_test_aggregation_dataframe_corr() : corr should be 2x2" << std::endl;
4661 throw std::runtime_error("pd_test_aggregation_dataframe_corr failed: corr should be 2x2");
4662 }
4663
4664 // Diagonal should be 1.0
4665 std::string aa = corr_df["A"].get_value_str(0);
4666 passed = std::abs(std::stod(aa) - 1.0) < 0.001;
4667 if (!passed) {
4668 std::cout << " [FAIL] : in pd_test_aggregation_dataframe_corr() : diagonal should be 1.0" << std::endl;
4669 throw std::runtime_error("pd_test_aggregation_dataframe_corr failed: diagonal should be 1.0");
4670 }
4671
4672 // A-B correlation should be 1.0 (perfect correlation)
4673 std::string ab = corr_df["B"].get_value_str(0);
4674 passed = std::abs(std::stod(ab) - 1.0) < 0.001;
4675 if (!passed) {
reset_index (pd_test_3_all.cpp:1618)
1608 }
1609
1610 std::cout << " -> tests passed" << std::endl;
1611}
1612
1613// ============================================================================
1614// Category 10: Remaining Untested Functions
1615// ============================================================================
1616
1617void pd_test_3_all_series_reset_index() {
1618 std::cout << "========= Series.reset_index() =======================";
1619
1620 std::vector<double> vals = {10.0, 20.0, 30.0};
1621 pandas::Series<double> s(vals, "test");
1622
1623 // Set a custom index
1624 pandas::Index<std::string> custom_idx({"a", "b", "c"});
1625 s.set_index(custom_idx);
1626
1627 // Reset the index
1628 s.reset_index(true); // drop=true
set_index (pd_test_1_all.cpp:20318)
20308 // Set datetime index
20309 std::vector<std::string> dates = {
20310 "2020-01-01 00:00:00",
20311 "2020-01-01 12:00:00",
20312 "2020-01-02 00:00:00",
20313 "2020-01-02 12:00:00",
20314 "2020-01-03 00:00:00",
20315 "2020-01-03 12:00:00"
20316 };
20317 df.set_index(std::make_unique<pandas::Index<std::string>>(dates));
20318
20319 // Resample to daily
20320 auto resampler = df.resample("D");
20321 pandas::DataFrame result = resampler.sum();
20322
20323 // Check that we got aggregated results
20324 bool passed = (result.nrows() <= df.nrows());
20325
20326 if (!passed) {
20327 std::cout << " [FAIL] : in pd_test_timeseries_resample_basic() : resample didn't reduce rows" << std::endl;
count (pd_test_1_all.cpp:66)
56 if (arr.is_na(0)) {
57 std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(0) should be false" << std::endl;
58 throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(0) should be false");
59 }
60
61 if (!arr.has_na()) {
62 std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : has_na() should be true" << std::endl;
63 throw std::runtime_error("pd_test_boolean_array_na_handling failed: has_na() should be true");
64 }
65
66 if (arr.count() != 2) {
67 std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : count() should be 2" << std::endl;
68 throw std::runtime_error("pd_test_boolean_array_na_handling failed: count() should be 2");
69 }
70
71 std::cout << " -> tests passed" << std::endl;
72 }
73
74 void pd_test_boolean_array_kleene_and() {
75 std::cout << "========= BooleanArray: Kleene AND ======================= ";
to_string (pd_test_1_all.cpp:2693)
2683 pandas::PeriodArray arr_m(std::vector<std::string>{
2684 "2020-01",
2685 "NaT",
2686 "2025-06"
2687 }, "M");
2688
2689 // Year
2690 auto years = arr_m.year();
2691 auto y0 = years[0];
2692 if (!y0.has_value() || y0.value() != 2020) {
2693 std::cout << " [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl;
2694 throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]");
2695 }
2696
2697 auto y1 = years[1];
2698 if (y1.has_value()) {
2699 std::cout << " [FAIL] : year[1] should be NA (NaT)" << std::endl;
2700 throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA");
2701 }
2702
2703 auto y2 = years[2];
to_string_vector (pd_test_1_all.cpp:10871)
10861 std::cout << " -> tests passed" << std::endl;
10862}
10863
10864void pd_test_extension_index_to_string_vector() {
10865 std::cout << "========= to_string_vector =========================";
10866
10867 pandas::CategoricalArray arr({"a", std::nullopt, "c"});
10868 pandas::CategoricalIndex idx(arr);
10869
10870 auto str_vec = idx.to_string_vector();
10871
10872 bool passed = (str_vec.size() == 3 &&
10873 str_vec[0] == "a" && str_vec[1] == "NA" && str_vec[2] == "c");
10874 if (!passed) {
10875 std::cout << " [FAIL] : in pd_test_extension_index_to_string_vector() : to_string_vector check failed" << std::endl;
10876 throw std::runtime_error("pd_test_extension_index_to_string_vector failed");
10877 }
10878
10879 std::cout << " -> tests passed" << std::endl;
10880}
clear_cache (pd_test_1_all.cpp:19413)
19403 s.mean();
19404 s.min();
19405 s.max();
19406
19407 passed = s.has_cached_values() == true;
19408 if (!passed) {
19409 std::cout << " [FAIL] : in pd_test_series_cache() : cache not populated" << std::endl;
19410 throw std::runtime_error("pd_test_series_cache failed: cache not populated");
19411 }
19412
19413 s.clear_cache();
19414 passed = s.has_cached_values() == false;
19415 if (!passed) {
19416 std::cout << " [FAIL] : in pd_test_series_cache() : cache not cleared" << std::endl;
19417 throw std::runtime_error("pd_test_series_cache failed: cache not cleared");
19418 }
19419
19420 std::cout << " -> tests passed" << std::endl;
19421 }
19422
19423 void pd_test_series_string_repr() {
clone (pd_test_1_all.cpp:5776)
5766 std::cout << " -> tests passed" << std::endl;
5767}
5768
5769void pd_test_categorical_index_clone() {
5770 std::cout << "========= clone =======================================";
5771
5772 pandas::CategoricalArray arr({"p", "q", "r"});
5773 pandas::CategoricalIndex idx(arr, "original");
5774
5775 std::unique_ptr<pandas::IndexBase> cloned = idx.clone();
5776
5777 bool passed = (cloned != nullptr && cloned->size() == idx.size() &&
5778 cloned->name() == idx.name());
5779 if (!passed) {
5780 std::cout << " [FAIL] : in pd_test_categorical_index_clone()" << std::endl;
5781 throw std::runtime_error("pd_test_categorical_index_clone failed");
5782 }
5783
5784 std::cout << " -> tests passed" << std::endl;
5785}
dtype_name (pd_test_1_all.cpp:10104)
10094}
10095
10096void pd_test_extension_index_array_constructor() {
10097 std::cout << "========= array constructor =========================";
10098
10099 pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"});
10100 pandas::CategoricalIndex idx(arr, "fruits");
10101
10102 bool passed = (idx.size() == 4 && !idx.empty() &&
10103 idx.name().has_value() && *idx.name() == "fruits" &&
10104 idx.dtype_name() == "category");
10105 if (!passed) {
10106 std::cout << " [FAIL] : in pd_test_extension_index_array_constructor() : array constructor check failed" << std::endl;
10107 throw std::runtime_error("pd_test_extension_index_array_constructor failed");
10108 }
10109
10110 std::cout << " -> tests passed" << std::endl;
10111}
10112
10113void pd_test_extension_index_copy_constructor() {
10114 std::cout << "========= copy constructor =========================";
empty (pd_test_1_all.cpp:941)
931#include "../pandas/pd_config.h"
932
933namespace dataframe_tests {
934
935namespace dataframe_tests_config {
936
937 void pd_test_config_version() {
938 std::cout << "========= df_config: version info ======================= ";
939 const char* version = pandas::DataFrameInfo::version();
940 if (version == nullptr || std::string(version).empty()) {
941 std::cout << "[FAIL] : in pd_test_config_version() : version is null or empty" << std::endl;
942 throw std::runtime_error("pd_test_config_version failed: version is null or empty");
943 }
944 std::cout << "-> tests passed" << std::endl;
945 }
946
947 void pd_test_config_na_repr() {
948 std::cout << "========= df_config: NA representation ======================= ";
949 const char* na_repr = pandas::DataFrameConfig::get_na_repr();
950 if (na_repr == nullptr) {
has_cached_values (pd_test_1_all.cpp:19395)
19385 }
19386
19387 std::cout << " -> tests passed" << std::endl;
19388 }
19389
19390 void pd_test_series_cache() {
19391 std::cout << "========= cache management =========================================";
19392
19393 pandas::Series<double> s({1.0, 2.0, 3.0, 4.0, 5.0});
19394
19395 bool passed = s.has_cached_values() == false;
19396 if (!passed) {
19397 std::cout << " [FAIL] : in pd_test_series_cache() : initial cache not empty" << std::endl;
19398 throw std::runtime_error("pd_test_series_cache failed: initial cache not empty");
19399 }
19400
19401 // Trigger cache
19402 s.sum();
19403 s.mean();
19404 s.min();
19405 s.max();
hasnans (pd_test_1_all.cpp:5363)
5353void pd_test_categorical_index_from_codes() {
5354 std::cout << "========= from_codes =================================";
5355
5356 std::vector<numpy::int32> codes = {0, 1, 0, 2, -1}; // -1 = NA
5357 std::vector<std::string> categories = {"low", "medium", "high"};
5358
5359 pandas::CategoricalIndex idx = pandas::CategoricalIndex::from_codes(codes, categories, true, "level");
5360
5361 bool passed = (idx.size() == 5 && idx.num_categories() == 3 &&
5362 idx.ordered() && idx.name().has_value() && *idx.name() == "level" &&
5363 idx.hasnans()); // has NA from code -1
5364 if (!passed) {
5365 std::cout << " [FAIL] : in pd_test_categorical_index_from_codes()" << std::endl;
5366 throw std::runtime_error("pd_test_categorical_index_from_codes failed");
5367 }
5368
5369 std::cout << " -> tests passed" << std::endl;
5370}
5371
5372void pd_test_categorical_index_simple_new() {
5373 std::cout << "========= _simple_new =================================";
index (pd_test_1_all.cpp:6680)
6670 void pd_test_dataframe_index_ops() {
6671 std::cout << "========= index operations =================";
6672
6673 // Test set_axis (rows)
6674 {
6675 std::map<std::string, std::vector<int>> data;
6676 data["A"] = {1, 2, 3};
6677 pandas::DataFrame df(data);
6678
6679 auto renamed = df.set_axis({"x", "y", "z"}, 0);
6680 std::string idx0 = renamed.index().get_value_str(0);
6681 if (idx0 != "x") {
6682 std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : set_axis first label should be 'x'" << std::endl;
6683 throw std::runtime_error("pd_test_dataframe_index_ops failed: set_axis");
6684 }
6685 }
6686
6687 // Test set_axis (columns)
6688 {
6689 std::map<std::string, std::vector<int>> data;
6690 data["A"] = {1, 2};
name (pd_test_1_all.cpp:295)
285 throw std::runtime_error("pd_test_boolean_array_reductions failed: mean");
286 }
287
288 std::cout << " -> tests passed" << std::endl;
289 }
290
291 void pd_test_boolean_array_dtype() {
292 std::cout << "========= BooleanArray: dtype ======================= ";
293
294 pandas::BooleanArray arr;
295 if (arr.dtype().name() != "boolean") {
296 std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl;
297 throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name");
298 }
299
300 if (arr.dtype().kind() != "b") {
301 std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl;
302 throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind");
303 }
304
305 std::cout << " -> tests passed" << std::endl;
nbytes (pd_test_1_all.cpp:6214)
6204 }
6205
6206 // Test empty DataFrame
6207 pandas::DataFrame empty_df;
6208 if (!empty_df.empty()) {
6209 std::cout << " [FAIL] : in pd_test_dataframe_properties() : should be empty" << std::endl;
6210 throw std::runtime_error("pd_test_dataframe_properties failed: should be empty");
6211 }
6212
6213 // Test nbytes > 0 for non-empty
6214 if (df.nbytes() == 0) {
6215 std::cout << " [FAIL] : in pd_test_dataframe_properties() : nbytes should be > 0" << std::endl;
6216 throw std::runtime_error("pd_test_dataframe_properties failed: nbytes should be > 0");
6217 }
6218
6219 // Test columns index
6220 if (df.columns().size() != 3) {
6221 std::cout << " [FAIL] : in pd_test_dataframe_properties() : columns size != 3" << std::endl;
6222 throw std::runtime_error("pd_test_dataframe_properties failed: columns size != 3");
6223 }
ndim (pd_test_1_all.cpp:6195)
6185 pandas::DataFrame df(data);
6186
6187 // Test shape
6188 auto shape = df.shape();
6189 if (shape.size() != 2 || shape[0] != 4 || shape[1] != 3) {
6190 std::cout << " [FAIL] : in pd_test_dataframe_properties() : shape mismatch" << std::endl;
6191 throw std::runtime_error("pd_test_dataframe_properties failed: shape mismatch");
6192 }
6193
6194 // Test ndim
6195 if (df.ndim() != 2) {
6196 std::cout << " [FAIL] : in pd_test_dataframe_properties() : ndim != 2" << std::endl;
6197 throw std::runtime_error("pd_test_dataframe_properties failed: ndim != 2");
6198 }
6199
6200 // Test empty
6201 if (df.empty()) {
6202 std::cout << " [FAIL] : in pd_test_dataframe_properties() : should not be empty" << std::endl;
6203 throw std::runtime_error("pd_test_dataframe_properties failed: should not be empty");
6204 }
repr (pd_test_1_all.cpp:10906)
10896 std::cout << " -> tests passed" << std::endl;
10897}
10898
10899void pd_test_extension_index_repr() {
10900 std::cout << "========= repr =========================";
10901
10902 pandas::CategoricalArray arr({"a", "b", "c"});
10903 // Use ExtensionIndex<CategoricalArray> directly to test base class repr
10904 pandas::ExtensionIndex<pandas::CategoricalArray> idx(arr, "test");
10905
10906 std::string repr_str = idx.repr();
10907
10908 bool passed = (!repr_str.empty() && repr_str.find("ExtensionIndex") != std::string::npos);
10909 if (!passed) {
10910 std::cout << " [FAIL] : in pd_test_extension_index_repr() : repr check failed" << std::endl;
10911 throw std::runtime_error("pd_test_extension_index_repr failed");
10912 }
10913
10914 std::cout << " -> tests passed" << std::endl;
10915}
set_name (pd_test_1_all.cpp:11798)
11788 throw std::runtime_error("pd_test_index_vector_constructor failed");
11789 }
11790
11791 std::cout << " -> tests passed" << std::endl;
11792 }
11793
11794 void pd_test_index_copy_constructor() {
11795 std::cout << "========= copy constructor ============================";
11796
11797 pandas::Index<numpy::int64> idx1{1, 2, 3};
11798 idx1.set_name("original");
11799
11800 pandas::Index<numpy::int64> idx2(idx1);
11801
11802 bool passed = (idx2.size() == 3);
11803 passed = passed && (idx2.name().value() == "original");
11804 passed = passed && idx2.equals(idx1);
11805
11806 if (!passed) {
11807 std::cout << " [FAIL] : in pd_test_index_copy_constructor() : copy failed" << std::endl;
11808 throw std::runtime_error("pd_test_index_copy_constructor failed");
shape (pd_test_1_all.cpp:6188)
6178 std::cout << "========= properties =======================";
6179
6180 std::map<std::string, std::vector<numpy::float64>> data;
6181 data["A"] = {1.0, 2.0, 3.0, 4.0};
6182 data["B"] = {5.0, 6.0, 7.0, 8.0};
6183 data["C"] = {9.0, 10.0, 11.0, 12.0};
6184
6185 pandas::DataFrame df(data);
6186
6187 // Test shape
6188 auto shape = df.shape();
6189 if (shape.size() != 2 || shape[0] != 4 || shape[1] != 3) {
6190 std::cout << " [FAIL] : in pd_test_dataframe_properties() : shape mismatch" << std::endl;
6191 throw std::runtime_error("pd_test_dataframe_properties failed: shape mismatch");
6192 }
6193
6194 // Test ndim
6195 if (df.ndim() != 2) {
6196 std::cout << " [FAIL] : in pd_test_dataframe_properties() : ndim != 2" << std::endl;
6197 throw std::runtime_error("pd_test_dataframe_properties failed: ndim != 2");
6198 }
size (pd_test_1_all.cpp:22)
12#include "../pandas/pd_boolean_array.h"
13
14namespace dataframe_tests {
15
16namespace dataframe_tests_boolean_array {
17 void pd_test_boolean_array_constructors() {
18 std::cout << "========= BooleanArray: constructors ======================= ";
19
20 // Default constructor
21 pandas::BooleanArray arr1;
22 if (arr1.size() != 0) {
23 std::cout << " [FAIL] : in pd_test_boolean_array_constructors() : default constructor size != 0" << std::endl;
24 throw std::runtime_error("pd_test_boolean_array_constructors failed: default constructor size != 0");
25 }
26
27 // Initializer list constructor
28 pandas::BooleanArray arr2({
29 std::optional<bool>(true),
30 std::optional<bool>(false),
31 std::nullopt,
32 std::optional<bool>(true)