SeriesBase ========== .. cpp:class:: pandas::SeriesBase Core data container class in the pandas namespace. Example ------- .. code-block:: cpp #include using namespace pandas; // Use SeriesBase SeriesBase obj; // ... operations ... Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual std::string get_value_str(size_t idx) const = 0`` - virtual std::string - pd_series_base.h:140 - :ref:`View ` Data Manipulation ----------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual void reset_index(bool drop = false, std::optional level = std::nullopt, bool inplace = false, std::optional col_level = std::nullopt, const std::string& col_fill = "", bool allow_duplicates = false, const std::optional>& names = std::nullopt) = 0`` - virtual void - pd_series_base.h:126 - :ref:`View ` * - ``virtual void set_index(std::unique_ptr new_index) = 0`` - virtual void - pd_series_base.h:114 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual size_t count() const = 0`` - virtual size_t - pd_series_base.h:159 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual std::string to_string() const = 0`` - virtual std::string - pd_series_base.h:178 - :ref:`View ` * - ``virtual std::vector to_string_vector() const = 0`` - virtual std::vector - pd_series_base.h:145 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t cache_memory_usage() const override`` - size_t - pd_series_base.h:202 - * - ``void clear_cache() const override = 0`` - void - pd_series_base.h:192 - :ref:`View ` * - ``virtual std::unique_ptr clone() const = 0`` - virtual std::unique_ptr - pd_series_base.h:169 - :ref:`View ` * - ``virtual std::string dtype_name() const = 0`` - virtual std::string - pd_series_base.h:74 - :ref:`View ` * - ``virtual bool empty() const = 0`` - virtual bool - pd_series_base.h:69 - :ref:`View ` * - ``bool has_cached_values() const override = 0`` - bool - pd_series_base.h:197 - :ref:`View ` * - ``virtual bool hasnans() const = 0`` - virtual bool - pd_series_base.h:154 - :ref:`View ` * - ``virtual const IndexBase& index() const = 0`` - virtual const IndexBase& - pd_series_base.h:108 - :ref:`View ` * - ``virtual std::optional name() const = 0`` - virtual std::optional - pd_series_base.h:79 - :ref:`View ` * - ``virtual size_t nbytes() const = 0`` - virtual size_t - pd_series_base.h:99 - :ref:`View ` * - ``virtual size_t ndim() const`` - virtual size_t - pd_series_base.h:94 - :ref:`View ` * - ``virtual std::string repr() const = 0`` - virtual std::string - pd_series_base.h:183 - :ref:`View ` * - ``virtual void set_name(const std::optional& name) = 0`` - virtual void - pd_series_base.h:84 - :ref:`View ` * - ``virtual std::vector shape() const = 0`` - virtual std::vector - pd_series_base.h:89 - :ref:`View ` * - ``virtual size_t size() const = 0`` - virtual size_t - pd_series_base.h:64 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-seriesbase-get_value_str-0: .. dropdown:: get_value_str (pd_test_1_all.cpp:4665) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4655 :emphasize-lines: 11 auto corr_df = df.corr(); // Check dimensions bool passed = corr_df.nrows() == 2 && corr_df.ncols() == 2; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_dataframe_corr() : corr should be 2x2" << std::endl; throw std::runtime_error("pd_test_aggregation_dataframe_corr failed: corr should be 2x2"); } // Diagonal should be 1.0 std::string aa = corr_df["A"].get_value_str(0); passed = std::abs(std::stod(aa) - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_dataframe_corr() : diagonal should be 1.0" << std::endl; throw std::runtime_error("pd_test_aggregation_dataframe_corr failed: diagonal should be 1.0"); } // A-B correlation should be 1.0 (perfect correlation) std::string ab = corr_df["B"].get_value_str(0); passed = std::abs(std::stod(ab) - 1.0) < 0.001; if (!passed) { .. _example-seriesbase-reset_index-1: .. dropdown:: reset_index (pd_test_3_all.cpp:1618) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1608 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 10: Remaining Untested Functions // ============================================================================ void pd_test_3_all_series_reset_index() { std::cout << "========= Series.reset_index() ======================="; std::vector vals = {10.0, 20.0, 30.0}; pandas::Series s(vals, "test"); // Set a custom index pandas::Index custom_idx({"a", "b", "c"}); s.set_index(custom_idx); // Reset the index s.reset_index(true); // drop=true .. _example-seriesbase-set_index-2: .. dropdown:: set_index (pd_test_1_all.cpp:20318) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20308 :emphasize-lines: 11 // Set datetime index std::vector dates = { "2020-01-01 00:00:00", "2020-01-01 12:00:00", "2020-01-02 00:00:00", "2020-01-02 12:00:00", "2020-01-03 00:00:00", "2020-01-03 12:00:00" }; df.set_index(std::make_unique>(dates)); // Resample to daily auto resampler = df.resample("D"); pandas::DataFrame result = resampler.sum(); // Check that we got aggregated results bool passed = (result.nrows() <= df.nrows()); if (!passed) { std::cout << " [FAIL] : in pd_test_timeseries_resample_basic() : resample didn't reduce rows" << std::endl; .. _example-seriesbase-count-3: .. dropdown:: count (pd_test_1_all.cpp:66) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 56 :emphasize-lines: 11 if (arr.is_na(0)) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : is_na(0) should be false" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: is_na(0) should be false"); } if (!arr.has_na()) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : has_na() should be true" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: has_na() should be true"); } if (arr.count() != 2) { std::cout << " [FAIL] : in pd_test_boolean_array_na_handling() : count() should be 2" << std::endl; throw std::runtime_error("pd_test_boolean_array_na_handling failed: count() should be 2"); } std::cout << " -> tests passed" << std::endl; } void pd_test_boolean_array_kleene_and() { std::cout << "========= BooleanArray: Kleene AND ======================= "; .. _example-seriesbase-to_string-4: .. dropdown:: to_string (pd_test_1_all.cpp:2693) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2683 :emphasize-lines: 11 pandas::PeriodArray arr_m(std::vector{ "2020-01", "NaT", "2025-06" }, "M"); // Year auto years = arr_m.year(); auto y0 = years[0]; if (!y0.has_value() || y0.value() != 2020) { std::cout << " [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]"); } auto y1 = years[1]; if (y1.has_value()) { std::cout << " [FAIL] : year[1] should be NA (NaT)" << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA"); } auto y2 = years[2]; .. _example-seriesbase-to_string_vector-5: .. dropdown:: to_string_vector (pd_test_1_all.cpp:10871) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10861 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_to_string_vector() { std::cout << "========= to_string_vector ========================="; pandas::CategoricalArray arr({"a", std::nullopt, "c"}); pandas::CategoricalIndex idx(arr); auto str_vec = idx.to_string_vector(); bool passed = (str_vec.size() == 3 && str_vec[0] == "a" && str_vec[1] == "NA" && str_vec[2] == "c"); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_to_string_vector() : to_string_vector check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_to_string_vector failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-seriesbase-clear_cache-6: .. dropdown:: clear_cache (pd_test_1_all.cpp:19413) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19403 :emphasize-lines: 11 s.mean(); s.min(); s.max(); passed = s.has_cached_values() == true; if (!passed) { std::cout << " [FAIL] : in pd_test_series_cache() : cache not populated" << std::endl; throw std::runtime_error("pd_test_series_cache failed: cache not populated"); } s.clear_cache(); passed = s.has_cached_values() == false; if (!passed) { std::cout << " [FAIL] : in pd_test_series_cache() : cache not cleared" << std::endl; throw std::runtime_error("pd_test_series_cache failed: cache not cleared"); } std::cout << " -> tests passed" << std::endl; } void pd_test_series_string_repr() { .. _example-seriesbase-clone-7: .. dropdown:: clone (pd_test_1_all.cpp:5776) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5766 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_clone() { std::cout << "========= clone ======================================="; pandas::CategoricalArray arr({"p", "q", "r"}); pandas::CategoricalIndex idx(arr, "original"); std::unique_ptr cloned = idx.clone(); bool passed = (cloned != nullptr && cloned->size() == idx.size() && cloned->name() == idx.name()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_clone()" << std::endl; throw std::runtime_error("pd_test_categorical_index_clone failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-seriesbase-dtype_name-8: .. dropdown:: dtype_name (pd_test_1_all.cpp:10104) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10094 :emphasize-lines: 11 } void pd_test_extension_index_array_constructor() { std::cout << "========= array constructor ========================="; pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"}); pandas::CategoricalIndex idx(arr, "fruits"); bool passed = (idx.size() == 4 && !idx.empty() && idx.name().has_value() && *idx.name() == "fruits" && idx.dtype_name() == "category"); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_array_constructor() : array constructor check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_array_constructor failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_copy_constructor() { std::cout << "========= copy constructor ========================="; .. _example-seriesbase-empty-9: .. dropdown:: empty (pd_test_1_all.cpp:941) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 931 :emphasize-lines: 11 #include "../pandas/pd_config.h" namespace dataframe_tests { namespace dataframe_tests_config { void pd_test_config_version() { std::cout << "========= df_config: version info ======================= "; const char* version = pandas::DataFrameInfo::version(); if (version == nullptr || std::string(version).empty()) { std::cout << "[FAIL] : in pd_test_config_version() : version is null or empty" << std::endl; throw std::runtime_error("pd_test_config_version failed: version is null or empty"); } std::cout << "-> tests passed" << std::endl; } void pd_test_config_na_repr() { std::cout << "========= df_config: NA representation ======================= "; const char* na_repr = pandas::DataFrameConfig::get_na_repr(); if (na_repr == nullptr) { .. _example-seriesbase-has_cached_values-10: .. dropdown:: has_cached_values (pd_test_1_all.cpp:19395) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19385 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void pd_test_series_cache() { std::cout << "========= cache management ========================================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); bool passed = s.has_cached_values() == false; if (!passed) { std::cout << " [FAIL] : in pd_test_series_cache() : initial cache not empty" << std::endl; throw std::runtime_error("pd_test_series_cache failed: initial cache not empty"); } // Trigger cache s.sum(); s.mean(); s.min(); s.max(); .. _example-seriesbase-hasnans-11: .. dropdown:: hasnans (pd_test_1_all.cpp:5363) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5353 :emphasize-lines: 11 void pd_test_categorical_index_from_codes() { std::cout << "========= from_codes ================================="; std::vector codes = {0, 1, 0, 2, -1}; // -1 = NA std::vector categories = {"low", "medium", "high"}; pandas::CategoricalIndex idx = pandas::CategoricalIndex::from_codes(codes, categories, true, "level"); bool passed = (idx.size() == 5 && idx.num_categories() == 3 && idx.ordered() && idx.name().has_value() && *idx.name() == "level" && idx.hasnans()); // has NA from code -1 if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_from_codes()" << std::endl; throw std::runtime_error("pd_test_categorical_index_from_codes failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_simple_new() { std::cout << "========= _simple_new ================================="; .. _example-seriesbase-index-12: .. dropdown:: index (pd_test_1_all.cpp:6680) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6670 :emphasize-lines: 11 void pd_test_dataframe_index_ops() { std::cout << "========= index operations ================="; // Test set_axis (rows) { std::map> data; data["A"] = {1, 2, 3}; pandas::DataFrame df(data); auto renamed = df.set_axis({"x", "y", "z"}, 0); std::string idx0 = renamed.index().get_value_str(0); if (idx0 != "x") { std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : set_axis first label should be 'x'" << std::endl; throw std::runtime_error("pd_test_dataframe_index_ops failed: set_axis"); } } // Test set_axis (columns) { std::map> data; data["A"] = {1, 2}; .. _example-seriesbase-name-13: .. dropdown:: name (pd_test_1_all.cpp:295) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 285 :emphasize-lines: 11 throw std::runtime_error("pd_test_boolean_array_reductions failed: mean"); } std::cout << " -> tests passed" << std::endl; } void pd_test_boolean_array_dtype() { std::cout << "========= BooleanArray: dtype ======================= "; pandas::BooleanArray arr; if (arr.dtype().name() != "boolean") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name"); } if (arr.dtype().kind() != "b") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind"); } std::cout << " -> tests passed" << std::endl; .. _example-seriesbase-nbytes-14: .. dropdown:: nbytes (pd_test_1_all.cpp:6214) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6204 :emphasize-lines: 11 } // Test empty DataFrame pandas::DataFrame empty_df; if (!empty_df.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : should be empty" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: should be empty"); } // Test nbytes > 0 for non-empty if (df.nbytes() == 0) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : nbytes should be > 0" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: nbytes should be > 0"); } // Test columns index if (df.columns().size() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : columns size != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: columns size != 3"); } .. _example-seriesbase-ndim-15: .. dropdown:: ndim (pd_test_1_all.cpp:6195) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6185 :emphasize-lines: 11 pandas::DataFrame df(data); // Test shape auto shape = df.shape(); if (shape.size() != 2 || shape[0] != 4 || shape[1] != 3) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : shape mismatch" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: shape mismatch"); } // Test ndim if (df.ndim() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : ndim != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: ndim != 2"); } // Test empty if (df.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : should not be empty" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: should not be empty"); } .. _example-seriesbase-repr-16: .. dropdown:: repr (pd_test_1_all.cpp:10906) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10896 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_repr() { std::cout << "========= repr ========================="; pandas::CategoricalArray arr({"a", "b", "c"}); // Use ExtensionIndex directly to test base class repr pandas::ExtensionIndex idx(arr, "test"); std::string repr_str = idx.repr(); bool passed = (!repr_str.empty() && repr_str.find("ExtensionIndex") != std::string::npos); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_repr() : repr check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_repr failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-seriesbase-set_name-17: .. dropdown:: set_name (pd_test_1_all.cpp:11798) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11788 :emphasize-lines: 11 throw std::runtime_error("pd_test_index_vector_constructor failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_index_copy_constructor() { std::cout << "========= copy constructor ============================"; pandas::Index idx1{1, 2, 3}; idx1.set_name("original"); pandas::Index idx2(idx1); bool passed = (idx2.size() == 3); passed = passed && (idx2.name().value() == "original"); passed = passed && idx2.equals(idx1); if (!passed) { std::cout << " [FAIL] : in pd_test_index_copy_constructor() : copy failed" << std::endl; throw std::runtime_error("pd_test_index_copy_constructor failed"); .. _example-seriesbase-shape-18: .. dropdown:: shape (pd_test_1_all.cpp:6188) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6178 :emphasize-lines: 11 std::cout << "========= properties ======================="; std::map> data; data["A"] = {1.0, 2.0, 3.0, 4.0}; data["B"] = {5.0, 6.0, 7.0, 8.0}; data["C"] = {9.0, 10.0, 11.0, 12.0}; pandas::DataFrame df(data); // Test shape auto shape = df.shape(); if (shape.size() != 2 || shape[0] != 4 || shape[1] != 3) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : shape mismatch" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: shape mismatch"); } // Test ndim if (df.ndim() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_properties() : ndim != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_properties failed: ndim != 2"); } .. _example-seriesbase-size-19: .. 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)