Series ====== .. cpp:class:: pandas::Series Core data container class in the pandas namespace. Example ------- .. code-block:: cpp #include using namespace pandas; // Create Series Series s({1.0, 2.0, 3.0}, "values"); // Statistics double mean_val = s.mean(); double std_val = s.std(); Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit Series(const numpy::NDArray& data, const std::optional& name = std::nullopt, const std::string& dtype = "", bool copy = false)`` - pd_series.h:802 - :ref:`View ` * - ``Series(const std::vector& data, const std::optional& name = std::nullopt, const std::string& dtype = "", bool copy = false)`` - pd_series.h:828 - :ref:`View ` * - ``Series(const numpy::NDArray& data, const Index& idx, const std::optional& name = std::nullopt)`` - pd_series.h:883 - :ref:`View ` * - ``Series(const numpy::NDArray& data, const RangeIndex& idx, const std::optional& name = std::nullopt)`` - pd_series.h:902 - :ref:`View ` * - ``Series(const std::vector& data, const Index& idx, const std::optional& name = std::nullopt)`` - pd_series.h:919 - :ref:`View ` * - ``Series(const Series& other)`` - pd_series.h:938 - :ref:`View ` * - ``Series(Series&& other) noexcept`` - pd_series.h:970 - :ref:`View ` Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::unique_ptr create_nan_filled(size_t n) const override`` - std::unique_ptr - pd_series.h:11189 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T at(size_t idx) const`` - T - pd_series.h:1550 - :ref:`View ` * - ``Series at_time( const std::string& time, bool asof = false, int axis = 0 ) const`` - Series - pd_series.h:15902 - :ref:`View ` * - ``Series first(const std::string& offset) const`` - Series - pd_series.h:12867 - :ref:`View ` * - ``std::optional first_valid_index() const`` - std::optional - pd_series.h:6786 - :ref:`View ` * - ``T get(const std::string& key, const std::optional& default_value = std::nullopt) const`` - T - pd_series.h:12896 - :ref:`View ` * - ``LabelLookup get_by_label_duplicates(const std::string& key) const`` - LabelLookup - pd_series.h:2198 - :ref:`View ` * - ``const std::vector& get_cat_categories() const override`` - const std::vector& - pd_series.h:1248 - :ref:`View ` * - ``std::string get_cat_categories_dtype() const override`` - std::string - pd_series.h:1269 - * - ``numpy::datetime64 get_datetime(size_t i) const`` - numpy::datetime64 - pd_series.h:10454 - * - ``std::optional get_freq() const`` - std::optional - pd_series.h:10544 - :ref:`View ` * - ``std::optional get_optional(size_t i) const`` - std::optional - pd_series.h:1300 - :ref:`View ` * - ``Series get_slice_as_series(size_t start, size_t stop) const`` - Series - pd_series.h:2238 - :ref:`View ` * - ``std::optional get_sparse_fill_value() const`` - std::optional - pd_series.h:10535 - * - ``std::string get_string(size_t i) const`` - std::string - pd_series.h:10420 - :ref:`View ` * - ``bool get_value_bool(size_t idx) const override`` - bool - pd_series.h:2947 - :ref:`View ` * - ``double get_value_double(size_t idx) const override`` - double - pd_series.h:10290 - :ref:`View ` * - ``std::string get_value_str(size_t idx) const override`` - std::string - pd_series.h:9264 - :ref:`View ` * - ``<< get_value_str(i)`` - << - pd_series.h:10946 - :ref:`View ` * - ``Series head(size_t n = 5) const`` - Series - pd_series.h:2494 - :ref:`View ` * - ``T iat(size_t idx) const`` - T - pd_series.h:1562 - :ref:`View ` * - ``T iat_resolved(std::int64_t idx) const`` - T - pd_series.h:2035 - :ref:`View ` * - ``std::string idxmax() const`` - std::string - pd_series.h:3913 - :ref:`View ` * - ``std::pair> idxmax_typed() const`` - std::pair> - pd_series.h:3921 - :ref:`View ` * - ``std::string idxmin() const`` - std::string - pd_series.h:3906 - :ref:`View ` * - ``std::pair> idxmin_typed() const`` - std::pair> - pd_series.h:3934 - :ref:`View ` * - ``Series iloc(size_t start, size_t stop, size_t step = 1) const`` - Series - pd_series.h:2052 - :ref:`View ` * - ``Series iloc(const std::vector& indices) const`` - Series - pd_series.h:2115 - :ref:`View ` * - ``Series last(const std::string& offset) const`` - Series - pd_series.h:12882 - :ref:`View ` * - ``std::optional last_valid_index() const`` - std::optional - pd_series.h:6806 - :ref:`View ` * - ``T loc(const std::string& label) const`` - T - pd_series.h:2269 - :ref:`View ` * - ``Series loc(const std::vector& labels) const`` - Series - pd_series.h:2282 - :ref:`View ` * - ``Series loc_slice(const pandas::SliceSpec& spec) const`` - Series - pd_series.h:1908 - :ref:`View ` * - ``pandas::StringLookupResult loc_string(const std::string& key) const`` - pandas::StringLookupResult - pd_series.h:1602 - :ref:`View ` * - ``pandas::LookupResult loc_timedelta(const pandas::Timedelta& key) const`` - pandas::LookupResult - pd_series.h:1786 - :ref:`View ` * - ``pandas::LookupResult loc_timestamp(const pandas::Timestamp& key) const`` - pandas::LookupResult - pd_series.h:1749 - :ref:`View ` * - ``const numpy::NDArray& mask() const`` - const numpy::NDArray& - pd_series.h:1293 - :ref:`View ` * - ``Series mask(const numpy::NDArray& cond, const T& other, int axis = 0, bool inplace = false, std::optional level = std::nullopt) const`` - Series - pd_series.h:2458 - :ref:`View ` * - ``bool mask_at(size_t i) const override`` - bool - pd_series.h:1295 - :ref:`View ` * - ``Series nlargest(size_t n = 5, const std::string& keep = "first") const`` - Series - pd_series.h:13644 - :ref:`View ` * - ``Series nsmallest(size_t n = 5, const std::string& keep = "first") const`` - Series - pd_series.h:13700 - :ref:`View ` * - ``Series sample( std::optional n = std::nullopt, std::optional frac = std::nullopt, bool replace = false, const std::vector& weights = {}, std::optional random_state = std::nullopt, int axis = 0, bool ignore_index = false) const`` - Series - pd_series.h:15536 - :ref:`View ` * - ``void set_value_double(size_t idx, double value) override`` - void - pd_series.h:2997 - * - ``void set_value_nan(size_t idx) override`` - void - pd_series.h:2971 - :ref:`View ` * - ``void set_value_str(size_t idx, const std::string& value) override`` - void - pd_series.h:3011 - * - ``bool set_value_with_enlarge(const std::string& label, T value)`` - bool - pd_series.h:3041 - :ref:`View ` * - ``Series tail(size_t n = 5) const`` - Series - pd_series.h:2502 - :ref:`View ` * - ``Series take(const std::vector& indices, int axis = 0) const`` - Series - pd_series.h:2512 - :ref:`View ` * - ``std::unique_ptr take_indices(const std::vector& indices) const override`` - std::unique_ptr - pd_series.h:11546 - * - ``Series where(const numpy::NDArray& cond, const T& other, int axis = 0, bool inplace = false, std::optional level = std::nullopt) const`` - Series - pd_series.h:2375 - :ref:`View ` * - ``Series where(const numpy::NDArray& cond, const Series& other) const`` - Series - pd_series.h:2409 - :ref:`View ` * - ``Result where_resolved_typed( const pandas::Series& cond, const pandas::FillValue& other) const`` - Result - pd_series.h:16301 - :ref:`View ` * - ``Series where_t_or_default(const numpy::NDArray& cond, const pandas::FillValue& fv) const`` - Series - pd_series.h:16340 - * - ``Series xs(const std::string& key, int level = 0, int axis = 0, bool drop_level = true) const`` - Series - pd_series.h:15785 - :ref:`View ` Data Manipulation ----------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series drop(const std::vector& labels, int axis = 0, const std::optional>& index = std::nullopt, const std::optional>& columns = std::nullopt, std::optional level = std::nullopt, bool inplace = false, const std::string& errors = "raise") const`` - Series - pd_series.h:12445 - :ref:`View ` * - ``Series drop_duplicates(const std::string& keep = "first", bool inplace = false, bool ignore_index = false) const`` - Series - pd_series.h:5712 - :ref:`View ` * - ``Series droplevel(int level, int axis = 0) const`` - Series - pd_series.h:12485 - :ref:`View ` * - ``Series dropna() const`` - Series - pd_series.h:2754 - :ref:`View ` * - ``T pop(size_t pos)`` - T - pd_series.h:12744 - :ref:`View ` * - ``T pop(const std::string& label)`` - T - pd_series.h:12805 - :ref:`View ` * - ``Series reindex( const std::vector& index, const std::string& method = "", const std::optional& fill_value = std::nullopt, int axis = 0, bool copy = true, std::optional level = std::nullopt, std::optional limit = std::nullopt, std::optional tolerance = std::nullopt, const std::vector& new_numeric_index = {}, const std::vector& old_numeric_index = {}) const`` - Series - pd_series.h:13996 - :ref:`View ` * - ``Result reindex_dispatch( const std::vector& labels, const std::string& method, const FillValue& fill, const std::optional& tolerance = std::nullopt, std::optional limit = std::nullopt, const std::vector& new_numeric_index = {}, const std::vector& old_numeric_index = {} ) const`` - Result - pd_series.h:14808 - :ref:`View ` * - ``Series reindex_like( const Series& other, const std::string& method = "", const std::optional& fill_value = std::nullopt, bool copy = true, std::optional limit = std::nullopt, std::optional tolerance = std::nullopt) const`` - Series - pd_series.h:15389 - :ref:`View ` * - ``Series reindex_promote_object_bool_fill( const std::vector& index, const std::string& method, bool bool_fill_value) const`` - Series - pd_series.h:14818 - :ref:`View ` * - ``Series reindex_promote_object_numeric_fill( const std::vector& index, const std::string& method, double fill_num, bool fill_is_python_int = false) const`` - Series - pd_series.h:15097 - * - ``Series reindex_promote_object_string_fill( const std::vector& index, const std::string& method, const std::string& string_fill_value) const`` - Series - pd_series.h:15340 - * - ``Result reindex_with_fill( const std::vector& labels, const std::string& method, const pandas::FillValue& fill, const std::optional& tolerance = std::nullopt, std::optional limit = std::nullopt, const std::vector& new_numeric_idx = {}, const std::vector& old_numeric_idx = {}) const`` - Result - pd_series.h:16284 - :ref:`View ` * - ``std::unique_ptr reindex_with_indexer(const numpy::NDArray& indexer) const override`` - std::unique_ptr - pd_series.h:11555 - :ref:`View ` * - ``Series rename(const std::optional& new_name, const std::optional>& index = std::nullopt, int axis = 0, bool copy = true, bool inplace = false, std::optional level = std::nullopt, const std::string& errors = "ignore") const`` - Series - pd_series.h:8897 - :ref:`View ` * - ``Series rename_axis( const std::optional& mapper = std::nullopt, const std::optional& index = std::nullopt, int axis = 0, [[maybe_unused]] bool copy = true, [[maybe_unused]] bool inplace = false) const`` - Series - pd_series.h:9020 - :ref:`View ` * - ``Series reorder_levels(const std::vector& order) const`` - Series - pd_series.h:15413 - :ref:`View ` * - ``Series replace( const T& to_replace, const T& value, bool inplace = false, std::optional limit = std::nullopt, bool regex = false, const std::string& method = "") const`` - Series - pd_series.h:15460 - :ref:`View ` * - ``Series replace( const std::map& replacements, bool inplace = false, std::optional limit = std::nullopt, bool regex = false, const std::string& method = "") const`` - Series - pd_series.h:15498 - :ref:`View ` * - ``void replace_value(double to_replace, double value) override`` - void - pd_series.h:2922 - * - ``Series& 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)`` - Series& - pd_series.h:1439 - :ref:`View ` * - ``void reset_index_with_name(bool drop = false, const std::optional& name = std::nullopt, 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)`` - void - pd_series.h:1470 - * - ``Series set_axis(const std::vector& labels, int axis = 0, bool copy = true) const`` - Series - pd_series.h:8994 - :ref:`View ` * - ``void set_index(std::unique_ptr new_index) override`` - void - pd_series.h:1403 - :ref:`View ` * - ``void set_index(const Index& new_index)`` - void - pd_series.h:1417 - :ref:`View ` * - ``Series swaplevel(int i = -2, int j = -1, bool copy = true) const`` - Series - pd_series.h:15703 - :ref:`View ` * - ``void update(const Series& other)`` - void - pd_series.h:15754 - :ref:`View ` Missing Data ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series backfill( int axis = 0, bool inplace = false, std::optional limit = std::nullopt, const std::string& downcast = "") const`` - Series - pd_series.h:11928 - :ref:`View ` * - ``Series bfill( int axis = 0, bool inplace = false, std::optional limit = std::nullopt, const std::string& limit_area = "", const std::string& downcast = "") const`` - Series - pd_series.h:11842 - :ref:`View ` * - ``Series ffill( int axis = 0, bool inplace = false, std::optional limit = std::nullopt, const std::string& limit_area = "", const std::string& downcast = "") const`` - Series - pd_series.h:11947 - :ref:`View ` * - ``Series fillna( const T& value, const std::string& method = "", int axis = 0, bool inplace = false, std::optional limit = std::nullopt, const std::string& downcast = "") const`` - Series - pd_series.h:2653 - :ref:`View ` * - ``void fillna_double(double value) override`` - void - pd_series.h:2834 - * - ``Result fillna_resolved(const pandas::FillValue& fv) const`` - Result - pd_series.h:16299 - :ref:`View ` * - ``void fillna_string(const std::string& value) override`` - void - pd_series.h:2896 - :ref:`View ` * - ``Series fillna_t_or_default(const pandas::FillValue& fv) const`` - Series - pd_series.h:16336 - * - ``Series interpolate( const std::string& method = "linear", int axis = 0, std::optional limit = std::nullopt, bool inplace = false, const std::string& limit_direction = "forward", const std::string& limit_area = "", const std::string& downcast = "") const`` - Series - pd_series.h:12950 - :ref:`View ` * - ``numpy::NDArray isna() const`` - numpy::NDArray - pd_series.h:2580 - :ref:`View ` * - ``numpy::NDArray isnull() const`` - numpy::NDArray - pd_series.h:13520 - :ref:`View ` * - ``numpy::NDArray notna() const`` - numpy::NDArray - pd_series.h:2610 - :ref:`View ` * - ``numpy::NDArray notnull() const`` - numpy::NDArray - pd_series.h:13527 - :ref:`View ` * - ``Series pad( int axis = 0, bool inplace = false, std::optional limit = std::nullopt, const std::string& downcast = "") const`` - Series - pd_series.h:12012 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t count() const override`` - size_t - pd_series.h:2545 - :ref:`View ` * - ``Series cummax(int axis = 0, bool skipna = true) const`` - Series - pd_series.h:5107 - :ref:`View ` * - ``Series cummin(int axis = 0, bool skipna = true) const`` - Series - pd_series.h:5080 - :ref:`View ` * - ``Series cumprod(int axis = 0, bool skipna = true) const`` - Series - pd_series.h:5020 - :ref:`View ` * - ``Series cumsum(int axis = 0, bool skipna = true) const`` - Series - pd_series.h:4949 - :ref:`View ` * - ``DataFrame describe( const std::vector& percentiles = {0.25, 0.5, 0.75}, const std::string& include = "all", const std::string& exclude = "" ) const`` - DataFrame - pd_series.h:3742 - :ref:`View ` * - ``Series describe_as_series( const std::vector& percentiles = {0.25, 0.5, 0.75} ) const`` - Series - pd_series.h:3754 - :ref:`View ` * - ``std::optional kurt(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3578 - :ref:`View ` * - ``std::optional kurtosis(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3620 - :ref:`View ` * - ``std::optional max(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3373 - :ref:`View ` * - ``int max_decimal_places() const override`` - int - pd_series.h:9092 - * - ``std::optional mean(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3229 - :ref:`View ` * - ``std::optional median(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3469 - :ref:`View ` * - ``std::optional min(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3331 - :ref:`View ` * - ``Series mode(bool dropna = true) const`` - Series - pd_series.h:3629 - :ref:`View ` * - ``size_t nunique(bool dropna = true) const`` - size_t - pd_series.h:5645 - :ref:`View ` * - ``std::optional prod(bool skipna = true, int axis = 0, bool numeric_only = false, std::optional min_count = std::nullopt) const`` - std::optional - pd_series.h:3416 - :ref:`View ` * - ``std::optional product(bool skipna = true, int axis = 0, bool numeric_only = false, std::optional min_count = std::nullopt) const`` - std::optional - pd_series.h:13757 - :ref:`View ` * - ``std::optional quantile(double q, const std::string& interpolation = "linear") const`` - std::optional - pd_series.h:3696 - :ref:`View ` * - ``Series quantile_list( const std::vector& q_vals, const std::string& interpolation = "linear" ) const`` - Series - pd_series.h:3764 - :ref:`View ` * - ``std::optional sem(bool skipna = true, int ddof = 1, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3519 - :ref:`View ` * - ``std::optional skew(bool skipna = true, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3538 - :ref:`View ` * - ``std::optional std_(bool skipna = true, int ddof = 1, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3256 - :ref:`View ` * - ``auto sum(bool skipna = true, int axis = 0, bool numeric_only = false, std::optional min_count = std::nullopt) const`` - auto - pd_series.h:3113 - :ref:`View ` * - ``Series value_counts(bool normalize = false, bool sort = true, bool ascending = false, bool dropna = true, std::optional bins = std::nullopt) const`` - Series - pd_series.h:5745 - :ref:`View ` * - ``std::optional var(bool skipna = true, int ddof = 1, int axis = 0, bool numeric_only = false) const`` - std::optional - pd_series.h:3270 - :ref:`View ` Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``auto agg(Func&& func, int axis = 0) const -> decltype(func(std::declval>()))`` - auto - pd_series.h:6343 - :ref:`View ` * - ``std::optional agg(const std::string& func, int axis = 0) const`` - std::optional - pd_series.h:6369 - :ref:`View ` * - ``DataFrame agg(const std::vector& funcs, int axis = 0) const`` - DataFrame - pd_series.h:6423 - :ref:`View ` * - ``Result agg_with_dtype(const std::vector& funcs) const`` - Result - pd_series.h:16297 - :ref:`View ` * - ``auto aggregate(Func&& func, int axis = 0) const -> decltype(agg(std::forward(func), axis))`` - auto - pd_series.h:6431 - :ref:`View ` * - ``std::optional aggregate(const std::string& func, int axis = 0) const`` - std::optional - pd_series.h:6440 - :ref:`View ` * - ``DataFrame aggregate(const std::vector& funcs, int axis = 0) const`` - DataFrame - pd_series.h:6452 - :ref:`View ` * - ``auto apply(Func&& func, [[maybe_unused]] bool convert_dtype = true, [[maybe_unused]] const std::string& by_row = "compat") const`` - auto - pd_series.h:5867 - :ref:`View ` * - ``auto apply(Func&& func, [[maybe_unused]] bool convert_dtype, std::tuple args, [[maybe_unused]] const std::string& by_row = "compat") const`` - auto - pd_series.h:5909 - :ref:`View ` * - ``Series apply(const std::string& func, [[maybe_unused]] bool convert_dtype = true, [[maybe_unused]] const std::vector& args = {}) const`` - Series - pd_series.h:5991 - :ref:`View ` * - ``Result apply_dispatch( const FuncArg& func, const std::function& cell_cb = {} ) const`` - Result - pd_series.h:5986 - :ref:`View ` * - ``Series apply_ns_transform(Fn&& transform, const std::string& result_dtype) const`` - Series - pd_series.h:4351 - :ref:`View ` * - ``static void apply_override_to_result(Result& r, const std::string& override_dtype)`` - static void - pd_series.h:16328 - * - ``Result apply_resolved_typed( const std::function& cb, pandas::ApplyResultInference::ScalarKindHistogram& hist) const`` - Result - pd_series.h:16293 - :ref:`View ` * - ``apply_scientific_notation(val_strs, raw_vals)`` - - pd_series.h:10682 - * - ``apply_scientific_notation(val_strs, raw_vals)`` - - pd_series.h:11058 - * - ``auto apply_with_args(Func&& func, std::tuple args, [[maybe_unused]] bool convert_dtype = true, [[maybe_unused]] const std::string& by_row = "compat") const`` - auto - pd_series.h:5942 - :ref:`View ` * - ``EWM ewm( std::optional com = std::nullopt, std::optional span = std::nullopt, std::optional halflife = std::nullopt, std::optional alpha = std::nullopt, size_t min_periods = 0, bool adjust = true, bool ignore_na = false, int axis = 0, const std::string& times = "", const std::string& method = "single") const`` - EWM - pd_series.h:6537 - :ref:`View ` * - ``EWM ewm_full( std::optional com = std::nullopt, std::optional span = std::nullopt, std::optional halflife = std::nullopt, std::optional alpha = std::nullopt, size_t min_periods = 0, bool adjust = true, bool ignore_na = false, int axis = 0, const std::string& times = "", const std::string& method = "single") const`` - EWM - pd_series.h:6576 - :ref:`View ` * - ``EWM ewm_span(double span, bool adjust = true, bool ignore_na = false) const`` - EWM - pd_series.h:6558 - :ref:`View ` * - ``Expanding expanding( size_t min_periods = 1, int axis = 0, const std::string& method = "single") const`` - Expanding - pd_series.h:6510 - :ref:`View ` * - ``SeriesGroupBy groupby(const Series& by, bool sort = true, int axis = 0, std::optional level = std::nullopt, bool as_index = true, bool group_keys = true, bool observed = false, bool dropna = true) const`` - SeriesGroupBy - pd_series.h:6620 - :ref:`View ` * - ``SeriesGroupBy groupby_by_callable( const std::function& convert, bool sort = true) const`` - SeriesGroupBy - pd_series.h:4376 - :ref:`View ` * - ``SeriesGroupBy groupby_by_categorical( const CategoricalArray& cat, bool sort = true, bool observed = true) const`` - SeriesGroupBy - pd_series.h:6668 - :ref:`View ` * - ``SeriesGroupBy groupby_by_index( bool sort = true, bool observed = true) const`` - SeriesGroupBy - pd_series.h:6649 - :ref:`View ` * - ``SeriesGroupBy groupby_by_labels( const std::vector& labels, const std::string& grouper_dtype = "object", bool sort = true) const`` - SeriesGroupBy - pd_series.h:6688 - :ref:`View ` * - ``SeriesGroupBy groupby_by_level( const std::vector& level_indices, bool sort = true) const`` - SeriesGroupBy - pd_series.h:6631 - :ref:`View ` * - ``SeriesGroupBy groupby_by_level( size_t level_idx, bool sort = true) const`` - SeriesGroupBy - pd_series.h:6640 - :ref:`View ` * - ``SeriesGroupBy groupby_by_level_names( const std::vector& level_names, bool sort = true) const`` - SeriesGroupBy - pd_series.h:6678 - :ref:`View ` * - ``SeriesGroupBy groupby_by_numeric( const Series& by_numeric, bool sort = true) const`` - SeriesGroupBy - pd_series.h:6658 - :ref:`View ` * - ``SeriesGroupBy groupby_by_string_series( const Series& by_str, bool sort = true) const`` - SeriesGroupBy - pd_series.h:6699 - :ref:`View ` * - ``Series map( const std::unordered_map& mapping, const std::string& na_action = "") const`` - Series - pd_series.h:6038 - :ref:`View ` * - ``auto map( Func&& func, const std::string& na_action = "") const -> Series()))>`` - auto - pd_series.h:6230 - :ref:`View ` * - ``Series map_dict(const std::map& mapping) const`` - Series - pd_series.h:6101 - :ref:`View ` * - ``Result map_dict_resolved(const std::map& m) const`` - Result - pd_series.h:16316 - :ref:`View ` * - ``Series map_series(const Series& lookup) const`` - Series - pd_series.h:6134 - :ref:`View ` * - ``Result map_series_resolved(const pandas::Series& mapper) const`` - Result - pd_series.h:16319 - :ref:`View ` * - ``Series map_to_string(const Series& lookup) const`` - Series - pd_series.h:6184 - :ref:`View ` * - ``auto pipe(Func&& func, Args&&... args) const`` - auto - pd_series.h:6458 - :ref:`View ` * - ``SeriesResampler resample(const std::string& freq, const std::string& closed = "", const std::string& label = "", int axis = 0, const std::string& convention = "start", const std::string& kind = "", const std::optional& on = std::nullopt, std::optional level = std::nullopt, const std::string& origin = "start_day", std::optional offset = std::nullopt, bool group_keys = false) const`` - SeriesResampler - pd_series.h:6748 - :ref:`View ` * - ``Rolling rolling( size_t window, size_t min_periods = 1, bool center = false, const std::string& win_type = "", const std::string& on = "", int axis = 0, const std::string& closed = "right", size_t step = 1, const std::string& method = "single") const`` - Rolling - pd_series.h:6486 - :ref:`View ` * - ``Series transform(const std::string& func, int axis = 0) const`` - Series - pd_series.h:6281 - :ref:`View ` * - ``Series transform(Func&& func, int axis = 0) const`` - Series - pd_series.h:6318 - :ref:`View ` * - ``DataFrame transform_named_list(const std::vector& func_names) const`` - DataFrame - pd_series.h:6309 - :ref:`View ` * - ``Result transform_resolved( const std::function& cb, pandas::ApplyResultInference::ScalarKindHistogram& hist) const`` - Result - pd_series.h:16321 - :ref:`View ` Arithmetic ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series add(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4208 - :ref:`View ` * - ``Series add(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4255 - :ref:`View ` * - ``Series add_dateoffset(const pandas::DateOffset& off) const`` - Series - pd_series.h:4298 - :ref:`View ` * - ``Series add_dateoffset_to_timedelta(const pandas::DateOffset& off) const`` - Series - pd_series.h:4340 - :ref:`View ` * - ``Series add_int64(int64_t scalar_ns) const`` - Series - pd_series.h:4265 - * - ``Series add_prefix(const std::string& prefix, int axis = 0) const`` - Series - pd_series.h:8941 - :ref:`View ` * - ``Series add_suffix(const std::string& suffix, int axis = 0) const`` - Series - pd_series.h:8970 - :ref:`View ` * - ``Series div(const Series& other, T fill_value = T{1}, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4487 - :ref:`View ` * - ``Series div(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4519 - :ref:`View ` * - ``Series divide(const Series& other, T fill_value = T{1}, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4537 - :ref:`View ` * - ``std::pair, Series> divmod(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - std::pair, Series> - pd_series.h:12352 - :ref:`View ` * - ``std::pair, Series> divmod(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - std::pair, Series> - pd_series.h:12376 - :ref:`View ` * - ``T dot(const Series& other) const`` - T - pd_series.h:12410 - :ref:`View ` * - ``T dot(const numpy::NDArray& other) const`` - T - pd_series.h:12422 - :ref:`View ` * - ``Series floordiv(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4567 - :ref:`View ` * - ``Series floordiv(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4591 - :ref:`View ` * - ``Series mod(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4615 - :ref:`View ` * - ``Series mod(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4636 - :ref:`View ` * - ``Series mul(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4435 - :ref:`View ` * - ``Series mul(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4469 - :ref:`View ` * - ``const MultiIndex& multiindex() const`` - const MultiIndex& - pd_series.h:1521 - :ref:`View ` * - ``Series multiply(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4475 - :ref:`View ` * - ``Series pow(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4660 - :ref:`View ` * - ``Series pow(double exponent, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4676 - :ref:`View ` * - ``Series radd(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13771 - :ref:`View ` * - ``Series radd(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13777 - :ref:`View ` * - ``Series rdiv(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13840 - :ref:`View ` * - ``std::pair, Series> rdivmod(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - std::pair, Series> - pd_series.h:13949 - :ref:`View ` * - ``Series rfloordiv(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13876 - :ref:`View ` * - ``Series rmod(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13902 - :ref:`View ` * - ``Series rmul(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13819 - :ref:`View ` * - ``Series rmul(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13825 - :ref:`View ` * - ``Series rpow(double other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13928 - :ref:`View ` * - ``Series rsub(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13792 - :ref:`View ` * - ``Series rsub(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13804 - :ref:`View ` * - ``Series rtruediv(const T& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:13862 - :ref:`View ` * - ``Series sub(const Series& other, T fill_value = T{}, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4387 - :ref:`View ` * - ``Series sub(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4417 - :ref:`View ` * - ``Series sub_dateoffset(const pandas::DateOffset& off) const`` - Series - pd_series.h:4326 - :ref:`View ` * - ``Series sub_int64(int64_t scalar_ns) const`` - Series - pd_series.h:4289 - * - ``Series subtract(const Series& other, T fill_value = T{}, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4423 - :ref:`View ` * - ``Series truediv(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4549 - :ref:`View ` * - ``Series truediv(T scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:4556 - :ref:`View ` Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame compare( const Series& other, int align_axis = 1, bool keep_shape = false, bool keep_equal = false, const std::pair& result_names = {"self", "other"} ) const`` - DataFrame - pd_series.h:12194 - :ref:`View ` * - ``Series eq(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12505 - :ref:`View ` * - ``Series eq(const T& scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12513 - :ref:`View ` * - ``bool equals(const Series& other) const`` - bool - pd_series.h:12637 - :ref:`View ` * - ``Series ge(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12614 - :ref:`View ` * - ``Series ge(const T& scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12622 - :ref:`View ` * - ``std::mt19937 gen(seed_val == 0 ? std::random_device{}() : seed_val)`` - std::mt19937 - pd_series.h:15561 - :ref:`View ` * - ``Series gt(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12593 - :ref:`View ` * - ``Series gt(const T& scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12601 - :ref:`View ` * - ``Series le(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12568 - :ref:`View ` * - ``Series le(const T& scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12576 - :ref:`View ` * - ``std::vector> level_arrays(nlevels)`` - std::vector> - pd_series.h:2069 - * - ``std::vector> level_arrays(nlevels)`` - std::vector> - pd_series.h:2131 - * - ``Series lt(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12547 - :ref:`View ` * - ``Series lt(const T& scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12555 - :ref:`View ` * - ``Series ne(const Series& other, std::optional fill_value = std::nullopt, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12526 - :ref:`View ` * - ``Series ne(const T& scalar, int axis = 0, std::optional level = std::nullopt) const`` - Series - pd_series.h:12534 - :ref:`View ` Sorting ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray argsort(int axis = 0, const std::string& kind = "quicksort", const std::string& order = "", bool ascending = true, bool stable = false) const`` - numpy::NDArray - pd_series.h:5202 - :ref:`View ` * - ``Series rank(const std::string& method = "average", bool ascending = true, const std::string& na_option = "keep", bool pct = false, int axis = 0, bool numeric_only = false) const`` - Series - pd_series.h:5480 - :ref:`View ` * - ``static std::vector rank_values(const std::vector& values)`` - static std::vector - pd_series.h:16005 - * - ``size_t searchsorted(const T& value, const std::string& side = "left", const std::vector& sorter = {}) const`` - size_t - pd_series.h:15630 - :ref:`View ` * - ``Series sort_index(bool ascending = true, int axis = 0, bool inplace = false, const std::string& kind = "quicksort", const std::string& na_position = "last", bool sort_remaining = true, bool ignore_index = false, std::function key = nullptr, std::optional level = std::nullopt) const`` - Series - pd_series.h:5348 - :ref:`View ` * - ``Series sort_values( bool ascending = true, bool inplace = false, const std::string& kind = "quicksort", const std::string& na_position = "last", bool ignore_index = false, std::function key = nullptr, int axis = 0) const`` - Series - pd_series.h:5244 - :ref:`View ` Reshaping --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series T_() const`` - Series - pd_series.h:15746 - :ref:`View ` * - ``Series explode(bool ignore_index = false) const`` - Series - pd_series.h:8857 - :ref:`View ` * - ``Series squeeze() const`` - Series - pd_series.h:15680 - :ref:`View ` * - ``Series swapaxes(int axis1, int axis2, bool copy = true) const`` - Series - pd_series.h:15722 - :ref:`View ` * - ``DataFrame to_frame(const std::optional& name = std::nullopt) const`` - DataFrame - pd_series.h:8240 - :ref:`View ` * - ``Series transpose() const`` - Series - pd_series.h:15739 - :ref:`View ` * - ``DataFrame unstack( int level = -1, std::optional fill_value = std::nullopt, bool sort = true ) const`` - DataFrame - pd_series.h:8881 - :ref:`View ` Combining --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::pair, Series> align( const Series& other, const std::string& join = "outer", int axis = 0, std::optional level = std::nullopt, bool copy = true, const std::optional& fill_value = std::nullopt, const std::string& method = "", std::optional limit = std::nullopt, int fill_axis = 0, std::optional broadcast_axis = std::nullopt) const`` - std::pair, Series> - pd_series.h:11650 - :ref:`View ` * - ``Series aligned_binary_op( const Series& other, BinaryOp op) const`` - Series - pd_series.h:16102 - :ref:`View ` * - ``Series aligned_binary_op_cross( const Series& other, BinaryOp op) const`` - Series - pd_series.h:16188 - :ref:`View ` * - ``Series combine( const Series& other, Func&& func, const std::optional& fill_value = std::nullopt) const`` - Series - pd_series.h:12086 - :ref:`View ` * - ``Series combine_first(const Series& other) const`` - Series - pd_series.h:12115 - :ref:`View ` * - ``static Series concat( const std::vector>& series, bool ignore_index = false, Series\* out_float = nullptr)`` - static Series - pd_series.h:11386 - :ref:`View ` * - ``std::unique_ptr concat_with(const NDFrameBase& other) const override`` - std::unique_ptr - pd_series.h:11215 - Time Series ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series asfreq( const std::string& freq, const std::string& method = "", const std::string& how = "", bool normalize = false, const std::optional& fill_value = std::nullopt) const`` - Series - pd_series.h:6771 - :ref:`View ` * - ``std::optional asof(const std::string& where, const std::vector& subset = {}) const`` - std::optional - pd_series.h:11745 - :ref:`View ` * - ``T asof(size_t where) const`` - T - pd_series.h:15832 - :ref:`View ` * - ``Series asof(const std::vector& where) const`` - Series - pd_series.h:15879 - :ref:`View ` * - ``Series between_time( const std::string& start_time, const std::string& end_time, const std::string& inclusive = "both", int axis = 0 ) const`` - Series - pd_series.h:15929 - :ref:`View ` * - ``Series diff(int periods = 1) const`` - Series - pd_series.h:5132 - :ref:`View ` * - ``Series pct_change( int periods = 1, const std::string& fill_method = "", std::optional limit = std::nullopt, const std::string& freq = "") const`` - Series - pd_series.h:3794 - :ref:`View ` * - ``Series shift(int periods = 1, const std::optional& fill_value = std::nullopt, int axis = 0, const std::string& freq = "", const std::string& suffix = "") const`` - Series - pd_series.h:5158 - :ref:`View ` * - ``Series to_period(const std::string& freq = "", bool copy = true) const`` - Series - pd_series.h:8260 - :ref:`View ` * - ``Series to_timestamp(const std::string& freq = "", const std::string& how = "start", bool copy = true) const`` - Series - pd_series.h:8281 - :ref:`View ` * - ``Series tz_convert(const std::string& tz, int axis = 0, std::optional level = std::nullopt, bool copy = true) const`` - Series - pd_series.h:15958 - :ref:`View ` * - ``Series tz_localize( const std::string& tz, int axis = 0, std::optional level = std::nullopt, bool copy = true, const std::string& ambiguous = "raise", const std::string& nonexistent = "raise" ) const`` - Series - pd_series.h:15984 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void to_clipboard(bool excel = true, const std::string& sep = "\\t") const`` - void - pd_series.h:8579 - :ref:`View ` * - ``std::string to_csv( bool index = true, char sep = ',', [[maybe_unused]] const std::string& na_rep = "", [[maybe_unused]] const std::string& float_format = "", [[maybe_unused]] const std::vector& columns = {}, bool header = true, [[maybe_unused]] const std::string& index_label = "", [[maybe_unused]] const std::string& mode = "w", [[maybe_unused]] const std::string& encoding = "utf-8", [[maybe_unused]] const std::string& compression = "infer", [[maybe_unused]] int quoting = 0, [[maybe_unused]] char quotechar = '"', const std::string& lineterminator = "\\n", [[maybe_unused]] bool doublequote = true, [[maybe_unused]] const std::string& escapechar = "", [[maybe_unused]] std::optional chunksize = std::nullopt, [[maybe_unused]] const std::string& date_format = "", [[maybe_unused]] const std::string& errors = "strict", [[maybe_unused]] char decimal = '.', [[maybe_unused]] const std::string& storage_options = "", [[maybe_unused]] const std::string& path_or_buf = "") const`` - std::string - pd_series.h:8082 - :ref:`View ` * - ``std::map to_dict() const`` - std::map - pd_series.h:8221 - :ref:`View ` * - ``void to_excel( const std::string& excel_writer, const std::string& sheet_name = "Sheet1", const std::string& na_rep = "", const std::string& float_format = "", bool header = true, bool index = true, const std::string& index_label = "", size_t startrow = 0, size_t startcol = 0, bool merge_cells = true, const std::string& inf_rep = "inf", std::optional> freeze_panes = std::nullopt, [[maybe_unused]] const std::vector& columns = {}, [[maybe_unused]] const std::string& engine = "openpyxl", [[maybe_unused]] const std::map& engine_kwargs = {}, [[maybe_unused]] const std::string& storage_options = "") const`` - void - pd_series.h:8720 - :ref:`View ` * - ``std::vector to_feather( const std::string& path = "", const std::string& compression = "uncompressed", int compression_level = -1, int64_t chunksize = -1, int version = 2) const`` - std::vector - pd_series.h:8817 - :ref:`View ` * - ``void to_hdf( const std::string& path_or_buf, const std::string& key = "data", const std::string& mode = "a", std::optional complevel = std::nullopt, const std::string& complib = "zlib", bool append = false, const std::string& format = "fixed", bool index = true, [[maybe_unused]] const std::vector& data_columns = {}, [[maybe_unused]] bool dropna = false, [[maybe_unused]] const std::string& encoding = "utf-8", [[maybe_unused]] const std::string& errors = "strict", [[maybe_unused]] const std::map& min_itemsize = {}, [[maybe_unused]] const std::string& nan_rep = "") const`` - void - pd_series.h:8643 - :ref:`View ` * - ``std::string to_json( [[maybe_unused]] const std::string& path_or_buf = "", const std::string& orient = "index", [[maybe_unused]] const std::string& date_format = "epoch", [[maybe_unused]] int double_precision = 10, [[maybe_unused]] bool force_ascii = true, [[maybe_unused]] const std::string& date_unit = "ms", [[maybe_unused]] const std::string& default_handler = "", [[maybe_unused]] bool lines = false, [[maybe_unused]] const std::string& compression = "infer", [[maybe_unused]] bool index = true, [[maybe_unused]] int indent = 0, [[maybe_unused]] const std::string& storage_options = "", [[maybe_unused]] const std::string& mode = "w") const`` - std::string - pd_series.h:8146 - :ref:`View ` * - ``std::string to_latex( bool header = true, bool index = true, [[maybe_unused]] const std::string& na_rep = "NaN", [[maybe_unused]] const std::string& float_format = "", [[maybe_unused]] const std::map>& formatters = {}, [[maybe_unused]] bool bold_rows = false, [[maybe_unused]] const std::string& column_format = "", [[maybe_unused]] bool longtable = false, [[maybe_unused]] bool escape = true, [[maybe_unused]] bool sparsify = true, [[maybe_unused]] bool index_names = true, [[maybe_unused]] char decimal = '.', [[maybe_unused]] bool multicolumn = true, [[maybe_unused]] const std::string& multicolumn_format = "l", [[maybe_unused]] bool multirow = false, [[maybe_unused]] const std::string& caption = "", [[maybe_unused]] const std::string& label = "", [[maybe_unused]] const std::string& position = "", [[maybe_unused]] std::ostream\* buf = nullptr, [[maybe_unused]] const std::vector& columns = {}, [[maybe_unused]] const std::string& encoding = "utf-8") const`` - std::string - pd_series.h:8380 - :ref:`View ` * - ``void to_latex(const std::string& path, bool header = true, bool index = true) const`` - void - pd_series.h:8441 - :ref:`View ` * - ``file << to_latex(header, index)`` - file << - pd_series.h:8446 - :ref:`View ` * - ``std::vector to_list() const`` - std::vector - pd_series.h:8012 - :ref:`View ` * - ``std::string to_markdown( bool index = true, [[maybe_unused]] std::ostream\* buf = nullptr, [[maybe_unused]] const std::string& mode = "wt", [[maybe_unused]] const std::string& storage_options = "") const`` - std::string - pd_series.h:8460 - :ref:`View ` * - ``void to_markdown(const std::string& path, bool index = true) const`` - void - pd_series.h:8502 - :ref:`View ` * - ``file << to_markdown(index)`` - file << - pd_series.h:8507 - :ref:`View ` * - ``numpy::NDArray to_numpy() const`` - numpy::NDArray - pd_series.h:7956 - :ref:`View ` * - ``numpy::NDArray to_numpy(bool copy) const`` - numpy::NDArray - pd_series.h:7984 - :ref:`View ` * - ``numpy::NDArray to_numpy(bool copy, U na_value) const`` - numpy::NDArray - pd_series.h:8003 - :ref:`View ` * - ``std::vector to_orc( const std::string& path = "", const std::string& engine = "pyarrow", std::optional index = std::nullopt) const`` - std::vector - pd_series.h:8842 - :ref:`View ` * - ``std::vector to_parquet( const std::string& path = "", const std::string& engine = "pyarrow", const std::string& compression = "snappy", std::optional index = std::nullopt) const`` - std::vector - pd_series.h:8757 - :ref:`View ` * - ``void to_pickle( const std::string& path, [[maybe_unused]] const std::string& compression = "infer", [[maybe_unused]] int protocol = 5, [[maybe_unused]] const std::string& storage_options = "") const`` - void - pd_series.h:8530 - :ref:`View ` * - ``std::optional to_sql( const std::string& name, const std::string& con, const std::string& schema = "", const std::string& if_exists = "fail", bool index = true, const std::string& index_label = "", std::optional chunksize = std::nullopt, const std::map& dtype = {}, const std::string& method = "") const`` - std::optional - pd_series.h:8683 - :ref:`View ` * - ``std::vector to_stata( const std::string& path = "", const std::map& convert_dates = {}, bool write_index = true, const std::string& byteorder = "", const std::string& time_stamp = "", const std::string& data_label = "", const std::map& variable_labels = {}, int version = 114) const`` - std::vector - pd_series.h:8786 - :ref:`View ` * - ``std::string to_string() const override`` - std::string - pd_series.h:10553 - :ref:`View ` * - ``std::string to_string_full( [[maybe_unused]] std::ostream\* buf, [[maybe_unused]] const std::string& na_rep, [[maybe_unused]] const std::string& float_format, [[maybe_unused]] bool header, bool index, bool length, bool dtype, bool name, std::optional max_rows, [[maybe_unused]] std::optional min_rows) const`` - std::string - pd_series.h:10989 - :ref:`View ` * - ``std::string to_string_multiindex() const`` - std::string - pd_series.h:10878 - * - ``std::vector to_string_vector() const override`` - std::vector - pd_series.h:10371 - :ref:`View ` * - ``DataArray to_xarray() const`` - DataArray - pd_series.h:8306 - :ref:`View ` * - ``std::vector tolist() const`` - std::vector - pd_series.h:15732 - :ref:`View ` Conversion ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series astype() const`` - Series - pd_series.h:6890 - :ref:`View ` * - ``std::unique_ptr astype( const std::string& dtype_str, const std::vector& cats = {}, bool ordered = false) const`` - std::unique_ptr - pd_series.h:7332 - :ref:`View ` * - ``std::unique_ptr astype_boolean() const`` - std::unique_ptr - pd_series.h:7091 - * - ``std::unique_ptr astype_category(const std::vector& cats, bool ordered) const`` - std::unique_ptr - pd_series.h:7012 - * - ``std::unique_ptr astype_dtype(const std::string& dtype_str) const override`` - std::unique_ptr - pd_series.h:11182 - :ref:`View ` * - ``std::unique_ptr astype_nullable_int(const std::string& dtype_str) const`` - std::unique_ptr - pd_series.h:6963 - * - ``std::unique_ptr astype_numeric() const`` - std::unique_ptr - pd_series.h:7271 - * - ``Series astype_object() const`` - Series - pd_series.h:7624 - * - ``std::unique_ptr astype_period() const`` - std::unique_ptr - pd_series.h:7262 - * - ``std::unique_ptr astype_string(const std::string& target_dtype) const`` - std::unique_ptr - pd_series.h:7146 - * - ``bool bool_() const`` - bool - pd_series.h:12024 - :ref:`View ` * - ``static numpy::NDArray bool_vec_to_cond(const std::vector& cond)`` - static numpy::NDArray - pd_series.h:2442 - :ref:`View ` * - ``std::unique_ptr convert_dtypes( bool infer_objects = true, bool convert_string = true, bool convert_integer = true, bool convert_boolean = true, bool convert_floating = true, const std::string& dtype_backend = "numpy_nullable") const`` - std::unique_ptr - pd_series.h:7659 - :ref:`View ` * - ``Series copy(bool deep = true) const`` - Series - pd_series.h:6870 - :ref:`View ` * - ``void copy_series_metadata_from(const Series& source)`` - void - pd_series.h:16261 - :ref:`View ` * - ``void copy_value_from(size_t src_idx, size_t dst_idx) override`` - void - pd_series.h:3086 - * - ``std::unique_ptr infer_objects(bool copy = true) const`` - std::unique_ptr - pd_series.h:7787 - :ref:`View ` * - ``Series view() const`` - Series - pd_series.h:15773 - :ref:`View ` Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector> items() const`` - std::vector> - pd_series.h:13549 - :ref:`View ` * - ``void items(Func&& func) const`` - void - pd_series.h:13563 - :ref:`View ` * - ``std::vector keys() const`` - std::vector - pd_series.h:13573 - :ref:`View ` Set Operations -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray duplicated(const std::string& keep = "first") const`` - numpy::NDArray - pd_series.h:5670 - :ref:`View ` * - ``Series isin(const std::vector& values) const`` - Series - pd_series.h:9047 - :ref:`View ` * - ``Series unique() const`` - Series - pd_series.h:5627 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static bool is_bool_string(const std::string& value)`` - static bool - pd_series.h:621 - * - ``static bool is_float_actually_integer(double value)`` - static bool - pd_series.h:666 - * - ``static bool is_float_string(const std::string& value)`` - static bool - pd_series.h:647 - * - ``bool is_int_dtype() const`` - bool - pd_series.h:8023 - * - ``static bool is_integer_string(const std::string& value)`` - static bool - pd_series.h:629 - * - ``bool is_na_at(size_t idx) const override`` - bool - pd_series.h:2807 - :ref:`View ` * - ``bool is_nullable_int_dtype() const`` - bool - pd_series.h:8043 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector> B_mat(static_cast(nvalid), std::vector(n_basis, 0.0))`` - std::vector> - pd_series.h:13318 - * - ``std::vector> B_mat2(static_cast(nvalid), std::vector(n_basis, 0.0))`` - std::vector> - pd_series.h:13359 - * - ``std::vector> B_mat2(static_cast(nvalid), std::vector(n_basis, 0.0))`` - std::vector> - pd_series.h:13376 - * - ``GlobalUnlock(hMem)`` - - pd_series.h:8606 - :ref:`View ` * - ``SetClipboardData(CF_TEXT, hMem)`` - - pd_series.h:8609 - * - ``std::vector> VtV(pn, std::vector(pn, 0.0))`` - std::vector> - pd_series.h:13336 - * - ``Series abs() const`` - Series - pd_series.h:4896 - :ref:`View ` * - ``bool all_values_whole_number() const override`` - bool - pd_series.h:9066 - :ref:`View ` * - ``bool any_missing_cells() const`` - bool - pd_series.h:16332 - * - ``size_t argmax() const`` - size_t - pd_series.h:3875 - :ref:`View ` * - ``size_t argmin() const`` - size_t - pd_series.h:3844 - :ref:`View ` * - ``std::optional autocorr(int lag = 1) const`` - std::optional - pd_series.h:11780 - :ref:`View ` * - ``std::vector axes() const override`` - std::vector - pd_series.h:1532 - :ref:`View ` * - ``Series between(const T& left, const T& right, const std::string& inclusive = "both") const`` - Series - pd_series.h:4863 - :ref:`View ` * - ``size_t cache_memory_usage() const override`` - size_t - pd_series.h:11620 - * - ``static std::string canonical_dtype_name()`` - static std::string - pd_series.h:1178 - :ref:`View ` * - ``Series case_when( const std::vector, T>>& caselist, const std::optional& default_value = std::nullopt) const`` - Series - pd_series.h:12047 - :ref:`View ` * - ``CategoricalAccessor> cat() const`` - CategoricalAccessor> - pd_series.h:10522 - :ref:`View ` * - ``bool cat_ordered() const override`` - bool - pd_series.h:1262 - :ref:`View ` * - ``Series ceil() const`` - Series - pd_series.h:4724 - :ref:`View ` * - ``clamp_bool_values(result_data, promo)`` - - pd_series.h:3961 - * - ``clamp_bool_values(result_data, promo)`` - - pd_series.h:4043 - * - ``static void clamp_bool_values(std::vector& data, const std::optional& promo)`` - void - pd_series.h:4192 - * - ``clamp_bool_values(result_data, promo)`` - - pd_series.h:4248 - * - ``clamp_bool_values(result_data, promo)`` - - pd_series.h:16123 - * - ``clamp_bool_values(result_data, promo)`` - - pd_series.h:16166 - * - ``void clear_cache() const override`` - void - pd_series.h:11605 - :ref:`View ` * - ``void clear_dtype_override() override`` - void - pd_series.h:1234 - * - ``Series clip(const T& lower, const T& upper, int axis = 0, bool inplace = false) const`` - Series - pd_series.h:4923 - :ref:`View ` * - ``std::unique_ptr clone() const override`` - std::unique_ptr - pd_series.h:11170 - :ref:`View ` * - ``std::optional corr( const Series& other, const std::string& method = "pearson", std::optional min_periods = std::nullopt) const`` - std::optional - pd_series.h:12209 - :ref:`View ` * - ``std::optional cov( const Series& other, std::optional min_periods = std::nullopt, int ddof = 1) const`` - std::optional - pd_series.h:12283 - :ref:`View ` * - ``const std::optional>& datetime_array() const`` - const std::optional>& - pd_series.h:1277 - :ref:`View ` * - ``static std::string datetime_ns_to_string(double v)`` - static std::string - pd_series.h:6922 - :ref:`View ` * - ``numpy::datetime64 dt(ns, numpy::DateTimeUnit::Nanosecond)`` - numpy::datetime64 - pd_series.h:4312 - :ref:`View ` * - ``DatetimeProperties> dt() const`` - DatetimeProperties> - pd_series.h:10497 - :ref:`View ` * - ``std::string dtype_name() const override`` - std::string - pd_series.h:1133 - :ref:`View ` * - ``std::string dtype_name_full() const override`` - std::string - pd_series.h:1191 - :ref:`View ` * - ``const std::optional& dtype_override() const`` - const std::optional& - pd_series.h:1167 - :ref:`View ` * - ``emit_val(i)`` - - pd_series.h:8181 - * - ``emit_val(i)`` - - pd_series.h:8200 - * - ``emit_val(i)`` - - pd_series.h:8209 - * - ``bool empty() const override`` - bool - pd_series.h:1097 - :ref:`View ` * - ``std::pair, Series> factorize(bool sort = false, bool use_na_sentinel = true) const`` - std::pair, Series> - pd_series.h:12669 - :ref:`View ` * - ``std::ofstream file(path)`` - std::ofstream - pd_series.h:8442 - :ref:`View ` * - ``std::ofstream file(path)`` - std::ofstream - pd_series.h:8503 - :ref:`View ` * - ``std::ofstream file(path, std::ios::binary)`` - std::ofstream - pd_series.h:8535 - :ref:`View ` * - ``Series filter( const std::optional>& items = std::nullopt, const std::optional& like = std::nullopt, const std::optional& regex = std::nullopt, int axis = 0) const`` - Series - pd_series.h:12823 - :ref:`View ` * - ``Series filter_by_bool_mask(const pandas::BooleanMask& mask) const`` - Series - pd_series.h:1706 - :ref:`View ` * - ``Series filter_by_bool_series(const Series& cond) const`` - Series - pd_series.h:1726 - :ref:`View ` * - ``Series floor() const`` - Series - pd_series.h:4710 - :ref:`View ` * - ``oss << fmt_cat(0) << sep << fmt_cat(1) << line_sep << indent`` - oss << fmt_cat(0) << sep << - pd_series.h:10855 - * - ``oss << fmt_cat(2) << sep << fmt_cat(3) << sep << "..." << line_sep << indent`` - oss << fmt_cat(2) << sep << - pd_series.h:10857 - * - ``oss << fmt_cat(last_start) << sep << fmt_cat(last_start + 1) << sep`` - oss << fmt_cat(last_start) << sep << - pd_series.h:10860 - * - ``oss << fmt_cat(last_start + 2) << line_sep << indent`` - oss << - pd_series.h:10861 - * - ``oss << fmt_cat(last_start + 3)`` - oss << - pd_series.h:10862 - * - ``oss << fmt_cat(i)`` - oss << - pd_series.h:10866 - * - ``std::string format_display_name() const`` - std::string - pd_series.h:1353 - * - ``func(index_->get_value_str(i), data_.getElementAt({i}))`` - - pd_series.h:13565 - :ref:`View ` * - ``bool has_cached_values() const override`` - bool - pd_series.h:11609 - :ref:`View ` * - ``bool has_cat_categories() const override`` - bool - pd_series.h:1241 - :ref:`View ` * - ``bool has_mask() const override`` - bool - pd_series.h:1291 - :ref:`View ` * - ``bool has_multiindex() const`` - bool - pd_series.h:1514 - :ref:`View ` * - ``bool has_nan_values() const`` - bool - pd_series.h:6905 - * - ``bool hasnans() const override`` - bool - pd_series.h:2524 - :ref:`View ` * - ``std::string indent(header.size(), ' ')`` - std::string - pd_series.h:10851 - * - ``const IndexBase& index() const override`` - const IndexBase& - pd_series.h:1389 - :ref:`View ` * - ``IndexBase& index_mut()`` - IndexBase& - pd_series.h:1396 - :ref:`View ` * - ``static std::string infer_best_dtype( const std::vector& str_values, const std::string& source_dtype, bool convert_string, bool convert_integer, bool convert_boolean, bool convert_floating)`` - static std::string - pd_series.h:681 - * - ``void info(std::ostream\* buf = nullptr, bool verbose = true, int max_cols = 0, bool memory_usage = true, bool show_counts = true) const`` - void - pd_series.h:12918 - :ref:`View ` * - ``void invalidate_caches() const`` - void - pd_series.h:16024 - * - ``T item() const`` - T - pd_series.h:13535 - :ref:`View ` * - ``std::unique_ptr make_default_index(size_t sz) const`` - std::unique_ptr - pd_series.h:16047 - * - ``memcpy(pMem, text.c_str(), text.size() + 1)`` - - pd_series.h:8605 - :ref:`View ` * - ``size_t memory_usage(bool index = true, bool deep = false) const`` - size_t - pd_series.h:13608 - :ref:`View ` * - ``const std::optional>& mixed_tz_array() const`` - const std::optional>& - pd_series.h:1282 - :ref:`View ` * - ``std::optional name() const override`` - std::optional - pd_series.h:1308 - :ref:`View ` * - ``const std::string& name_interval_closed() const`` - const std::string& - pd_series.h:1344 - * - ``double name_interval_left() const`` - double - pd_series.h:1342 - * - ``double name_interval_right() const`` - double - pd_series.h:1343 - * - ``bool name_is_int() const`` - bool - pd_series.h:1328 - * - ``bool name_is_interval() const`` - bool - pd_series.h:1335 - * - ``bool name_is_timestamp() const`` - bool - pd_series.h:1366 - * - ``bool name_is_tuple() const`` - bool - pd_series.h:1346 - * - ``int64_t name_timestamp_ns() const`` - int64_t - pd_series.h:1372 - * - ``const std::string& name_timestamp_tz() const`` - const std::string& - pd_series.h:1373 - * - ``const std::vector& name_tuple_elements() const`` - const std::vector& - pd_series.h:1350 - * - ``size_t nbytes() const override`` - size_t - pd_series.h:1118 - :ref:`View ` * - ``size_t ndim() const override`` - size_t - pd_series.h:1111 - :ref:`View ` * - ``static std::optional promote_arith_dtype( const std::optional& a, const std::optional& b)`` - static std::optional - pd_series.h:4118 - * - ``static std::optional promote_div_dtype( const std::optional& a, const std::optional& b)`` - static std::optional - pd_series.h:4165 - * - ``static std::optional promote_div_scalar_dtype( const std::optional& series_dt, T2 /\*scalar\*/)`` - std::optional - pd_series.h:4183 - * - ``static std::optional promote_scalar_dtype( const std::optional& series_dt, T2 scalar)`` - std::optional - pd_series.h:4140 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:3964 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:3984 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:4012 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:4032 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:4251 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:4413 - * - ``propagate_index_to(result, len, other)`` - - pd_series.h:4465 - * - ``propagate_index_to(res, len, other)`` - - pd_series.h:4587 - * - ``propagate_index_to(res, len, other)`` - - pd_series.h:4632 - * - ``propagate_index_to(res, len, other)`` - - pd_series.h:4672 - * - ``void propagate_index_to(Series& result, size_t result_len) const`` - void - pd_series.h:16056 - * - ``void propagate_index_to(Series& result, size_t result_len, const Series& other) const`` - void - pd_series.h:16070 - * - ``numpy::NDArray ravel() const`` - numpy::NDArray - pd_series.h:13974 - :ref:`View ` * - ``Series repeat(size_t repeats, int axis = 0) const`` - Series - pd_series.h:15432 - :ref:`View ` * - ``std::string repr() const override`` - std::string - pd_series.h:11133 - :ref:`View ` * - ``size_t resolve_multiindex_level(const std::string& name) const`` - size_t - pd_series.h:6705 - :ref:`View ` * - ``size_t resolve_multiindex_level(int level) const`` - size_t - pd_series.h:6716 - :ref:`View ` * - ``size_t resolve_slice_bound( const std::variant& v, bool is_start, bool /\*inclusive\*/) const`` - size_t - pd_series.h:1832 - * - ``resolve_slice_bound(spec.start, true, true))`` - - pd_series.h:1942 - * - ``resolve_slice_bound(spec.stop, true, true))`` - - pd_series.h:1951 - * - ``std::string result(buf)`` - std::string - pd_series.h:9454 - :ref:`View ` * - ``std::string result(buf)`` - std::string - pd_series.h:9599 - :ref:`View ` * - ``Series round(int decimals = 0) const`` - Series - pd_series.h:4695 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:9117 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:9178 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:9233 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:9738 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:9778 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:14891 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:15230 - :ref:`View ` * - ``std::string s(buf)`` - std::string - pd_series.h:15281 - :ref:`View ` * - ``void set_cat_categories(const std::vector& cats) override`` - void - pd_series.h:1255 - :ref:`View ` * - ``void set_cat_categories_dtype(const std::string& dtype) override`` - void - pd_series.h:1270 - * - ``void set_cat_ordered(bool ordered) override`` - void - pd_series.h:1267 - :ref:`View ` * - ``void set_datetime_array(std::shared_ptr arr) { datetime_array_ = std::move(arr)`` - void - pd_series.h:1278 - :ref:`View ` * - ``void set_dtype_override(const std::string& dtype_str) override`` - void - pd_series.h:1211 - :ref:`View ` * - ``void set_freq(const std::optional& freq)`` - void - pd_series.h:10537 - :ref:`View ` * - ``void set_mask(const numpy::NDArray& mask)`` - void - pd_series.h:1285 - :ref:`View ` * - ``void set_mixed_tz_array(std::shared_ptr arr) { mixed_tz_array_ = std::move(arr)`` - void - pd_series.h:1283 - :ref:`View ` * - ``void set_multiindex(MultiIndex multi_idx)`` - void - pd_series.h:1501 - :ref:`View ` * - ``void set_name(const std::optional& name) override`` - void - pd_series.h:1315 - :ref:`View ` * - ``void set_name_interval_data(double left, double right, const std::string& closed)`` - void - pd_series.h:1337 - * - ``void set_name_is_int(bool flag)`` - void - pd_series.h:1333 - * - ``void set_name_is_interval(bool flag)`` - void - pd_series.h:1336 - * - ``void set_name_is_timestamp(bool flag)`` - void - pd_series.h:1367 - * - ``void set_name_is_tuple(bool flag)`` - void - pd_series.h:1347 - * - ``void set_name_timestamp_data(int64_t ns, const std::string& tz = "")`` - void - pd_series.h:1368 - * - ``void set_name_tuple_elements(const std::vector& elems)`` - void - pd_series.h:1348 - * - ``void set_name_tuple_elements(std::vector&& elems) { name_tuple_elements_ = std::move(elems)`` - void - pd_series.h:1349 - * - ``void set_sparse_fill_value(double fv) override`` - void - pd_series.h:10534 - * - ``std::vector shape() const override`` - std::vector - pd_series.h:1104 - :ref:`View ` * - ``size_t size() const override`` - size_t - pd_series.h:1090 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4046 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4058 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4078 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4092 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4109 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4282 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4318 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:4364 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:4604 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:4649 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:4684 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:4703 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:4912 - :ref:`View ` * - ``propagate_index_to(q, size())`` - propagate_index_to(q, - pd_series.h:12371 - :ref:`View ` * - ``propagate_index_to(r, size())`` - propagate_index_to(r, - pd_series.h:12372 - :ref:`View ` * - ``propagate_index_to(q, size(), other)`` - propagate_index_to(q, - pd_series.h:12400 - :ref:`View ` * - ``propagate_index_to(r, size(), other)`` - propagate_index_to(r, - pd_series.h:12401 - :ref:`View ` * - ``propagate_index_to(result, size())`` - propagate_index_to(result, - pd_series.h:13800 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:13849 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:13889 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:13915 - :ref:`View ` * - ``propagate_index_to(res, size())`` - propagate_index_to(res, - pd_series.h:13936 - :ref:`View ` * - ``propagate_index_to(q, size())`` - propagate_index_to(q, - pd_series.h:13966 - :ref:`View ` * - ``propagate_index_to(r, size())`` - propagate_index_to(r, - pd_series.h:13967 - :ref:`View ` * - ``SparseAccessor> sparse(double fill_value = 0.0) const`` - SparseAccessor> - pd_series.h:10546 - :ref:`View ` * - ``StringMethods> str() const`` - StringMethods> - pd_series.h:10442 - :ref:`View ` * - ``trim_zeros_column(val_strs)`` - - pd_series.h:10681 - * - ``trim_zeros_column(val_strs)`` - - pd_series.h:11057 - * - ``Series truncate(const std::optional& before = std::nullopt, const std::optional& after = std::nullopt, int axis = 0, bool copy = true) const`` - Series - pd_series.h:6830 - :ref:`View ` * - ``void validate_index_data_alignment() const`` - void - pd_series.h:16039 - * - ``std::unordered_set> value_set(values.begin(), values.end())`` - std::unordered_set> - pd_series.h:9048 - * - ``const numpy::NDArray& values() const`` - const numpy::NDArray& - pd_series.h:1382 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-series-series-0: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-series-1: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-series-2: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-series-3: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-series-4: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-series-5: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-series-6: .. dropdown:: Series (pd_test_1_all.cpp:11161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11151 :emphasize-lines: 11 pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; .. _example-series-at-7: .. dropdown:: at (pd_test_1_all.cpp:6581) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6571 :emphasize-lines: 11 // Test isna/notna with float data { std::map> float_data; float_data["X"] = {1.0, std::nan(""), 3.0}; float_data["Y"] = {4.0, 5.0, std::nan("")}; pandas::DataFrame df_na(float_data); auto na_mask = df_na.isna(); // Row 1, col 0 (X) should be NA if (!na_mask.getElementAt({1, 0})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (1,0) should be true" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: isna at (1,0)"); } // Row 2, col 1 (Y) should be NA if (!na_mask.getElementAt({2, 1})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (2,1) should be true" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: isna at (2,1)"); } // Row 0, col 0 should NOT be NA if (na_mask.getElementAt({0, 0})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (0,0) should be false" << std::endl; .. _example-series-at_time-8: .. dropdown:: at_time (pd_test_2_all.cpp:728) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 718 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_asof test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_asof.cpp (end) ----------------------------- // ------------------- pd_test_at_time.cpp (start) ----------------------------- // dataframe_tests/pd_test_at_time.cpp // Tests for DataFrame.at_time() method (pandas 2.0+ API) // Selects values at particular time of day from datetime-indexed DataFrame #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-first-9: .. dropdown:: first (pd_test_1_all.cpp:11616) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11606 :emphasize-lines: 11 void pd_test_groupby_first_last() { std::cout << "========= GroupBy first/last ===================="; std::map> data = { {"category", {1.0, 1.0, 2.0, 2.0}}, {"value", {10.0, 20.0, 30.0, 40.0}} }; pandas::DataFrame df(data); auto first_result = df.groupby("category").first(); auto last_result = df.groupby("category").last(); // First for group 1: 10, group 2: 30 // Last for group 1: 20, group 2: 40 double first1 = std::stod(first_result["value"].get_value_str(0)); double first2 = std::stod(first_result["value"].get_value_str(1)); bool passed = ((std::abs(first1 - 10.0) < 0.001 && std::abs(first2 - 30.0) < 0.001) || (std::abs(first1 - 30.0) < 0.001 && std::abs(first2 - 10.0) < 0.001)); if (!passed) { .. _example-series-first_valid_index-10: .. dropdown:: first_valid_index (pd_test_1_all.cpp:20555) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20545 :emphasize-lines: 11 std::vector values = { std::numeric_limits::quiet_NaN(), std::numeric_limits::quiet_NaN(), 3.0, 4.0, 5.0 }; pandas::Series s(values, "test"); auto first_idx = s.first_valid_index(); bool passed = first_idx.has_value() && first_idx.value() == 2; if (!passed) { std::cout << " [FAIL] : in pd_test_timeseries_first_valid_index() : expected index 2" << std::endl; throw std::runtime_error("pd_test_timeseries_first_valid_index failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-get-11: .. dropdown:: get (pd_test_1_all.cpp:10290) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10280 :emphasize-lines: 11 void pd_test_extension_index_get_loc_unique() { std::cout << "========= get_loc (unique) ========================="; pandas::CategoricalArray arr({"apple", "banana", "cherry"}); pandas::CategoricalIndex idx(arr); auto loc_apple = idx.get_loc("apple"); auto loc_banana = idx.get_loc("banana"); auto loc_cherry = idx.get_loc("cherry"); bool passed = (std::holds_alternative(loc_apple) && std::get(loc_apple) == 0 && std::get(loc_banana) == 1 && std::get(loc_cherry) == 2); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_get_loc_unique() : get_loc check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_get_loc_unique failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-get_by_label_duplicates-12: .. dropdown:: get_by_label_duplicates (pd_test_3_all.cpp:28133) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28123 :emphasize-lines: 11 if (fail) throw std::runtime_error("pd_test_getitem_timedelta_str_not_found failed"); } void pd_test_getitem_duplicate_labels() { std::cout << " -- pd_test_getitem_duplicate_labels --" << std::endl; int fail = 0; std::vector vals{1.0, 2.0, 3.0, 4.0}; pandas::Series s(vals, "x"); pandas::Index idx(std::vector{"a", "x", "x", "x"}); s.set_index(idx); auto lookup = s.get_by_label_duplicates("x"); if (lookup.count != 3) { std::cout << " FAIL: expected count=3, got " << lookup.count << std::endl; fail++; } if (!lookup.sub_series.has_value()) { std::cout << " FAIL: sub_series missing" << std::endl; fail++; } else if (lookup.sub_series->size() != 3) { std::cout << " FAIL: sub size " << lookup.sub_series->size() << std::endl; fail++; } auto lookup_one = s.get_by_label_duplicates("a"); if (lookup_one.count != 1) { std::cout << " FAIL: expected count=1" << std::endl; fail++; } if (lookup_one.single_index != 0) { std::cout << " FAIL: expected single_index=0" << std::endl; fail++; } auto lookup_zero = s.get_by_label_duplicates("z"); if (lookup_zero.count != 0) { std::cout << " FAIL: expected count=0" << std::endl; fail++; } if (fail == 0) std::cout << " OK" << std::endl; if (fail) throw std::runtime_error("pd_test_getitem_duplicate_labels failed"); .. _example-series-get_cat_categories-13: .. dropdown:: get_cat_categories (pd_test_2_all.cpp:20374) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20364 :emphasize-lines: 11 auto cs = std::make_unique>(svals, "cat"); cs->set_dtype_override("category"); cs->set_cat_categories({"a", "b", "c"}); cs->set_cat_ordered(true); df.insert(0, "cat", std::move(cs), true); auto s = df.get_column_as_string_series("cat"); check(s.dtype_name() == "category", "cat dtype"); check(s.has_cat_categories(), "cat has_categories"); check(s.cat_ordered() == true, "cat ordered"); auto cats = s.get_cat_categories(); check(cats.size() == 3, "cat categories size"); std::set cat_set(cats.begin(), cats.end()); check(cat_set.count("a") && cat_set.count("b") && cat_set.count("c"), "cat categories content"); } void pd_test_getitem_dispatch_index_propagation() { std::cout << "pd_test_getitem_dispatch_index_propagation" << std::endl; // Test DatetimeIndex freq propagation pandas::DataFrame df; .. _example-series-get_freq-14: .. dropdown:: get_freq (pd_test_2_all.cpp:20397) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20387 :emphasize-lines: 11 std::vector ts = { numpy::datetime64(0LL, numpy::DateTimeUnit::Day), numpy::datetime64(1LL, numpy::DateTimeUnit::Day), numpy::datetime64(2LL, numpy::DateTimeUnit::Day) }; auto dt_idx = std::make_unique(ts); dt_idx->set_freq(std::string("D")); df.set_index(std::move(dt_idx)); auto s = df.extract_column_as_numeric_series("val"); check(s.get_freq().has_value(), "freq propagated"); if (s.get_freq().has_value()) { check(s.get_freq().value() == "D", "freq value D"); } // Test MultiIndex propagation pandas::DataFrame df2; std::vector vals2 = {10.0, 20.0}; df2.insert(0, "A", std::make_unique>(vals2, "A"), true); std::vector> arrays = {{"x", "y"}, {"1", "2"}}; std::vector> names = {std::string("first"), std::string("second")}; .. _example-series-get_optional-15: .. dropdown:: get_optional (pd_test_1_all.cpp:6741) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6731 :emphasize-lines: 11 throw std::runtime_error("pd_test_dataframe_index_ops failed: reindex cols"); } } // Test get_optional { std::map> data; data["A"] = {1, 2, 3}; pandas::DataFrame df(data); auto col_opt = df.get_optional("A"); if (!col_opt.has_value()) { std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : get_optional A should exist" << std::endl; throw std::runtime_error("pd_test_dataframe_index_ops failed: get_optional A"); } auto missing = df.get_optional("Z"); if (missing.has_value()) { std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : get_optional Z should not exist" << std::endl; throw std::runtime_error("pd_test_dataframe_index_ops failed: get_optional Z"); } .. _example-series-get_slice_as_series-16: .. dropdown:: get_slice_as_series (pd_test_3_all.cpp:28169) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28159 :emphasize-lines: 11 } if (fail == 0) std::cout << " OK" << std::endl; if (fail) throw std::runtime_error("pd_test_getitem_duplicate_preserves_dtype failed"); } void pd_test_getitem_period_sub_series() { std::cout << " -- pd_test_getitem_period_sub_series --" << std::endl; int fail = 0; std::vector vals{10.0, 20.0, 30.0, 40.0, 50.0}; pandas::Series s(vals, "v"); auto sub = s.get_slice_as_series(1, 4); if (sub.size() != 3) { std::cout << " FAIL: size " << sub.size() << std::endl; fail++; } if (sub[0] != 20.0) { std::cout << " FAIL: [0] " << sub[0] << std::endl; fail++; } if (sub[2] != 40.0) { std::cout << " FAIL: [2] " << sub[2] << std::endl; fail++; } if (fail == 0) std::cout << " OK" << std::endl; if (fail) throw std::runtime_error("pd_test_getitem_period_sub_series failed"); } int pd_test_getitem_edge_main_impl() { std::cout << "====================================== pd_test_getitem_edge ==============================" << std::endl; int fail = 0; .. _example-series-get_string-17: .. dropdown:: get_string (pd_test_3_all.cpp:27746) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27736 :emphasize-lines: 11 } } pandas::Series si({10, 20, 30}, "ints"); auto result2 = si.astype("str"); auto* str_s2 = dynamic_cast*>(result2.get()); if (!str_s2) { std::cout << " FAIL: expected Series from int" << std::endl; fail++; } else { if (str_s2->get_string(0) != "10") { std::cout << " FAIL: expected '10', got '" << str_s2->get_string(0) << "'" << std::endl; fail++; } } if (fail == 0) std::cout << " OK" << std::endl; } void pd_test_astype_datetime_to_string() { std::cout << " -- pd_test_astype_datetime_to_string --" << std::endl; .. _example-series-get_value_bool-18: .. dropdown:: get_value_bool (pd_test_5_all.cpp:35197) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 35187 :emphasize-lines: 11 df.add_column_nullable("X", {true, pandas::NA_BOOL, false}); pandas_tests::check(df["X"].get_value_double(0) == 1.0, "case_2.idx0_one", local_fail); pandas_tests::check(std::isnan(df["X"].get_value_double(1)), "case_2.idx1_nan", local_fail); pandas_tests::check(df["X"].get_value_double(2) == 0.0, "case_2.idx2_zero", local_fail); } void bool_nullable_826495_case_3_get_value_bool_mask_aware(int& local_fail) { pandas::DataFrame df; df.add_column_nullable("X", {true, pandas::NA_BOOL, false}); pandas_tests::check(df["X"].get_value_bool(0) == true, "case_3.idx0_true", local_fail); pandas_tests::check(df["X"].get_value_bool(1) == false, "case_3.idx1_NA_false", local_fail); pandas_tests::check(df["X"].get_value_bool(2) == false, "case_3.idx2_false", local_fail); } void bool_nullable_826495_case_4_is_na_at_mask_aware(int& local_fail) { pandas::DataFrame df; df.add_column_nullable("X", {true, pandas::NA_BOOL, false}); pandas_tests::check(df["X"].is_na_at(0) == false, "case_4.idx0_not_na", local_fail); pandas_tests::check(df["X"].is_na_at(1) == true, "case_4.idx1_is_na", local_fail); pandas_tests::check(df["X"].is_na_at(2) == false, "case_4.idx2_not_na", local_fail); .. _example-series-get_value_double-19: .. dropdown:: get_value_double (pd_test_2_all.cpp:19160) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19150 :emphasize-lines: 11 std::map col_funcs; col_funcs["a"] = "sum"; col_funcs["b"] = "mean"; pandas::Series result = df.agg_to_series(col_funcs); // a.sum() = 10.0, b.mean() = 25.0 check(result.size() == 2, "result_size_2"); // std::map iterates in alphabetical order: a, b check(std::abs(result.get_value_double(0) - 10.0) < 1e-9, "a_sum_10"); check(std::abs(result.get_value_double(1) - 25.0) < 1e-9, "b_mean_25"); // Check index labels check(result.index().get_value_str(0) == "a", "index_0_a"); check(result.index().get_value_str(1) == "b", "index_1_b"); } void pd_test_agg_dispatch_dict_simple_single_col() { std::cout << " -- pd_test_agg_dispatch_dict_simple_single_col --" << std::endl; .. _example-series-get_value_str-20: .. 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-series-get_value_str-21: .. 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-series-head-22: .. dropdown:: head (pd_test_1_all.cpp:6301) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6291 :emphasize-lines: 11 void pd_test_dataframe_indexing() { std::cout << "========= indexing (loc/iloc) =============="; std::map> data; data["A"] = {10.0, 20.0, 30.0, 40.0, 50.0}; data["B"] = {1.0, 2.0, 3.0, 4.0, 5.0}; pandas::DataFrame df(data); // Test head auto head_df = df.head(3); if (head_df.nrows() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_indexing() : head(3) nrows != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_indexing failed: head(3) nrows != 3"); } // Test tail auto tail_df = df.tail(2); if (tail_df.nrows() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_indexing() : tail(2) nrows != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_indexing failed: tail(2) nrows != 2"); .. _example-series-iat-23: .. dropdown:: iat (pd_test_1_all.cpp:22028) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22018 :emphasize-lines: 11 pandas::DataFrame result = df.where(cond, -1.0); // Get column index for A - it's sorted alphabetically in std::map size_t col_a_idx = df.get_column_index("A"); size_t col_b_idx = df.get_column_index("B"); bool passed = true; std::string error_msg; // Check A column values std::string a0 = result.iat(0, col_a_idx) == -1.0 ? "ok" : "fail"; std::string a1 = result.iat(1, col_a_idx) == -1.0 ? "ok" : "fail"; std::string a2 = result.iat(2, col_a_idx) == 3.0 ? "ok" : "fail"; std::string a3 = result.iat(3, col_a_idx) == 4.0 ? "ok" : "fail"; if (a0 != "ok" || a1 != "ok" || a2 != "ok" || a3 != "ok") { passed = false; error_msg = "Column A values incorrect: A[0]=" + a0 + ", A[1]=" + a1 + ", A[2]=" + a2 + ", A[3]=" + a3; } .. _example-series-iat_resolved-24: .. dropdown:: iat_resolved (pd_test_5_all.cpp:92945) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 92935 :emphasize-lines: 11 pandas_tests::check(result.iat(0) == 1.0, "f_test_25_loc_list_positions_happy_553011.iat0_eq_1", local_fail); pandas_tests::check(result.iat(2) == 5.0, "f_test_25_loc_list_positions_happy_553011.iat2_eq_5", local_fail); } void case_23_iat_resolved_positive() { std::cout << "-- case_23_iat_resolved_positive()\n"; int local_fail = 0; auto s = make_f64_series_5(); pandas_tests::check(s.iat_resolved(0) == 1.0, "f_test_25_iat_resolved_positive_446201.idx0_eq_1", local_fail); pandas_tests::check(s.iat_resolved(2) == 3.0, "f_test_25_iat_resolved_positive_446201.idx2_eq_3", local_fail); pandas_tests::check(s.iat_resolved(4) == 5.0, "f_test_25_iat_resolved_positive_446201.idx4_eq_5", local_fail); } void case_24_iat_resolved_negative_one() { std::cout << "-- case_24_iat_resolved_negative_one()\n"; int local_fail = 0; .. _example-series-idxmax-25: .. dropdown:: idxmax (pd_test_1_all.cpp:23956) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23946 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_ffill_bfill test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_ffill_bfill.cpp (end) ----------------------------- // ------------------- pd_test_idxmax_idxmin.cpp (start) ----------------------------- // dataframe_tests/pd_test_idxmax_idxmin.cpp // Test for DataFrame.idxmax() and idxmin() methods #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { .. _example-series-idxmax_typed-26: .. dropdown:: idxmax_typed (pd_test_3_all.cpp:26151) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26141 :emphasize-lines: 11 // ------------------- pd_test_where_series (end) --------------------- // ------------------- pd_test_idxmax_min_typed (begin) --------------- #include "../pandas/pd_datetime_index.h" namespace dataframe_tests_idxmax_min_typed { void pd_test_idxmax_min_typed_regular_max() { std::cout << " pd_test_idxmax_min_typed_regular_max: "; ::pandas::Series<::numpy::float64> s({1.0, 3.0, 2.0, 5.0, 4.0}); auto [is_dt, data] = s.idxmax_typed(); if (is_dt) throw std::runtime_error("Expected non-datetime result"); if (data.second != "3") throw std::runtime_error("Expected label '3', got '" + data.second + "'"); std::cout << "PASSED" << std::endl; } void pd_test_idxmax_min_typed_regular_min() { std::cout << " pd_test_idxmax_min_typed_regular_min: "; ::pandas::Series<::numpy::float64> s({3.0, 1.0, 2.0, 5.0, 4.0}); auto [is_dt, data] = s.idxmin_typed(); if (is_dt) throw std::runtime_error("Expected non-datetime result"); .. _example-series-idxmin-27: .. dropdown:: idxmin (pd_test_1_all.cpp:23956) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23946 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_ffill_bfill test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_ffill_bfill.cpp (end) ----------------------------- // ------------------- pd_test_idxmax_idxmin.cpp (start) ----------------------------- // dataframe_tests/pd_test_idxmax_idxmin.cpp // Test for DataFrame.idxmax() and idxmin() methods #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { .. _example-series-idxmin_typed-28: .. dropdown:: idxmin_typed (pd_test_3_all.cpp:26160) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26150 :emphasize-lines: 11 ::pandas::Series<::numpy::float64> s({1.0, 3.0, 2.0, 5.0, 4.0}); auto [is_dt, data] = s.idxmax_typed(); if (is_dt) throw std::runtime_error("Expected non-datetime result"); if (data.second != "3") throw std::runtime_error("Expected label '3', got '" + data.second + "'"); std::cout << "PASSED" << std::endl; } void pd_test_idxmax_min_typed_regular_min() { std::cout << " pd_test_idxmax_min_typed_regular_min: "; ::pandas::Series<::numpy::float64> s({3.0, 1.0, 2.0, 5.0, 4.0}); auto [is_dt, data] = s.idxmin_typed(); if (is_dt) throw std::runtime_error("Expected non-datetime result"); if (data.second != "1") throw std::runtime_error("Expected label '1', got '" + data.second + "'"); std::cout << "PASSED" << std::endl; } void pd_test_idxmax_min_typed_datetime_max() { std::cout << " pd_test_idxmax_min_typed_datetime_max: "; // Create DatetimeIndex with 3 timestamps, values [1,3,2] => max at pos 1 ::pandas::Series<::numpy::float64> s({1.0, 3.0, 2.0}); std::vector<::numpy::datetime64> dates; .. _example-series-iloc-29: .. dropdown:: iloc (pd_test_1_all.cpp:19149) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19139 :emphasize-lines: 11 pandas::Series s({10, 20, 30, 40, 50}); // Positional indexing bool passed = s[0] == 10 && s[4] == 50 && s.at(2) == 30; if (!passed) { std::cout << " [FAIL] : in pd_test_series_indexing() : positional indexing failed" << std::endl; throw std::runtime_error("pd_test_series_indexing failed: positional indexing"); } // iloc slice auto slice = s.iloc(1, 4); passed = slice.size() == 3 && slice[0] == 20 && slice[2] == 40; if (!passed) { std::cout << " [FAIL] : in pd_test_series_indexing() : iloc slice failed" << std::endl; throw std::runtime_error("pd_test_series_indexing failed: iloc slice"); } // iloc with indices auto selected = s.iloc({0, 2, 4}); passed = selected.size() == 3 && selected[0] == 10 && selected[1] == 30 && selected[2] == 50; if (!passed) { .. _example-series-iloc-30: .. dropdown:: iloc (pd_test_1_all.cpp:19149) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19139 :emphasize-lines: 11 pandas::Series s({10, 20, 30, 40, 50}); // Positional indexing bool passed = s[0] == 10 && s[4] == 50 && s.at(2) == 30; if (!passed) { std::cout << " [FAIL] : in pd_test_series_indexing() : positional indexing failed" << std::endl; throw std::runtime_error("pd_test_series_indexing failed: positional indexing"); } // iloc slice auto slice = s.iloc(1, 4); passed = slice.size() == 3 && slice[0] == 20 && slice[2] == 40; if (!passed) { std::cout << " [FAIL] : in pd_test_series_indexing() : iloc slice failed" << std::endl; throw std::runtime_error("pd_test_series_indexing failed: iloc slice"); } // iloc with indices auto selected = s.iloc({0, 2, 4}); passed = selected.size() == 3 && selected[0] == 10 && selected[1] == 30 && selected[2] == 50; if (!passed) { .. _example-series-last-31: .. dropdown:: last (pd_test_1_all.cpp:11617) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11607 :emphasize-lines: 11 void pd_test_groupby_first_last() { std::cout << "========= GroupBy first/last ===================="; std::map> data = { {"category", {1.0, 1.0, 2.0, 2.0}}, {"value", {10.0, 20.0, 30.0, 40.0}} }; pandas::DataFrame df(data); auto first_result = df.groupby("category").first(); auto last_result = df.groupby("category").last(); // First for group 1: 10, group 2: 30 // Last for group 1: 20, group 2: 40 double first1 = std::stod(first_result["value"].get_value_str(0)); double first2 = std::stod(first_result["value"].get_value_str(1)); bool passed = ((std::abs(first1 - 10.0) < 0.001 && std::abs(first2 - 30.0) < 0.001) || (std::abs(first1 - 30.0) < 0.001 && std::abs(first2 - 10.0) < 0.001)); if (!passed) { std::cout << " [FAIL] : in pd_test_groupby_first_last() : first values incorrect" << std::endl; .. _example-series-last_valid_index-32: .. dropdown:: last_valid_index (pd_test_1_all.cpp:20579) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20569 :emphasize-lines: 11 std::vector values = { 1.0, 2.0, 3.0, std::numeric_limits::quiet_NaN(), std::numeric_limits::quiet_NaN() }; pandas::Series s(values, "test"); auto last_idx = s.last_valid_index(); bool passed = last_idx.has_value() && last_idx.value() == 2; if (!passed) { std::cout << " [FAIL] : in pd_test_timeseries_last_valid_index() : expected index 2" << std::endl; throw std::runtime_error("pd_test_timeseries_last_valid_index failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-loc-33: .. dropdown:: loc (pd_test_3_all.cpp:10916) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10906 :emphasize-lines: 11 {{"A","A","B"}, {"x","y","x"}}); df.set_index(mi); if (!df.has_multiindex() || df.multiindex().nlevels() != 2) { std::cout << " [FAIL] : in pd_test_3_all_set_index_multiindex()" << std::endl; throw std::runtime_error("set_index MultiIndex failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_loc_single_arg() { std::cout << "========= Fix 3: loc(string) MultiIndex ================="; pandas::DataFrame df; df.add_column("val", {10, 20, 30, 40}); auto mi = pandas::MultiIndex::from_arrays( {{"London","London","Paris","Paris"}, {"2020","2021","2020","2021"}}); df.set_multiindex(mi); pandas::DataFrame result = df.loc("London"); if (result.nrows() != 2) { std::cout << " [FAIL] : in pd_test_3_all_loc_single_arg() : expected 2 rows" << std::endl; throw std::runtime_error("loc single-arg failed"); } .. _example-series-loc-34: .. dropdown:: loc (pd_test_3_all.cpp:10916) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10906 :emphasize-lines: 11 {{"A","A","B"}, {"x","y","x"}}); df.set_index(mi); if (!df.has_multiindex() || df.multiindex().nlevels() != 2) { std::cout << " [FAIL] : in pd_test_3_all_set_index_multiindex()" << std::endl; throw std::runtime_error("set_index MultiIndex failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_loc_single_arg() { std::cout << "========= Fix 3: loc(string) MultiIndex ================="; pandas::DataFrame df; df.add_column("val", {10, 20, 30, 40}); auto mi = pandas::MultiIndex::from_arrays( {{"London","London","Paris","Paris"}, {"2020","2021","2020","2021"}}); df.set_multiindex(mi); pandas::DataFrame result = df.loc("London"); if (result.nrows() != 2) { std::cout << " [FAIL] : in pd_test_3_all_loc_single_arg() : expected 2 rows" << std::endl; throw std::runtime_error("loc single-arg failed"); } .. _example-series-loc_slice-35: .. dropdown:: loc_slice (pd_test_5_all.cpp:92767) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 92757 :emphasize-lines: 11 void case_13_loc_slice_none_bounds() { // start=None, stop=None, step=+1: full copy. std::cout << "-- case_13_loc_slice_none_bounds()\n"; int local_fail = 0; auto s = make_f64_series_5(); pandas::SliceSpec spec; spec.start = std::monostate{}; spec.stop = std::monostate{}; spec.step = 1; auto result = s.loc_slice(spec); pandas_tests::check(result.size() == 5, "f_test_25_loc_slice_none_bounds_310099.size_eq_5", local_fail); pandas_tests::check(result.iat(0) == 1.0, "f_test_25_loc_slice_none_bounds_310099.iat0_eq_1", local_fail); pandas_tests::check(result.iat(4) == 5.0, "f_test_25_loc_slice_none_bounds_310099.iat4_eq_5", local_fail); } void case_14_loc_slice_int_bounds() { // start=int(1), stop=int(4), step=+1: positions [1,4) -> 3 elements. .. _example-series-loc_string-36: .. dropdown:: loc_string (pd_test_5_all.cpp:90637) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 90627 :emphasize-lines: 11 throw std::runtime_error(case_label + " failed"); } std::cout << " " << case_label << " -> OK\n"; } void case_1_plain_string_index_exact() { std::cout << "-- case_1_plain_string_index_exact\n"; auto s = make_float_series_with_str_index({10.0, 20.0, 30.0}, {"a", "b", "c"}); int local_fail = 0; pandas::StringLookupResult r = s.loc_string("b"); pandas_tests::check(r.kind == pandas::StringLookupResult::Kind::SCALAR, "case_1.kind_is_SCALAR", local_fail); pandas_tests::check(r.scalar_index == 1, "case_1.scalar_index_eq_1", local_fail); pandas_tests::check(r.range == std::make_pair(0, 0), "case_1.range_unused_default", local_fail); finish_case("case_1_plain_string_index_exact", local_fail); } void case_2_plain_string_index_miss() { .. _example-series-loc_timedelta-37: .. dropdown:: loc_timedelta (pd_test_5_all.cpp:92739) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 92729 :emphasize-lines: 11 "f_test_25_loc_timestamp_edge_first_996311.kind_scalar", local_fail); pandas_tests::check(r.scalar == 10.0, "f_test_25_loc_timestamp_edge_first_996311.scalar_eq_10", local_fail); } void case_11_loc_timedelta_exact_hit() { std::cout << "-- case_11_loc_timedelta_exact_hit()\n"; int local_fail = 0; auto s = make_tdi_f64_series_3(); pandas::Timedelta key(2, 0); // 2 days auto r = s.loc_timedelta(key); pandas_tests::check( r.kind == pandas::LookupResult::Kind::SCALAR, "f_test_25_loc_timedelta_exact_hit_421003.kind_scalar", local_fail); pandas_tests::check(r.scalar == 200.0, "f_test_25_loc_timedelta_exact_hit_421003.scalar_eq_200", local_fail); } void case_12_loc_timedelta_not_found() { std::cout << "-- case_12_loc_timedelta_not_found()\n"; int local_fail = 0; .. _example-series-loc_timestamp-38: .. dropdown:: loc_timestamp (pd_test_5_all.cpp:92701) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 92691 :emphasize-lines: 11 pandas_tests::check(threw, "f_test_25_filter_bool_series_mismatch_throws_172339.value_error", local_fail); } void case_8_loc_timestamp_exact_hit() { std::cout << "-- case_8_loc_timestamp_exact_hit()\n"; int local_fail = 0; auto s = make_dti_f64_series_3(); pandas::Timestamp key(2024, 1, 2, 0, 0, 0); auto r = s.loc_timestamp(key); pandas_tests::check( r.kind == pandas::LookupResult::Kind::SCALAR, "f_test_25_loc_timestamp_exact_hit_318227.kind_scalar", local_fail); pandas_tests::check(r.scalar == 20.0, "f_test_25_loc_timestamp_exact_hit_318227.scalar_eq_20", local_fail); } void case_9_loc_timestamp_not_found() { std::cout << "-- case_9_loc_timestamp_not_found()\n"; int local_fail = 0; .. _example-series-mask-39: .. dropdown:: mask (pd_test_1_all.cpp:9119) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9109 :emphasize-lines: 11 void pd_test_datetime_mixin_array_constructor() { std::cout << "========= DatetimeTDMixin array constructor ========================="; // Create DatetimeArray with some values numpy::NDArray data(std::vector{3}); data.setElementAt({0}, numpy::datetime64(1000000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2001 data.setElementAt({1}, numpy::datetime64(1500000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2017 data.setElementAt({2}, numpy::datetime64(1600000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2020 numpy::NDArray mask(std::vector{3}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); mask.setElementAt({2}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeTDMixin idx(arr, "timestamps"); bool passed = (idx.size() == 3 && !idx.empty() && idx.name().has_value() && *idx.name() == "timestamps" && idx.inferred_type() == "datetime"); .. _example-series-mask-40: .. dropdown:: mask (pd_test_1_all.cpp:9119) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9109 :emphasize-lines: 11 void pd_test_datetime_mixin_array_constructor() { std::cout << "========= DatetimeTDMixin array constructor ========================="; // Create DatetimeArray with some values numpy::NDArray data(std::vector{3}); data.setElementAt({0}, numpy::datetime64(1000000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2001 data.setElementAt({1}, numpy::datetime64(1500000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2017 data.setElementAt({2}, numpy::datetime64(1600000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2020 numpy::NDArray mask(std::vector{3}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); mask.setElementAt({2}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeTDMixin idx(arr, "timestamps"); bool passed = (idx.size() == 3 && !idx.empty() && idx.name().has_value() && *idx.name() == "timestamps" && idx.inferred_type() == "datetime"); .. _example-series-mask_at-41: .. dropdown:: mask_at (pd_test_3_all.cpp:27712) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27702 :emphasize-lines: 11 fail++; } else { if (bool_s->dtype_name() != "boolean") { std::cout << " FAIL: dtype should be boolean, got " << bool_s->dtype_name() << std::endl; fail++; } if (!bool_s->has_mask()) { std::cout << " FAIL: should have mask for NA" << std::endl; fail++; } else { if (!bool_s->mask_at(2)) { std::cout << " FAIL: position 2 should be masked (NA)" << std::endl; fail++; } } } if (fail == 0) std::cout << " OK" << std::endl; } void pd_test_astype_to_string() { .. _example-series-nlargest-42: .. dropdown:: nlargest (pd_test_1_all.cpp:6425) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6415 :emphasize-lines: 11 // Test sort_values descending auto sorted_desc = df.sort_values("A", false); first_val = sorted_desc["A"].get_value_str(0); if (std::stod(first_val) != 5.0) { std::cout << " [FAIL] : in pd_test_dataframe_sorting() : sort_values desc first != 5" << std::endl; throw std::runtime_error("pd_test_dataframe_sorting failed: sort_values desc first != 5"); } // Test nlargest auto largest = df.nlargest(2, "A"); if (largest.nrows() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_sorting() : nlargest nrows != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_sorting failed: nlargest nrows != 2"); } // Test nsmallest auto smallest = df.nsmallest(2, "A"); if (smallest.nrows() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_sorting() : nsmallest nrows != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_sorting failed: nsmallest nrows != 2"); .. _example-series-nsmallest-43: .. dropdown:: nsmallest (pd_test_1_all.cpp:6432) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6422 :emphasize-lines: 11 } // Test nlargest auto largest = df.nlargest(2, "A"); if (largest.nrows() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_sorting() : nlargest nrows != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_sorting failed: nlargest nrows != 2"); } // Test nsmallest auto smallest = df.nsmallest(2, "A"); if (smallest.nrows() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_sorting() : nsmallest nrows != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_sorting failed: nsmallest nrows != 2"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // Test: Rank .. _example-series-sample-44: .. dropdown:: sample (pd_test_3_all.cpp:207) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 197 :emphasize-lines: 11 if (df.index().dtype_name() != "int64") { std::cout << " [FAIL] : in pd_test_3_all_dtype_typing_overloads() : cookbook_105 integration dtype" << std::endl; throw std::runtime_error("pd_test_3_all_dtype_typing_overloads failed: cookbook_105 integration dtype"); } } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_sample() { std::cout << "========= DataFrame.sample() ======================="; std::map> data = { {"A", {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}}, {"B", {10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0}} }; pandas::DataFrame df(data); // Sample 3 rows pandas::DataFrame result = df.sample(3, 42); // seed=42 for reproducibility .. _example-series-set_value_nan-45: .. dropdown:: set_value_nan (pd_test_5_all.cpp:18478) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18468 :emphasize-lines: 11 "0 a\n" "1 NaN\n" "2 c"; bool ok = (actual == expected); pandas_tests::check(ok, "where_mask_dtype_promotion_2_503514_case_10_str_col_where_default.to_string", local_fail); if (!ok) dump_diff("case_10", expected, actual); } void where_mask_dtype_promotion_2_503514_case_11_get_value_str_mask_int_renders_NaN(int& local_fail) { pandas::Series s({10, 20, 30}); s.set_value_nan(0); std::string actual = s.get_value_str(0); std::string expected = "NaN"; bool ok = (actual == expected); pandas_tests::check(ok, "where_mask_dtype_promotion_2_503514_case_11_get_value_str_mask_int_renders_NaN (got " + actual + ")", local_fail); bool ok1 = (s.get_value_str(1) == "20"); bool ok2 = (s.get_value_str(2) == "30"); pandas_tests::check(ok1, "case_11.kept_idx1_eq_20", local_fail); .. _example-series-set_value_with_enlarge-46: .. dropdown:: set_value_with_enlarge (pd_test_3_all.cpp:29152) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 29142 :emphasize-lines: 11 static int sm_check(bool cond, const char* msg) { if (!cond) { std::cout << " FAIL: " << msg << std::endl; return 1; } return 0; } void pd_test_setitem_multicolumn_series_enlarge_int() { std::cout << " -- pd_test_setitem_multicolumn_series_enlarge_int --" << std::endl; int fail = 0; std::vector v = {1.0, 2.0, 3.0}; pandas::Series s(v); bool grew = s.set_value_with_enlarge("5", 99.0); fail += sm_check(grew, "enlargement reported"); fail += sm_check(s.size() == 4, "size grew to 4"); fail += sm_check(s[3] == 99.0, "appended value"); auto k = s.keys(); fail += sm_check(k.size() == 4 && k[3] == "5", "label appended"); if (fail == 0) std::cout << " OK" << std::endl; if (fail != 0) throw std::runtime_error("pd_test_setitem_multicolumn_series_enlarge_int failed"); } void pd_test_setitem_multicolumn_series_enlarge_string() { .. _example-series-tail-47: .. dropdown:: tail (pd_test_1_all.cpp:6308) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6298 :emphasize-lines: 11 pandas::DataFrame df(data); // Test head auto head_df = df.head(3); if (head_df.nrows() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_indexing() : head(3) nrows != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_indexing failed: head(3) nrows != 3"); } // Test tail auto tail_df = df.tail(2); if (tail_df.nrows() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_indexing() : tail(2) nrows != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_indexing failed: tail(2) nrows != 2"); } // Test iloc_rows range auto slice = df.iloc_rows(1, 4); if (slice.nrows() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_indexing() : iloc_rows(1,4) nrows != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_indexing failed: iloc_rows(1,4) nrows != 3"); .. _example-series-take-48: .. dropdown:: take (pd_test_1_all.cpp:5903) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5893 :emphasize-lines: 11 // Inherited Operations Tests // ============================================================================ void pd_test_categorical_index_take() { std::cout << "========= inherited take =============================="; pandas::CategoricalArray arr({"a", "b", "c", "d"}); pandas::CategoricalIndex idx(arr); std::vector indices = {0, 2, 3}; pandas::ExtensionIndex taken = idx.take(indices); bool passed = (taken.size() == 3); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_take()" << std::endl; throw std::runtime_error("pd_test_categorical_index_take failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-where-49: .. dropdown:: where (pd_test_1_all.cpp:22018) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22008 :emphasize-lines: 11 data["B"] = {5.0, 6.0, 7.0, 8.0}; pandas::DataFrame df(data); // Create condition DataFrame (values > 2) std::map> cond_data; cond_data["A"] = {false, false, true, true}; // 1<=2, 2<=2, 3>2, 4>2 cond_data["B"] = {true, true, true, true}; // all >2 pandas::DataFrame cond(cond_data); // Apply where with replacement value -1 pandas::DataFrame result = df.where(cond, -1.0); // Get column index for A - it's sorted alphabetically in std::map size_t col_a_idx = df.get_column_index("A"); size_t col_b_idx = df.get_column_index("B"); bool passed = true; std::string error_msg; // Check A column values std::string a0 = result.iat(0, col_a_idx) == -1.0 ? "ok" : "fail"; .. _example-series-where-50: .. dropdown:: where (pd_test_1_all.cpp:22018) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22008 :emphasize-lines: 11 data["B"] = {5.0, 6.0, 7.0, 8.0}; pandas::DataFrame df(data); // Create condition DataFrame (values > 2) std::map> cond_data; cond_data["A"] = {false, false, true, true}; // 1<=2, 2<=2, 3>2, 4>2 cond_data["B"] = {true, true, true, true}; // all >2 pandas::DataFrame cond(cond_data); // Apply where with replacement value -1 pandas::DataFrame result = df.where(cond, -1.0); // Get column index for A - it's sorted alphabetically in std::map size_t col_a_idx = df.get_column_index("A"); size_t col_b_idx = df.get_column_index("B"); bool passed = true; std::string error_msg; // Check A column values std::string a0 = result.iat(0, col_a_idx) == -1.0 ? "ok" : "fail"; .. _example-series-where_resolved_typed-51: .. dropdown:: where_resolved_typed (pd_test_5_all.cpp:144251) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 144241 :emphasize-lines: 11 auto s = mk_str({"a", "b", "c"}, {"0", "1", "2"}); auto r = s.fillna_resolved(pandas::FillValue::of_bool(true)); check_dtype_eq("C_26_case_70_fillna_bool_string()_dtype", result_series_dtype_full(r), "object", local_fail); } void case_71_where_int_int_other(int& local_fail) { std::cout << "-- case_71_where_int_int_other\n"; auto s = mk_f64({10, 20, 30}, {"0", "1", "2"}, "int64"); auto cond = mk_cond_tft(); auto r = s.where_resolved_typed(cond, pandas::FillValue::of_numeric_int(-1.0)); check_dtype_eq("C_26_case_71_where_int_int_other()_dtype", result_series_dtype_full(r), "int64", local_fail); } void case_72_where_int_float_other(int& local_fail) { std::cout << "-- case_72_where_int_float_other\n"; auto s = mk_f64({10, 20, 30}, {"0", "1", "2"}, "int64"); auto cond = mk_cond_tft(); auto r = s.where_resolved_typed(cond, pandas::FillValue::of_numeric(0.5)); check_dtype_eq("C_26_case_72_where_int_float_other()_dtype", .. _example-series-xs-52: .. dropdown:: xs (pd_test_2_all.cpp:18668) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18658 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_tz_localize test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_tz_localize.cpp (end) ----------------------------- // ------------------- pd_test_xs.cpp (start) ----------------------------- // dataframe_tests/pd_test_xs.cpp // Tests for DataFrame xs() (cross-section) implementation #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-drop-53: .. dropdown:: drop (pd_test_1_all.cpp:6558) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6548 :emphasize-lines: 11 if (df.ncols() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : pop ncols != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: pop ncols != 2"); } if (!popped) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : popped is null" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: popped is null"); } // Test drop columns auto dropped = df.drop(std::vector{"B"}, 1); if (dropped.ncols() != 1) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : drop ncols != 1" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: drop ncols != 1"); } // Test rename auto renamed = df.rename_columns(std::map{{"A", "X"}}); if (!renamed.has_column("X")) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : rename failed" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: rename failed"); .. _example-series-drop_duplicates-54: .. dropdown:: drop_duplicates (pd_test_1_all.cpp:6639) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6629 :emphasize-lines: 11 } } // Test drop_duplicates { std::map> dup_data; dup_data["A"] = {1, 1, 2, 2}; dup_data["B"] = {1, 1, 2, 3}; pandas::DataFrame df_dup(dup_data); auto deduped = df_dup.drop_duplicates(); // Rows 0 and 1 are duplicates (A=1, B=1), so should have 3 rows if (deduped.nrows() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : drop_duplicates nrows != 3, got " << deduped.nrows() << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: drop_duplicates"); } } // Test assign { std::map> assign_data; .. _example-series-droplevel-55: .. dropdown:: droplevel (pd_test_1_all.cpp:14428) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14418 :emphasize-lines: 11 void pd_test_multiindex_droplevel() { std::cout << "========= droplevel =================================== "; std::vector> arrays = { {"a", "a", "b"}, {"x", "y", "z"}, {"1", "2", "3"} }; pandas::MultiIndex mi = pandas::MultiIndex::from_arrays(arrays); pandas::MultiIndex dropped = mi.droplevel(1); bool passed = true; if (dropped.nlevels() != 2) { std::cout << " [FAIL] : nlevels should be 2 after drop" << std::endl; passed = false; } // Check remaining levels auto tup = dropped[0]; .. _example-series-dropna-56: .. dropdown:: dropna (pd_test_1_all.cpp:531) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 521 :emphasize-lines: 11 } // Test isna array numpy::NDArray na_mask = arr.isna(); if (na_mask.getSize() != 4) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : isna size != 4" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: isna size != 4"); } // Test dropna pandas::CategoricalArray dropped = arr.dropna(); if (dropped.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : dropna size != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: dropna size != 2"); } // Test fillna (fill with existing category) pandas::CategoricalArray filled = arr.fillna("a"); // 'a' is in categories if (filled.has_na()) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : fillna should have no NA" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: fillna should have no NA"); .. _example-series-pop-57: .. dropdown:: pop (pd_test_1_all.cpp:6547) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6537 :emphasize-lines: 11 pandas::DataFrame df(data); // Test add_column df.add_column("C", {7, 8, 9}); if (df.ncols() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : add_column ncols != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: add_column ncols != 3"); } // Test pop auto popped = df.pop("C"); if (df.ncols() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : pop ncols != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: pop ncols != 2"); } if (!popped) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : popped is null" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: popped is null"); } // Test drop columns .. _example-series-pop-58: .. dropdown:: pop (pd_test_1_all.cpp:6547) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6537 :emphasize-lines: 11 pandas::DataFrame df(data); // Test add_column df.add_column("C", {7, 8, 9}); if (df.ncols() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : add_column ncols != 3" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: add_column ncols != 3"); } // Test pop auto popped = df.pop("C"); if (df.ncols() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : pop ncols != 2" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: pop ncols != 2"); } if (!popped) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : popped is null" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: popped is null"); } // Test drop columns .. _example-series-reindex-59: .. dropdown:: reindex (pd_test_1_all.cpp:6708) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6698 :emphasize-lines: 11 } } // Test reindex rows { std::map> data; data["A"] = {1.0, 2.0, 3.0}; pandas::DataFrame df(data); df = df.set_axis({"x", "y", "z"}, 0); auto reindexed = df.reindex({"x", "z", "w"}, 0); if (reindexed.nrows() != 3) { std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : reindex wrong nrows" << std::endl; throw std::runtime_error("pd_test_dataframe_index_ops failed: reindex nrows"); } // 'w' should have NaN std::string val = reindexed["A"].get_value_str(2); if (!std::isnan(std::stod(val))) { std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : missing label should be NaN" << std::endl; throw std::runtime_error("pd_test_dataframe_index_ops failed: reindex NaN"); } .. _example-series-reindex_dispatch-60: .. dropdown:: reindex_dispatch (pd_test_5_all.cpp:61046) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 61036 :emphasize-lines: 11 for (size_t i = 0; i < n; ++i) out.push_back(std::to_string(i)); return out; } template static std::pair run_numeric_fill_lt29(const std::vector& vals, const std::vector& new_idx, double fill) { auto s = make_series_with_idx_lt29(vals, src_idx_for_lt29(vals.size())); pandas::Result r = s.reindex_dispatch( new_idx, "", pandas::FillValue::of_numeric(fill)); if (std::holds_alternative< std::unique_ptr>>(r.value)) { auto& sp = std::get< std::unique_ptr>>(r.value); auto df = sp->to_frame(std::optional("v")); auto dts = df.dtypes(); return {df.to_string(), dts.empty() ? std::string("") : dts[0]}; .. _example-series-reindex_like-61: .. dropdown:: reindex_like (pd_test_1_all.cpp:6777) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6767 :emphasize-lines: 11 data1["A"] = {1, 2}; data1["B"] = {3, 4}; pandas::DataFrame df1(data1); std::map> data2; data2["B"] = {10, 20, 30}; data2["C"] = {40, 50, 60}; pandas::DataFrame df2(data2); df2 = df2.set_axis({"x", "y", "z"}, 0); auto reindexed = df1.reindex_like(df2); if (reindexed.nrows() != 3 || reindexed.ncols() != 2) { std::cout << " [FAIL] : in pd_test_dataframe_index_ops() : reindex_like wrong shape" << std::endl; throw std::runtime_error("pd_test_dataframe_index_ops failed: reindex_like"); } } std::cout << " -> tests passed" << std::endl; } // ===================================================================== .. _example-series-reindex_promote_object_bool_fill-62: .. dropdown:: reindex_promote_object_bool_fill (pd_test_5_all.cpp:43334) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 43324 :emphasize-lines: 11 df, actual, expected, "object", local_fail); } void f_dtype_object_collapse_groupby_314827_case_13_series_float64_reindex_float_fill_one(int& local_fail) { std::cout << "-- case_13_series_float64_reindex_float_fill_one\n"; pandas::Series s({1.0, 2.0, 3.0}); s.set_index(std::make_unique>( std::vector{"a", "b", "c"})); // c2: invoke the new bool-promote overload directly (mirrors what the // PandasPython binding now does for a Python bool fill_value). auto r = s.reindex_promote_object_bool_fill({"a", "b", "z"}, /*method=*/"", /*bool_fill_value=*/true); pandas::DataFrame df = r.to_frame(std::optional("v")); std::string actual = df.to_string(); std::cout << " nrows=" << df.nrows() << " ncols=" << df.ncols() << "\n"; std::string expected = " v\n" "a 1.0\n" "b 2.0\n" .. _example-series-reindex_with_fill-63: .. dropdown:: reindex_with_fill (pd_test_5_all.cpp:97806) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 97796 :emphasize-lines: 11 // by recording a pass: we don't fail the suite for a cell that // can't even be set up. pandas_tests::check(true, tag + " [override-rejected-by-storage]", local_fail); return; } } std::vector target_keys = has_missing ? std::vector{"a", "b", "z"} : std::vector{"a", "b", "c"}; try { pandas::Result r = s.reindex_with_fill( target_keys, std::string(""), fv, std::nullopt, std::nullopt, {}, {}); Probe p = probe_result(r); pandas_tests::check(p.in_whitelist, tag + " [variant in apply_override_to_result whitelist]", local_fail); pandas_tests::check_str(tag + " [result_override]", expected_override, p.result_override, local_fail); } catch (const std::exception& e) { pandas_tests::check(false, .. _example-series-reindex_with_indexer-64: .. dropdown:: reindex_with_indexer (pd_test_5_all.cpp:40388) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 40378 :emphasize-lines: 11 s.set_dtype_override("boolean"); s.set_freq(std::optional("D")); s.set_string_na_sentinel_disabled(true); // Indexer: identity over the 3 source positions. numpy::NDArray indexer(std::vector{3}); indexer.setElementAt({0}, 0); indexer.setElementAt({1}, 1); indexer.setElementAt({2}, 2); auto base = s.reindex_with_indexer(indexer); pandas_tests::check(base != nullptr, "case7.reindex_with_indexer_nonnull", local_fail); if (!base) return; auto* r = dynamic_cast*>(base.get()); pandas_tests::check(r != nullptr, "case7.reindex_with_indexer_is_Series_int64", local_fail); if (!r) return; // dtype_override propagates (oracle says yes). pandas_tests::check(r->dtype_override().has_value() && .. _example-series-rename-65: .. dropdown:: rename (pd_test_1_all.cpp:5816) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5806 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_rename() { std::cout << "========= rename ======================================"; pandas::CategoricalArray arr({"x", "y"}); pandas::CategoricalIndex idx(arr, "old_name"); pandas::CategoricalIndex renamed = idx.rename("new_name"); bool passed = (renamed.name().has_value() && *renamed.name() == "new_name" && renamed.size() == idx.size() && renamed.categories() == idx.categories()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_rename()" << std::endl; throw std::runtime_error("pd_test_categorical_index_rename failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-rename_axis-66: .. dropdown:: rename_axis (pd_test_1_all.cpp:6760) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6750 :emphasize-lines: 11 throw std::runtime_error("pd_test_dataframe_index_ops failed: get_optional Z"); } } // Test rename_axis { std::map> data; data["A"] = {1, 2, 3}; pandas::DataFrame df(data); auto renamed = df.rename_axis("my_index", 0); // Should not throw } // Test reindex_like { std::map> data1; data1["A"] = {1, 2}; data1["B"] = {3, 4}; pandas::DataFrame df1(data1); .. _example-series-reorder_levels-67: .. dropdown:: reorder_levels (pd_test_1_all.cpp:14495) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14485 :emphasize-lines: 11 void pd_test_multiindex_reorder_levels() { std::cout << "========= reorder_levels ============================== "; std::vector> arrays = { {"a", "b"}, {"x", "y"}, {"1", "2"} }; pandas::MultiIndex mi = pandas::MultiIndex::from_arrays(arrays); pandas::MultiIndex reordered = mi.reorder_levels({2, 0, 1}); bool passed = true; auto tup = reordered[0]; if (tup[0] != "1" || tup[1] != "a" || tup[2] != "x") { std::cout << " [FAIL] : reordered tuple should be ('1', 'a', 'x')" << std::endl; passed = false; } if (!passed) { .. _example-series-replace-68: .. dropdown:: replace (pd_test_1_all.cpp:6623) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6613 :emphasize-lines: 11 } } // Test replace { std::map> float_data; float_data["X"] = {1.0, 2.0, 3.0}; float_data["Y"] = {2.0, 2.0, 4.0}; pandas::DataFrame df_repl(float_data); auto replaced = df_repl.replace(2.0, 99.0); // Check some value was replaced (crude check via string) std::string val_str = replaced.col("X").get_value_str(1); if (val_str.find("99") == std::string::npos) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : replace didn't work" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: replace"); } } // Test drop_duplicates { .. _example-series-replace-69: .. dropdown:: replace (pd_test_1_all.cpp:6623) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6613 :emphasize-lines: 11 } } // Test replace { std::map> float_data; float_data["X"] = {1.0, 2.0, 3.0}; float_data["Y"] = {2.0, 2.0, 4.0}; pandas::DataFrame df_repl(float_data); auto replaced = df_repl.replace(2.0, 99.0); // Check some value was replaced (crude check via string) std::string val_str = replaced.col("X").get_value_str(1); if (val_str.find("99") == std::string::npos) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : replace didn't work" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: replace"); } } // Test drop_duplicates { .. _example-series-reset_index-70: .. 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-series-set_axis-71: .. dropdown:: set_axis (pd_test_1_all.cpp:6673) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6663 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // ===================================================================== // Test: Index Operations // ===================================================================== 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"); .. _example-series-set_index-72: .. 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-series-set_index-73: .. 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-series-swaplevel-74: .. dropdown:: swaplevel (pd_test_1_all.cpp:14461) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14451 :emphasize-lines: 11 void pd_test_multiindex_swaplevel() { std::cout << "========= swaplevel =================================== "; std::vector> arrays = { {"a", "b"}, {"x", "y"} }; std::vector> names = {"first", "second"}; pandas::MultiIndex mi = pandas::MultiIndex::from_arrays(arrays, names); pandas::MultiIndex swapped = mi.swaplevel(0, 1); bool passed = true; // Tuple should be reversed auto tup = swapped[0]; if (tup[0] != "x" || tup[1] != "a") { std::cout << " [FAIL] : swapped tuple should be ('x', 'a')" << std::endl; passed = false; } .. _example-series-update-75: .. dropdown:: update (pd_test_1_all.cpp:13945) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13935 :emphasize-lines: 11 if (!result.has_column("C")) { passed = false; std::cout << " [FAIL] : in pd_test_joining_combine_first() : missing column C" << std::endl; throw std::runtime_error("pd_test_joining_combine_first failed: col C missing"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // update() Tests // ===================================================================== void pd_test_joining_update() { std::cout << "========= update ======================================"; std::map> left_data = { {"A", {1.0, 2.0, 3.0}}, {"B", {10.0, 20.0, 30.0}} }; std::vector left_idx = {"x", "y", "z"}; .. _example-series-backfill-76: .. dropdown:: backfill (pd_test_3_all.cpp:2645) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2635 :emphasize-lines: 11 void pd_test_3_all_df_backfill_pad() { std::cout << "========= DataFrame.backfill/pad() ======================="; std::map> data = { {"A", {1.0, std::nan(""), std::nan(""), 4.0}}, {"B", {std::nan(""), 2.0, std::nan(""), 4.0}} }; pandas::DataFrame df(data); // Test backfill (should fill backward) pandas::DataFrame bfill_result = df.backfill(0); if (bfill_result.nrows() != 4 || bfill_result.ncols() != 2) { throw std::runtime_error("backfill shape failed"); } // Test pad (should fill forward) pandas::DataFrame pad_result = df.pad(0); if (pad_result.nrows() != 4 || pad_result.ncols() != 2) { throw std::runtime_error("pad shape failed"); } .. _example-series-bfill-77: .. dropdown:: bfill (pd_test_1_all.cpp:23603) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23593 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_equals test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_equals.cpp (end) ----------------------------- // ------------------- pd_test_ffill_bfill.cpp (start) ----------------------------- // dataframe_tests/pd_test_ffill_bfill.cpp // Test file for DataFrame.ffill() and DataFrame.bfill() methods #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-ffill-78: .. dropdown:: ffill (pd_test_1_all.cpp:23603) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23593 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_equals test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_equals.cpp (end) ----------------------------- // ------------------- pd_test_ffill_bfill.cpp (start) ----------------------------- // dataframe_tests/pd_test_ffill_bfill.cpp // Test file for DataFrame.ffill() and DataFrame.bfill() methods #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-fillna-79: .. dropdown:: fillna (pd_test_1_all.cpp:537) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 527 :emphasize-lines: 11 throw std::runtime_error("pd_test_categorical_array_na_handling failed: isna size != 4"); } // Test dropna pandas::CategoricalArray dropped = arr.dropna(); if (dropped.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : dropna size != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: dropna size != 2"); } // Test fillna (fill with existing category) pandas::CategoricalArray filled = arr.fillna("a"); // 'a' is in categories if (filled.has_na()) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : fillna should have no NA" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: fillna should have no NA"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_array_add_categories() { .. _example-series-fillna_resolved-80: .. dropdown:: fillna_resolved (pd_test_5_all.cpp:100541) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 100531 :emphasize-lines: 11 void case_3_object_fillna_nan_no_missing(int& local_fail) { std::cout << "-- case_3_object_fillna_nan_no_missing\n"; pandas::Series s( std::vector{ numpy::object_(std::string("a")), numpy::object_(std::string("b")), numpy::object_(std::string("c"))}, std::optional{}); pandas::Result r; try { r = s.fillna_resolved(pandas::FillValue::nan()); } catch (const std::exception& e) { std::cout << " exception: " << e.what() << "\n"; } pandas_tests::check(r.is_series(), "C_26m_case_3_object_fillna_nan_no_missing()_is_series", local_fail); pandas_tests::check(result_object_series_dtype_full(r) == "object", "C_26m_case_3_object_fillna_nan_no_missing()_dtype_object", local_fail); } void case_4_object_fillna_na_with_sentinel(int& local_fail) { .. _example-series-fillna_string-81: .. dropdown:: fillna_string (pd_test_5_all.cpp:47965) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 47955 :emphasize-lines: 11 "NaT", "null", "", "x", ""}); auto& col = df["col"]; for (size_t r = 0; r < df.nrows(); ++r) { std::cout << tag << " [" << r << "] val=\"" << col.get_value_str(r) << "\" is_na_at=" << col.is_na_at(r) << "\n"; } // CROSS-REFERENCE: pd_series.h:1938 lists only ""/None/nan/NaN as NA // for Series; "NA"/"NaT"/"null"/"" are NOT treated // as NA by is_na_at. This interacts with the fillna bug (item #1): // fillna_string (pd_series.h:1995) shares the SAME list. } catch (const std::exception& e) { std::cout << tag << " exception: " << e.what() << "\n"; } std::cout << tag << " === end ===\n"; } static void P33_forced_object_sentinels() { const std::string tag = "[P33]"; std::cout << "\n" << tag << " === dtype='object' with 'NaT'/'null' literals (residual bug?) ===\n"; .. _example-series-interpolate-82: .. dropdown:: interpolate (pd_test_1_all.cpp:24365) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24355 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_idxmax_idxmin test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_idxmax_idxmin.cpp (end) ----------------------------- // ------------------- pd_test_interpolate.cpp (start) ----------------------------- // dataframe_tests/pd_test_interpolate.cpp // Test file for DataFrame.interpolate() method #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-isna-83: .. dropdown:: isna (pd_test_1_all.cpp:524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 514 :emphasize-lines: 11 throw std::runtime_error("pd_test_categorical_array_na_handling failed: has_na() should be true"); } // Test count (non-NA) if (arr.count() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : count() != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: count() != 2"); } // Test isna array numpy::NDArray na_mask = arr.isna(); if (na_mask.getSize() != 4) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : isna size != 4" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: isna size != 4"); } // Test dropna pandas::CategoricalArray dropped = arr.dropna(); if (dropped.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_na_handling() : dropna size != 2" << std::endl; throw std::runtime_error("pd_test_categorical_array_na_handling failed: dropna size != 2"); .. _example-series-isnull-84: .. dropdown:: isnull (pd_test_3_all.cpp:671) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 661 :emphasize-lines: 11 // Category 5: Index Null Detection // ============================================================================ void pd_test_3_all_index_null_detection() { std::cout << "========= Index.isnull/notnull() ====================="; // Test with float index (can have NaN) std::vector vals = {1.0, std::nan(""), 3.0, std::nan("")}; pandas::Index idx(vals); numpy::NDArray isnull_result = idx.isnull(); if (isnull_result.getSize() != 4) { std::cout << " [FAIL] : in pd_test_3_all_index_null_detection() : isnull() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_index_null_detection failed: isnull() size"); } // Index 0: 1.0 -> not null if (isnull_result.getElementAt({0})) { std::cout << " [FAIL] : in pd_test_3_all_index_null_detection() : index 0 should not be null" << std::endl; throw std::runtime_error("pd_test_3_all_index_null_detection failed: index 0"); } // Index 1: NaN -> null .. _example-series-notna-85: .. dropdown:: notna (pd_test_1_all.cpp:6595) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6585 :emphasize-lines: 11 if (!na_mask.getElementAt({2, 1})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (2,1) should be true" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: isna at (2,1)"); } // Row 0, col 0 should NOT be NA if (na_mask.getElementAt({0, 0})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : isna at (0,0) should be false" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: isna at (0,0)"); } auto notna_mask = df_na.notna(); if (notna_mask.getElementAt({1, 0})) { std::cout << " [FAIL] : in pd_test_dataframe_manipulation() : notna at (1,0) should be false" << std::endl; throw std::runtime_error("pd_test_dataframe_manipulation failed: notna at (1,0)"); } } // Test fillna { std::map> float_data; float_data["X"] = {1.0, std::nan(""), 3.0}; .. _example-series-notnull-86: .. dropdown:: notnull (pd_test_3_all.cpp:665) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 655 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 5: Index Null Detection // ============================================================================ void pd_test_3_all_index_null_detection() { std::cout << "========= Index.isnull/notnull() ====================="; // Test with float index (can have NaN) std::vector vals = {1.0, std::nan(""), 3.0, std::nan("")}; pandas::Index idx(vals); numpy::NDArray isnull_result = idx.isnull(); if (isnull_result.getSize() != 4) { std::cout << " [FAIL] : in pd_test_3_all_index_null_detection() : isnull() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_index_null_detection failed: isnull() size"); } .. _example-series-pad-87: .. dropdown:: pad (pd_test_3_all.cpp:1771) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1761 :emphasize-lines: 11 if (result_single.nrows() != 3 || result_single.ncols() != 1) { std::cout << " [FAIL] : in pd_test_3_all_dataframe_unstack() : single col shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_dataframe_unstack failed: single col shape"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_fbbuilder_pad() { std::cout << "========= FBBuilder.pad() (internal) ================="; // Note: FBBuilder.pad() is an internal method for FlatBuffer serialization // It's not the pandas DataFrame.pad() method (which is ffill alias) // This test verifies the to_feather() serialization works, which uses FBBuilder.pad() std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); .. _example-series-count-88: .. 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-series-cummax-89: .. dropdown:: cummax (pd_test_1_all.cpp:5152) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5142 :emphasize-lines: 11 // cummin: [1, 1, 1, 1] auto cmin = df.cummin(); val = cmin["A"].get_value_str(3); passed = std::abs(std::stod(val) - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cummin failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cummin failed"); } // cummax: [1, 2, 3, 4] auto cmax = df.cummax(); val = cmax["A"].get_value_str(2); passed = std::abs(std::stod(val) - 3.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cummax failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cummax failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-cummin-90: .. dropdown:: cummin (pd_test_1_all.cpp:5143) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5133 :emphasize-lines: 11 // cumprod: [1, 2, 6, 24] auto cp = df.cumprod(); val = cp["A"].get_value_str(3); passed = std::abs(std::stod(val) - 24.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cumprod failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cumprod failed"); } // cummin: [1, 1, 1, 1] auto cmin = df.cummin(); val = cmin["A"].get_value_str(3); passed = std::abs(std::stod(val) - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cummin failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cummin failed"); } // cummax: [1, 2, 3, 4] auto cmax = df.cummax(); val = cmax["A"].get_value_str(2); .. _example-series-cumprod-91: .. dropdown:: cumprod (pd_test_1_all.cpp:5134) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5124 :emphasize-lines: 11 // cumsum: [1, 3, 6, 10] auto cs = df.cumsum(); std::string val = cs["A"].get_value_str(2); bool passed = std::abs(std::stod(val) - 6.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cumsum failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cumsum failed"); } // cumprod: [1, 2, 6, 24] auto cp = df.cumprod(); val = cp["A"].get_value_str(3); passed = std::abs(std::stod(val) - 24.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cumprod failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cumprod failed"); } // cummin: [1, 1, 1, 1] auto cmin = df.cummin(); val = cmin["A"].get_value_str(3); .. _example-series-cumsum-92: .. dropdown:: cumsum (pd_test_1_all.cpp:5125) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5115 :emphasize-lines: 11 } void pd_test_arithmetic_dataframe_cumulative() { std::cout << "========= DataFrame cumulative =================="; std::map> data; data["A"] = {1.0, 2.0, 3.0, 4.0}; pandas::DataFrame df(data); // cumsum: [1, 3, 6, 10] auto cs = df.cumsum(); std::string val = cs["A"].get_value_str(2); bool passed = std::abs(std::stod(val) - 6.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_cumulative() : cumsum failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_cumulative failed: cumsum failed"); } // cumprod: [1, 2, 6, 24] auto cp = df.cumprod(); val = cp["A"].get_value_str(3); .. _example-series-describe-93: .. dropdown:: describe (pd_test_2_all.cpp:19793) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19783 :emphasize-lines: 11 ++g_fail; } } static bool approx_eq(double a, double b, double tol = 1e-9) { if (std::isnan(a) && std::isnan(b)) return true; return std::abs(a - b) < tol; } // ===================================================================== // Test: describe() default mode — numeric columns only // ===================================================================== void pd_test_describe_numeric_only() { std::cout << " -- pd_test_describe_numeric_only --" << std::endl; pandas::DataFrame df; df.add_column("A", std::vector{1.0, 2.0, 3.0, 4.0, 5.0}); df.add_column("B", std::vector{10.0, 20.0, 30.0, 40.0, 50.0}); df.add_column("Name", std::vector{"a", "b", "c", "d", "e"}); .. _example-series-describe_as_series-94: .. dropdown:: describe_as_series (pd_test_3_all.cpp:25806) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25796 :emphasize-lines: 11 if (idx2 != "0.75") throw std::runtime_error("Expected index '0.75', got '" + idx2 + "'"); // Check values (quantile of 1,2,3,4,5: q=0.5 should be 3.0) double v1 = result[1]; if (std::abs(v1 - 3.0) > 1e-10) throw std::runtime_error("Expected median 3.0, got " + std::to_string(v1)); std::cout << "PASSED" << std::endl; } void pd_test_series_format_helpers_describe() { std::cout << " describe_as_series... "; ::pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}, "test"); auto result = s.describe_as_series(); // Should have 8 rows: count, mean, std, min, 25%, 50%, 75%, max if (result.size() != 8) throw std::runtime_error("Expected 8 rows, got " + std::to_string(result.size())); // Check count = 5.0 double count_val = result[0]; if (std::abs(count_val - 5.0) > 1e-10) throw std::runtime_error("Expected count=5.0, got " + std::to_string(count_val)); // Check index[0] = "count" std::string idx0 = result.index().get_value_str(0); if (idx0 != "count") throw std::runtime_error("Expected index[0]='count', got '" + idx0 + "'"); std::cout << "PASSED" << std::endl; } .. _example-series-kurt-95: .. dropdown:: kurt (pd_test_1_all.cpp:4599) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4589 :emphasize-lines: 11 std::cout << "========= Series skew/kurt ======================"; pandas::Series s({1.0, 2.0, 2.0, 3.0, 9.0}); auto skew_val = s.skew(); bool passed = skew_val.has_value() && *skew_val > 0; // Should be right-skewed if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_skew_kurt() : skew should be positive" << std::endl; throw std::runtime_error("pd_test_aggregation_series_skew_kurt failed: skew should be positive"); } auto kurt_val = s.kurt(); passed = kurt_val.has_value(); if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_skew_kurt() : kurt should have value" << std::endl; throw std::runtime_error("pd_test_aggregation_series_skew_kurt failed: kurt should have value"); } // Test kurtosis alias auto kurt_alias = s.kurtosis(); passed = kurt_alias.has_value() && std::abs(*kurt_alias - *kurt_val) < 0.0001; if (!passed) { .. _example-series-kurtosis-96: .. dropdown:: kurtosis (pd_test_1_all.cpp:4607) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4597 :emphasize-lines: 11 } auto kurt_val = s.kurt(); passed = kurt_val.has_value(); if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_skew_kurt() : kurt should have value" << std::endl; throw std::runtime_error("pd_test_aggregation_series_skew_kurt failed: kurt should have value"); } // Test kurtosis alias auto kurt_alias = s.kurtosis(); passed = kurt_alias.has_value() && std::abs(*kurt_alias - *kurt_val) < 0.0001; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_skew_kurt() : kurtosis alias failed" << std::endl; throw std::runtime_error("pd_test_aggregation_series_skew_kurt failed: kurtosis alias failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_series_pct_change() { .. _example-series-max-97: .. dropdown:: max (pd_test_1_all.cpp:771) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 761 :emphasize-lines: 11 pandas::CategoricalArray arr = pandas::CategoricalArray::from_codes(codes, cats, true); // ordered // Test min std::optional min_val = arr.min(); if (!min_val.has_value() || *min_val != "low") { std::cout << " [FAIL] : in pd_test_categorical_array_ordered_operations() : min != 'low'" << std::endl; throw std::runtime_error("pd_test_categorical_array_ordered_operations failed: min != 'low'"); } // Test max std::optional max_val = arr.max(); if (!max_val.has_value() || *max_val != "high") { std::cout << " [FAIL] : in pd_test_categorical_array_ordered_operations() : max != 'high'" << std::endl; throw std::runtime_error("pd_test_categorical_array_ordered_operations failed: max != 'high'"); } // Test unordered throws for min/max pandas::CategoricalArray unordered = arr.as_unordered(); bool threw = false; try { unordered.min(); .. _example-series-mean-98: .. dropdown:: mean (pd_test_1_all.cpp:282) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 272 :emphasize-lines: 11 std::optional(true), std::optional(true) }); auto s = arr.sum(); if (!s.has_value() || s.value() != 3) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: sum"); } auto m = arr.mean(); if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl; 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 ======================= "; .. _example-series-median-99: .. dropdown:: median (pd_test_1_all.cpp:20910) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20900 :emphasize-lines: 11 throw std::runtime_error("pd_test_expanding_var failed: expanding var values incorrect"); } std::cout << " -> tests passed" << std::endl; } void pd_test_expanding_median() { std::cout << "========= Expanding median ======================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.expanding().median(); // Expanding median: 1, 1.5, 2, 2.5, 3 bool passed = std::abs(result[0] - 1.0) < 0.001 && std::abs(result[1] - 1.5) < 0.001 && std::abs(result[2] - 2.0) < 0.001 && std::abs(result[3] - 2.5) < 0.001 && std::abs(result[4] - 3.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_expanding_median() : expanding median values incorrect" << std::endl; throw std::runtime_error("pd_test_expanding_median failed: expanding median values incorrect"); .. _example-series-min-100: .. dropdown:: min (pd_test_1_all.cpp:764) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 754 :emphasize-lines: 11 } void pd_test_categorical_array_ordered_operations() { std::cout << "========= CategoricalArray: ordered operations (min/max) ======================= "; std::vector cats = {"low", "medium", "high"}; std::vector codes = {0, 2, 1, 0, -1}; // low, high, medium, low, NA pandas::CategoricalArray arr = pandas::CategoricalArray::from_codes(codes, cats, true); // ordered // Test min std::optional min_val = arr.min(); if (!min_val.has_value() || *min_val != "low") { std::cout << " [FAIL] : in pd_test_categorical_array_ordered_operations() : min != 'low'" << std::endl; throw std::runtime_error("pd_test_categorical_array_ordered_operations failed: min != 'low'"); } // Test max std::optional max_val = arr.max(); if (!max_val.has_value() || *max_val != "high") { std::cout << " [FAIL] : in pd_test_categorical_array_ordered_operations() : max != 'high'" << std::endl; throw std::runtime_error("pd_test_categorical_array_ordered_operations failed: max != 'high'"); .. _example-series-mode-101: .. dropdown:: mode (pd_test_1_all.cpp:4569) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4559 :emphasize-lines: 11 throw std::runtime_error("pd_test_aggregation_series_quantile failed: quantile(1) should be 5.0"); } std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_series_mode() { std::cout << "========= Series mode ==========================="; pandas::Series s({1, 2, 2, 3, 3, 3}); auto m = s.mode(); bool passed = m.size() == 1 && m[0] == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_mode() : mode should be 3" << std::endl; throw std::runtime_error("pd_test_aggregation_series_mode failed: mode should be 3"); } // Test multi-mode pandas::Series s2({1, 1, 2, 2}); auto m2 = s2.mode(); passed = m2.size() == 2; // Both 1 and 2 are modes .. _example-series-nunique-102: .. dropdown:: nunique (pd_test_1_all.cpp:10604) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10594 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_nunique() { std::cout << "========= nunique ========================="; pandas::CategoricalArray arr({"a", "b", "a", "c", "b", std::nullopt}); pandas::CategoricalIndex idx(arr); bool passed = (idx.nunique(true) == 3 && idx.nunique(false) == 4); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_nunique() : nunique check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_nunique failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_factorize() { std::cout << "========= factorize ========================="; .. _example-series-prod-103: .. dropdown:: prod (pd_test_1_all.cpp:26082) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26072 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_pivot_table test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_pivot_table.cpp (end) ----------------------------- // ------------------- pd_test_prod.cpp (start) ----------------------------- // dataframe_tests/pd_test_prod.cpp // Tests for DataFrame.prod() and DataFrame.prod_cols() methods #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { .. _example-series-product-104: .. dropdown:: product (pd_test_3_all.cpp:2584) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2574 :emphasize-lines: 11 // Test quantile along rows pandas::Series q50_rows = df.quantile(0.5, 1); if (q50_rows.size() != 5) { throw std::runtime_error("quantile(0.5, axis=1) failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_df_product() { std::cout << "========= DataFrame.product(axis) ========================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // Test product along columns pandas::Series prod_cols = df.product(0); if (prod_cols.size() != 2 || std::abs(prod_cols[static_cast(0)] - 6.0) > 0.001 || .. _example-series-quantile-105: .. dropdown:: quantile (pd_test_1_all.cpp:4540) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4530 :emphasize-lines: 11 throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_series_quantile() { std::cout << "========= Series quantile ======================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto q50 = s.quantile(0.5); bool passed = q50.has_value() && std::abs(*q50 - 3.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_quantile() : quantile(0.5) should be 3.0" << std::endl; throw std::runtime_error("pd_test_aggregation_series_quantile failed: quantile(0.5) should be 3.0"); } // Test q=0 and q=1 auto q0 = s.quantile(0.0); passed = q0.has_value() && std::abs(*q0 - 1.0) < 0.001; if (!passed) { .. _example-series-quantile_list-106: .. dropdown:: quantile_list (pd_test_3_all.cpp:25788) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25778 :emphasize-lines: 11 std::string r2 = ::pandas::display::format_quantile_label(0.5); if (r2 != "0.5") throw std::runtime_error("Expected '0.5', got '" + r2 + "'"); std::string r3 = ::pandas::display::format_quantile_label(0.75); if (r3 != "0.75") throw std::runtime_error("Expected '0.75', got '" + r3 + "'"); std::cout << "PASSED" << std::endl; } void pd_test_series_format_helpers_quantile_list() { std::cout << " quantile_list... "; ::pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}, "test"); auto result = s.quantile_list({0.25, 0.5, 0.75}); if (result.size() != 3) throw std::runtime_error("Expected 3 values, got " + std::to_string(result.size())); // Check index labels std::string idx0 = result.index().get_value_str(0); std::string idx1 = result.index().get_value_str(1); std::string idx2 = result.index().get_value_str(2); if (idx0 != "0.25") throw std::runtime_error("Expected index '0.25', got '" + idx0 + "'"); if (idx1 != "0.5") throw std::runtime_error("Expected index '0.5', got '" + idx1 + "'"); if (idx2 != "0.75") throw std::runtime_error("Expected index '0.75', got '" + idx2 + "'"); // Check values (quantile of 1,2,3,4,5: q=0.5 should be 3.0) double v1 = result[1]; .. _example-series-sem-107: .. dropdown:: sem (pd_test_1_all.cpp:4525) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4515 :emphasize-lines: 11 #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-skew-108: .. dropdown:: skew (pd_test_1_all.cpp:4592) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4582 :emphasize-lines: 11 throw std::runtime_error("pd_test_aggregation_series_mode failed: multi-mode should return 2 values"); } std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_series_skew_kurt() { std::cout << "========= Series skew/kurt ======================"; pandas::Series s({1.0, 2.0, 2.0, 3.0, 9.0}); auto skew_val = s.skew(); bool passed = skew_val.has_value() && *skew_val > 0; // Should be right-skewed if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_skew_kurt() : skew should be positive" << std::endl; throw std::runtime_error("pd_test_aggregation_series_skew_kurt failed: skew should be positive"); } auto kurt_val = s.kurt(); passed = kurt_val.has_value(); if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_skew_kurt() : kurt should have value" << std::endl; .. _example-series-std_-109: .. dropdown:: std_ (pd_test_1_all.cpp:20752) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20742 :emphasize-lines: 11 throw std::runtime_error("pd_test_rolling_min_periods failed: with min_periods=1, idx 1 should be 3.0"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rolling_std() { std::cout << "========= Rolling std ==========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.rolling(3).std_(); // std([1,2,3]) = 1.0 (ddof=1) // std([2,3,4]) = 1.0 // std([3,4,5]) = 1.0 bool passed = std::abs(result[2] - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_rolling_std() : rolling std should be 1.0" << std::endl; throw std::runtime_error("pd_test_rolling_std failed: rolling std should be 1.0"); } .. _example-series-sum-110: .. dropdown:: sum (pd_test_1_all.cpp:276) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 266 :emphasize-lines: 11 } // Test sum/mean pandas::BooleanArray arr({ std::optional(true), std::optional(false), std::optional(true), std::optional(true) }); auto s = arr.sum(); if (!s.has_value() || s.value() != 3) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: sum"); } auto m = arr.mean(); if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: mean"); } .. _example-series-value_counts-111: .. dropdown:: value_counts (pd_test_1_all.cpp:865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 855 :emphasize-lines: 11 std::vector> values = { std::optional("a"), std::optional("b"), std::optional("a"), std::optional("a"), std::optional("b"), std::nullopt // NA not counted }; pandas::CategoricalArray arr(values); auto [cats, counts] = arr.value_counts(); // Should have 2 categories if (cats.size() != 2 || counts.size() != 2) { std::cout << " [FAIL] : in pd_test_categorical_array_value_counts() : wrong size" << std::endl; throw std::runtime_error("pd_test_categorical_array_value_counts failed: wrong size"); } // Find 'a' count int64_t a_count = 0, b_count = 0; for (size_t i = 0; i < cats.size(); ++i) { .. _example-series-var-112: .. dropdown:: var (pd_test_1_all.cpp:20890) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20880 :emphasize-lines: 11 throw std::runtime_error("pd_test_expanding_std failed: expanding std values incorrect"); } std::cout << " -> tests passed" << std::endl; } void pd_test_expanding_var() { std::cout << "========= Expanding var ========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.expanding().var(); // Expanding var (ddof=1): NaN, 0.5, 1.0, 1.6667, 2.5 bool passed = std::isnan(result[0]) && std::abs(result[1] - 0.5) < 0.001 && std::abs(result[2] - 1.0) < 0.001 && std::abs(result[3] - 1.6667) < 0.001 && std::abs(result[4] - 2.5) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_expanding_var() : expanding var values incorrect" << std::endl; throw std::runtime_error("pd_test_expanding_var failed: expanding var values incorrect"); .. _example-series-agg-113: .. dropdown:: agg (pd_test_1_all.cpp:11100) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11090 :emphasize-lines: 11 } void pd_test_func_apply_series_agg() { std::cout << "========= Series agg =================================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}, "values"); bool passed = true; // Test string-based aggregation auto sum_result = s.agg("sum"); if (!sum_result.has_value() || !approx_equal(sum_result.value(), 15.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : sum failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: sum failed"); } auto mean_result = s.agg("mean"); if (!mean_result.has_value() || !approx_equal(mean_result.value(), 3.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : mean failed" << std::endl; .. _example-series-agg-114: .. dropdown:: agg (pd_test_1_all.cpp:11100) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11090 :emphasize-lines: 11 } void pd_test_func_apply_series_agg() { std::cout << "========= Series agg =================================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}, "values"); bool passed = true; // Test string-based aggregation auto sum_result = s.agg("sum"); if (!sum_result.has_value() || !approx_equal(sum_result.value(), 15.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : sum failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: sum failed"); } auto mean_result = s.agg("mean"); if (!mean_result.has_value() || !approx_equal(mean_result.value(), 3.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : mean failed" << std::endl; .. _example-series-agg-115: .. dropdown:: agg (pd_test_1_all.cpp:11100) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11090 :emphasize-lines: 11 } void pd_test_func_apply_series_agg() { std::cout << "========= Series agg =================================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}, "values"); bool passed = true; // Test string-based aggregation auto sum_result = s.agg("sum"); if (!sum_result.has_value() || !approx_equal(sum_result.value(), 15.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : sum failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: sum failed"); } auto mean_result = s.agg("mean"); if (!mean_result.has_value() || !approx_equal(mean_result.value(), 3.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : mean failed" << std::endl; .. _example-series-agg_with_dtype-116: .. dropdown:: agg_with_dtype (pd_test_5_all.cpp:94652) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 94642 :emphasize-lines: 11 static void run_dfgb_case(const std::string& fn, const std::string& col, const std::string& expected_dtype, const std::string& label, int& local_fail) { pandas::DataFrame df = make_mixed_df(); auto gb = df.groupby("key"); pandas::DataFrame out; std::string err; try { out = gb.agg_with_dtype(fn); } catch (const std::exception& e) { err = e.what(); } catch (...) { err = ""; } pandas_tests::check(err.empty(), label + "_no_throw", local_fail); if (!err.empty()) { std::cout << " err: " << err << "\n"; .. _example-series-aggregate-117: .. dropdown:: aggregate (pd_test_1_all.cpp:11139) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11129 :emphasize-lines: 11 auto custom_agg = s.agg([](const std::vector& v) { return std::accumulate(v.begin(), v.end(), 0.0) / v.size(); }); if (!approx_equal(custom_agg, 3.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : custom agg failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: custom agg failed"); } // Test aggregate alias auto alias_result = s.aggregate("sum"); if (!alias_result.has_value() || !approx_equal(alias_result.value(), 15.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : aggregate alias failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: aggregate alias failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_func_apply_series_pipe() { .. _example-series-aggregate-118: .. dropdown:: aggregate (pd_test_1_all.cpp:11139) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11129 :emphasize-lines: 11 auto custom_agg = s.agg([](const std::vector& v) { return std::accumulate(v.begin(), v.end(), 0.0) / v.size(); }); if (!approx_equal(custom_agg, 3.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : custom agg failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: custom agg failed"); } // Test aggregate alias auto alias_result = s.aggregate("sum"); if (!alias_result.has_value() || !approx_equal(alias_result.value(), 15.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : aggregate alias failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: aggregate alias failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_func_apply_series_pipe() { .. _example-series-aggregate-119: .. dropdown:: aggregate (pd_test_1_all.cpp:11139) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11129 :emphasize-lines: 11 auto custom_agg = s.agg([](const std::vector& v) { return std::accumulate(v.begin(), v.end(), 0.0) / v.size(); }); if (!approx_equal(custom_agg, 3.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : custom agg failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: custom agg failed"); } // Test aggregate alias auto alias_result = s.aggregate("sum"); if (!alias_result.has_value() || !approx_equal(alias_result.value(), 15.0)) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_agg() : aggregate alias failed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_agg failed: aggregate alias failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_func_apply_series_pipe() { .. _example-series-apply-120: .. dropdown:: apply (pd_test_1_all.cpp:11244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11234 :emphasize-lines: 11 void pd_test_func_apply_dataframe_apply_axis0() { std::cout << "========= DataFrame apply axis=0 ======================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // apply axis=0 applies function to each column auto result = df.apply([](const std::vector& col) { return std::accumulate(col.begin(), col.end(), 0.0); }, 0); bool passed = true; // Plan F·dtype: axis=0 reduce now returns a single "result" column // with the original column names ("A", "B") as the row index. // Sum of A: 1+2+3=6, Sum of B: 4+5+6=15 const auto& result_col = result["result"]; double sum_a = std::stod(result_col.get_value_str(0)); .. _example-series-apply-121: .. dropdown:: apply (pd_test_1_all.cpp:11244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11234 :emphasize-lines: 11 void pd_test_func_apply_dataframe_apply_axis0() { std::cout << "========= DataFrame apply axis=0 ======================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // apply axis=0 applies function to each column auto result = df.apply([](const std::vector& col) { return std::accumulate(col.begin(), col.end(), 0.0); }, 0); bool passed = true; // Plan F·dtype: axis=0 reduce now returns a single "result" column // with the original column names ("A", "B") as the row index. // Sum of A: 1+2+3=6, Sum of B: 4+5+6=15 const auto& result_col = result["result"]; double sum_a = std::stod(result_col.get_value_str(0)); .. _example-series-apply-122: .. dropdown:: apply (pd_test_1_all.cpp:11244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11234 :emphasize-lines: 11 void pd_test_func_apply_dataframe_apply_axis0() { std::cout << "========= DataFrame apply axis=0 ======================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // apply axis=0 applies function to each column auto result = df.apply([](const std::vector& col) { return std::accumulate(col.begin(), col.end(), 0.0); }, 0); bool passed = true; // Plan F·dtype: axis=0 reduce now returns a single "result" column // with the original column names ("A", "B") as the row index. // Sum of A: 1+2+3=6, Sum of B: 4+5+6=15 const auto& result_col = result["result"]; double sum_a = std::stod(result_col.get_value_str(0)); .. _example-series-apply_dispatch-123: .. dropdown:: apply_dispatch (pd_test_5_all.cpp:53781) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 53771 :emphasize-lines: 11 << " actual dtype: [" << dts[0] << "]\n"; } } static void f_series_apply_dispatch_502719_case_H1_dispatch_callable_square_int(int& local_fail) { std::cout << "-- case_H1_dispatch_callable_square_int\n"; pandas::Series s({2, 3, 4}, "v"); auto h = dispatch_test_helpers::make_stub_callable( dispatch_test_helpers::test_cell_int_cb([](double x) { return static_cast(x * x); }), "square"); pandas::Result r = s.apply_dispatch(pandas::FuncArg::from_callable_handle(h)); bool got = std::holds_alternative>>(r.value); pandas_tests::check(got, "case_H1.is_series_float64", local_fail); if (!got) return; auto& sp = std::get>>(r.value); bool ok = (sp->size() == 3) && (*sp)[0] == 4.0 && (*sp)[1] == 9.0 && (*sp)[2] == 16.0; pandas_tests::check(ok, "case_H1.values_squared", local_fail); } static void f_series_apply_dispatch_502719_case_H2_dispatch_callable_half_float(int& local_fail) { std::cout << "-- case_H2_dispatch_callable_half_float\n"; .. _example-series-apply_ns_transform-124: .. dropdown:: apply_ns_transform (pd_test_4_all.cpp:6365) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6355 :emphasize-lines: 11 auto neg = me.negate(); auto b = s.add_dateoffset(*neg); EXPECT(a.size() == b.size()); for (size_t i = 0; i < a.size(); ++i) { EXPECT(static_cast(a[i]) == static_cast(b[i])); } } void test_apply_ns_transform_identity() { auto s = make_dt_series({1, 2, 3}); auto out = s.apply_ns_transform([](int64_t x) { return x; }, "datetime64[ns]"); EXPECT(out.size() == 3); for (size_t i = 0; i < 3; ++i) { EXPECT(static_cast(out[i]) == static_cast(s[i])); } EXPECT(out.dtype_name() == "datetime64[ns]"); } void test_tz_aware_calendar_preserves_override() { auto s = make_dt_series({0}); s.set_dtype_override("datetime64[ns, UTC]"); .. _example-series-apply_resolved_typed-125: .. dropdown:: apply_resolved_typed (pd_test_5_all.cpp:98141) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 98131 :emphasize-lines: 11 switch (cid) { case CbId::Int: cb = cb_int(hist); break; case CbId::Bool: cb = cb_bool(hist); break; case CbId::Float: cb = cb_float(hist); break; case CbId::Str: cb = cb_string(hist); break; case CbId::Mixed: cb = cb_mixed(hist); break; } pandas::Result r; try { r = s.apply_resolved_typed(cb, hist); } catch (const std::exception& e) { std::string tag = std::string("apply src=") + src_name(sid) + " cb=" + cb_name(cid) + " mode=" + mode_name(mid); std::cout << "[FAIL] : in f_27a_core_3094022_apply_resolved_typed_post_cb_dtype() " << tag << " unexpected exception: " << e.what() << "\n"; ++pandas_tests::g_failed; ++local_fail; ++pandas_tests::g_failed; ++local_fail; ++pandas_tests::g_failed; ++local_fail; return; } .. _example-series-apply_with_args-126: .. dropdown:: apply_with_args (pd_test_3_all.cpp:16993) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16983 :emphasize-lines: 11 } } if (!passed) { throw std::runtime_error("pd_test_apply_axis1_broadcast failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_apply_with_args() { std::cout << "========= DataFrame.apply_with_args() ================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // Apply with additional argument: multiply sum by factor auto result = df.apply_with_args( [](const std::vector& col, double factor) { .. _example-series-ewm-127: .. dropdown:: ewm (pd_test_3_all.cpp:2961) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2951 :emphasize-lines: 11 // Test expanding sum pandas::DataFrame expanding_sum = df.expanding().sum(); if (expanding_sum.nrows() != 5 || expanding_sum.ncols() != 2) { throw std::runtime_error("expanding().sum() shape failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_df_ewm() { std::cout << "========= DataFrame.ewm() ================================"; std::map> data = { {"A", {1.0, 2.0, 3.0, 4.0, 5.0}}, {"B", {10.0, 20.0, 30.0, 40.0, 50.0}} }; pandas::DataFrame df(data); // Test ewm mean with span=3 pandas::DataFrame ewm_mean = df.ewm(std::nullopt, 3.0).mean(); if (ewm_mean.nrows() != 5 || ewm_mean.ncols() != 2) { .. _example-series-ewm_full-128: .. dropdown:: ewm_full (pd_test_3_all.cpp:9903) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9893 :emphasize-lines: 11 // Test with span using legacy overload auto ewm1 = s.ewm(3.0); auto result1 = ewm1.mean(); if (result1.size() != 5) { std::cout << " [FAIL] : ewm with span returned wrong size" << std::endl; throw std::runtime_error("pd_test_3_all_phase2_ewm_params failed"); } // Test with alpha using ewm_full auto ewm2 = s.ewm_full(std::nullopt, std::nullopt, std::nullopt, 0.5); auto result2 = ewm2.mean(); if (result2.size() != 5) { std::cout << " [FAIL] : ewm with alpha returned wrong size" << std::endl; throw std::runtime_error("pd_test_3_all_phase2_ewm_params failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_phase2_combine_params() { .. _example-series-ewm_span-129: .. dropdown:: ewm_span (pd_test_1_all.cpp:21167) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21157 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_ewm_span() { std::cout << "========= EWM span =============================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); // EWM with span=3 => alpha = 2/(3+1) = 0.5 auto result = s.ewm_span(3.0).mean(); // Check that result has correct size bool passed = result.size() == 5; if (!passed) { std::cout << " [FAIL] : in pd_test_ewm_span() : result size should be 5" << std::endl; throw std::runtime_error("pd_test_ewm_span failed: result size should be 5"); } // First value should be equal to original (no weighting yet) passed = std::abs(result[0] - 1.0) < 0.001; .. _example-series-expanding-130: .. dropdown:: expanding (pd_test_1_all.cpp:20770) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20760 :emphasize-lines: 11 throw std::runtime_error("pd_test_rolling_std failed: rolling std should be 1.0"); } std::cout << " -> tests passed" << std::endl; } void pd_test_expanding_sum() { std::cout << "========= Expanding sum ========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.expanding().sum(); // Cumulative sum: 1, 3, 6, 10, 15 bool passed = std::abs(result[0] - 1.0) < 0.001 && std::abs(result[1] - 3.0) < 0.001 && std::abs(result[2] - 6.0) < 0.001 && std::abs(result[3] - 10.0) < 0.001 && std::abs(result[4] - 15.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_expanding_sum() : expanding sum values incorrect" << std::endl; throw std::runtime_error("pd_test_expanding_sum failed: expanding sum values incorrect"); .. _example-series-groupby-131: .. dropdown:: groupby (pd_test_1_all.cpp:11495) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11485 :emphasize-lines: 11 std::cout << "========= GroupBy basic ========================="; // Create DataFrame with category column std::map> data = { {"category", {1.0, 1.0, 2.0, 2.0, 2.0}}, {"value", {10.0, 20.0, 30.0, 40.0, 50.0}} }; pandas::DataFrame df(data); // Test groupby auto grouped = df.groupby("category"); bool passed = grouped.ngroups() == 2; if (!passed) { std::cout << " [FAIL] : in pd_test_groupby_basic() : ngroups should be 2" << std::endl; throw std::runtime_error("pd_test_groupby_basic failed: ngroups should be 2"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-groupby_by_callable-132: .. dropdown:: groupby_by_callable (pd_test_4_all.cpp:6412) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6402 :emphasize-lines: 11 EXPECT(static_cast(out[1]) - (a + add) <= 2); } void test_groupby_by_callable_int_index() { std::vector v = {1.0, 2.0, 3.0, 4.0}; pandas::Series s(v); auto convert = [](size_t /*i*/, const std::string& label, bool /*hint*/) -> std::string { int64_t k = std::stoll(label); return std::to_string(k % 2); }; auto gb = s.groupby_by_callable(convert, true); EXPECT(gb.ngroups() == 2); } void test_groupby_by_callable_empty() { pandas::Series s(std::vector{}); int calls = 0; auto convert = [&](size_t, const std::string&, bool) -> std::string { ++calls; return ""; }; auto gb = s.groupby_by_callable(convert, true); EXPECT(calls == 0); EXPECT(gb.ngroups() == 0); .. _example-series-groupby_by_categorical-133: .. dropdown:: groupby_by_categorical (pd_test_3_all.cpp:23500) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23490 :emphasize-lines: 11 auto sums = gb.sum(); if (sums[0] != 40.0 || sums[1] != 60.0) throw std::runtime_error("sum mismatch"); if (gb.grouper_dtype() != "float64") throw std::runtime_error("grouper_dtype mismatch"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_by_categorical() { std::cout << "========= groupby_by_categorical() ===================="; pandas::Series s({10.0, 20.0, 30.0}); pandas::CategoricalArray cat({"a", "b", "a"}, {"a", "b", "c"}); auto gb_obs = s.groupby_by_categorical(cat, true, true); if (gb_obs.group_keys_order().size() != 2) throw std::runtime_error("expected 2 observed groups"); auto gb_all = s.groupby_by_categorical(cat, true, false); if (gb_all.group_keys_order().size() != 3) .. _example-series-groupby_by_index-134: .. dropdown:: groupby_by_index (pd_test_3_all.cpp:23426) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23416 :emphasize-lines: 11 auto gb = s.groupby_by_level(levels, true); if (gb.group_keys_order().size() != 4) throw std::runtime_error("expected 4 composite groups"); if (gb.multiindex_names().size() != 2 || gb.multiindex_names()[0] != "L0" || gb.multiindex_names()[1] != "L1") throw std::runtime_error("multiindex names mismatch"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_by_index() { std::cout << "========= groupby_by_index() =========================="; pandas::Series s({10.0, 20.0, 30.0}); s.set_index(pandas::Index({"a", "b", "a"})); auto gb = s.groupby_by_index(true, true); if (gb.group_keys_order().size() != 2) throw std::runtime_error("expected 2 groups"); auto sums = gb.sum(); if (sums[0] != 40.0 || sums[1] != 20.0) throw std::runtime_error("sum mismatch"); .. _example-series-groupby_by_labels-135: .. dropdown:: groupby_by_labels (pd_test_3_all.cpp:23520) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23510 :emphasize-lines: 11 if (gb_all.group_keys_order().size() != 3) throw std::runtime_error("expected 3 groups with observed=false"); if (gb_obs.categorical_categories().size() != 3) throw std::runtime_error("categorical_categories not set"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_by_labels() { std::cout << "========= groupby_by_labels() ========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0}); std::vector labels = {"X", "Y", "X", "Y"}; auto gb = s.groupby_by_labels(labels, "object", true); auto sums = gb.sum(); if (sums[0] != 4.0 || sums[1] != 6.0) throw std::runtime_error("sum mismatch"); if (gb.grouper_dtype() != "object") throw std::runtime_error("grouper_dtype mismatch"); .. _example-series-groupby_by_level-136: .. dropdown:: groupby_by_level (pd_test_3_all.cpp:23382) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23372 :emphasize-lines: 11 int pd_test_df_construct_mi_main() { return dataframe_tests_df_construct_mi::pd_test_df_construct_mi_main(); } // ------------------- pd_test_df_construct_mi (end) --------------------------- // ------------------- pd_test_groupby_level_dispatch.cpp (start) --------------------------- namespace dataframe_tests_groupby_level_dispatch { void pd_test_groupby_level_single() { std::cout << "========= groupby_by_level(single) ===================="; pandas::Series s({10.0, 20.0, 30.0, 40.0}); std::vector> level_values = { {"a", "a", "b", "b"}, {"x", "y", "x", "y"} }; std::vector> level_names = {"first", "second"}; auto mi = pandas::MultiIndex::from_arrays(level_values, level_names); s.set_multiindex(mi); auto gb = s.groupby_by_level(static_cast(0), true); .. _example-series-groupby_by_level-137: .. dropdown:: groupby_by_level (pd_test_3_all.cpp:23382) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23372 :emphasize-lines: 11 int pd_test_df_construct_mi_main() { return dataframe_tests_df_construct_mi::pd_test_df_construct_mi_main(); } // ------------------- pd_test_df_construct_mi (end) --------------------------- // ------------------- pd_test_groupby_level_dispatch.cpp (start) --------------------------- namespace dataframe_tests_groupby_level_dispatch { void pd_test_groupby_level_single() { std::cout << "========= groupby_by_level(single) ===================="; pandas::Series s({10.0, 20.0, 30.0, 40.0}); std::vector> level_values = { {"a", "a", "b", "b"}, {"x", "y", "x", "y"} }; std::vector> level_names = {"first", "second"}; auto mi = pandas::MultiIndex::from_arrays(level_values, level_names); s.set_multiindex(mi); auto gb = s.groupby_by_level(static_cast(0), true); .. _example-series-groupby_by_level_names-138: .. dropdown:: groupby_by_level_names (pd_test_3_all.cpp:23548) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23538 :emphasize-lines: 11 pandas::Series by_s({"cat", "dog", "cat"}); auto gb = s.groupby_by_string_series(by_s, true); auto sums = gb.sum(); if (sums[0] != 400.0 || sums[1] != 200.0) throw std::runtime_error("sum mismatch"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_by_level_names() { std::cout << "========= groupby_by_level_names() ===================="; pandas::Series s({1.0, 2.0, 3.0, 4.0}); std::vector> level_values = { {"a", "a", "b", "b"}, {"x", "y", "x", "y"} }; std::vector> level_names = {"first", "second"}; auto mi = pandas::MultiIndex::from_arrays(level_values, level_names); s.set_multiindex(mi); std::vector by_names = {"first", "second"}; .. _example-series-groupby_by_numeric-139: .. dropdown:: groupby_by_numeric (pd_test_3_all.cpp:23483) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23473 :emphasize-lines: 11 int pd_test_groupby_level_dispatch_main() { return dataframe_tests_groupby_level_dispatch::pd_test_groupby_level_dispatch_main(); } // ------------------- pd_test_groupby_level_dispatch.cpp (end) --------------------------- // ------------------- pd_test_groupby_by_dispatch.cpp (start) --------------------------- namespace dataframe_tests_groupby_by_dispatch { void pd_test_groupby_by_numeric() { std::cout << "========= groupby_by_numeric() ========================"; pandas::Series s({10.0, 20.0, 30.0, 40.0}); pandas::Series by_s({1.0, 2.0, 1.0, 2.0}); auto gb = s.groupby_by_numeric(by_s, true); if (gb.group_keys_order().size() != 2) throw std::runtime_error("expected 2 groups"); auto sums = gb.sum(); if (sums[0] != 40.0 || sums[1] != 60.0) throw std::runtime_error("sum mismatch"); if (gb.grouper_dtype() != "float64") .. _example-series-groupby_by_string_series-140: .. dropdown:: groupby_by_string_series (pd_test_3_all.cpp:23535) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23525 :emphasize-lines: 11 auto sums = gb.sum(); if (sums[0] != 4.0 || sums[1] != 6.0) throw std::runtime_error("sum mismatch"); if (gb.grouper_dtype() != "object") throw std::runtime_error("grouper_dtype mismatch"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_by_string_series() { std::cout << "========= groupby_by_string_series() =================="; pandas::Series s({100.0, 200.0, 300.0}); pandas::Series by_s({"cat", "dog", "cat"}); auto gb = s.groupby_by_string_series(by_s, true); auto sums = gb.sum(); if (sums[0] != 400.0 || sums[1] != 200.0) throw std::runtime_error("sum mismatch"); std::cout << " -> tests passed" << std::endl; } .. _example-series-map-141: .. dropdown:: map (pd_test_1_all.cpp:5839) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5829 :emphasize-lines: 11 // Map Tests // ============================================================================ void pd_test_categorical_index_map() { std::cout << "========= map ========================================="; pandas::CategoricalArray arr({"yes", "no", "yes"}); pandas::CategoricalIndex idx(arr); std::unordered_map mapping = {{"yes", "1"}, {"no", "0"}}; pandas::CategoricalIndex mapped = idx.map(mapping); bool passed = (mapped.has_category("1") && mapped.has_category("0") && !mapped.has_category("yes") && !mapped.has_category("no")); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_map()" << std::endl; throw std::runtime_error("pd_test_categorical_index_map failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-map-142: .. dropdown:: map (pd_test_1_all.cpp:5839) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5829 :emphasize-lines: 11 // Map Tests // ============================================================================ void pd_test_categorical_index_map() { std::cout << "========= map ========================================="; pandas::CategoricalArray arr({"yes", "no", "yes"}); pandas::CategoricalIndex idx(arr); std::unordered_map mapping = {{"yes", "1"}, {"no", "0"}}; pandas::CategoricalIndex mapped = idx.map(mapping); bool passed = (mapped.has_category("1") && mapped.has_category("0") && !mapped.has_category("yes") && !mapped.has_category("no")); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_map()" << std::endl; throw std::runtime_error("pd_test_categorical_index_map failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-map_dict-143: .. dropdown:: map_dict (pd_test_3_all.cpp:23599) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23589 :emphasize-lines: 11 // ------------------- pd_test_groupby_by_dispatch.cpp (end) --------------------------- // ------------------- pd_test_map_dispatch.cpp (begin) --------------------------- namespace dataframe_tests_map_dispatch { void pd_test_map_dispatch_dict_basic() { std::cout << " pd_test_map_dispatch_dict_basic"; // Dict mapping: 3 matched values + 1 unmapped -> NaN; name preserved pandas::Series s({1.0, 2.0, 3.0, 4.0}, "vals"); std::map mapping = {{1.0, 10.0}, {2.0, 20.0}, {3.0, 30.0}}; auto res = s.map_dict(mapping); if (res.name() != "vals") throw std::runtime_error("name not preserved"); if (res[0] != 10.0 || res[1] != 20.0 || res[2] != 30.0) throw std::runtime_error("mapped values incorrect"); if (!std::isnan(res[3])) throw std::runtime_error("unmapped key should be NaN"); std::cout << " -> tests passed" << std::endl; } void pd_test_map_dispatch_preserves_index() { .. _example-series-map_dict_resolved-144: .. dropdown:: map_dict_resolved (pd_test_5_all.cpp:139935) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 139925 :emphasize-lines: 11 pandas::Series s(data, std::optional{}); s.set_index(std::make_unique>(labels)); s.set_dtype_override("int64"); return s; } static void f_plan_02_apply_result_dtype_517043_map_full_int(int& lf) { std::cout << "-- E_map_full_int\n"; auto s = mk_src_int64({1, 2, 3}, {"a", "b", "c"}); std::map m{{1, 10}, {2, 20}, {3, 30}}; auto r = s.map_dict_resolved(m); pandas_tests::check(result_series_dtype(r) == "int64", "E_map_full_int()_dtype", lf); } static void f_plan_02_apply_result_dtype_517043_map_partial_int(int& lf) { std::cout << "-- E_map_partial_int\n"; auto s = mk_src_int64({1, 2, 3}, {"a", "b", "c"}); std::map m{{1, 10}}; auto r = s.map_dict_resolved(m); pandas_tests::check(result_series_dtype(r) == "float64", .. _example-series-map_series-145: .. dropdown:: map_series (pd_test_3_all.cpp:23633) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23623 :emphasize-lines: 11 void pd_test_map_dispatch_series_lookup() { std::cout << " pd_test_map_dispatch_series_lookup"; // Series lookup via index labels returns correct mapped values // lookup: index=[1,2,3], values=[10,20,30] pandas::Series lookup({10.0, 20.0, 30.0}, "lk"); pandas::Index lk_idx({"1", "2", "3"}); lookup.set_index(lk_idx); pandas::Series s({2.0, 3.0, 1.0}, "src"); auto res = s.map_series(lookup); if (res[0] != 20.0 || res[1] != 30.0 || res[2] != 10.0) throw std::runtime_error("series lookup values incorrect"); if (res.name() != "src") throw std::runtime_error("name not preserved"); std::cout << " -> tests passed" << std::endl; } void pd_test_map_dispatch_nan_passthrough() { std::cout << " pd_test_map_dispatch_nan_passthrough"; // NaN in source passes through as NaN, non-NaN values mapped .. _example-series-map_series_resolved-146: .. dropdown:: map_series_resolved (pd_test_5_all.cpp:143266) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 143256 :emphasize-lines: 11 std::map m{{1, 10}}; auto r = s.map_dict_resolved(m); check_dtype_eq("apply_empty_hist_case_30_empty_map_nonempty_dict_int64()", result_series_dtype_full(r), "int64", lf); } void case_31_empty_map_nonempty_series_int64(int& lf) { std::cout << "-- case_31_empty_map_nonempty_series_int64\n"; auto s = mk_f64({}, {}, "int64"); auto mapper = mk_f64({100, 200}, {"1", "2"}, "int64"); auto r = s.map_series_resolved(mapper); check_dtype_eq("apply_empty_hist_case_31_empty_map_nonempty_series_int64()", result_series_dtype_full(r), "int64", lf); } void case_40_E1_empty_map_empty_series_int64(int& lf) { std::cout << "-- case_40_E1_empty_map_empty_series_int64\n"; auto s = mk_f64({}, {}, "int64"); auto mapper = mk_f64({}, {}, "int64"); auto r = s.map_series_resolved(mapper); check_dtype_eq("apply_empty_hist_case_40_E1_empty_map_empty_series_int64()", .. _example-series-map_to_string-147: .. dropdown:: map_to_string (pd_test_3_all.cpp:23669) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23659 :emphasize-lines: 11 void pd_test_map_dispatch_to_string() { std::cout << " pd_test_map_dispatch_to_string"; // map_to_string returns string values for matched, "NaN" for unmatched pandas::Series lookup(std::vector{"one", "two"}, "lk"); pandas::Index lk_idx({"1", "2"}); lookup.set_index(lk_idx); double nan_val = std::numeric_limits::quiet_NaN(); pandas::Series s({1.0, 2.0, 3.0, nan_val}, "src"); auto res = s.map_to_string(lookup); if (res[0] != "one" || res[1] != "two") throw std::runtime_error("matched values incorrect"); if (res[2] != "NaN") throw std::runtime_error("unmatched should be NaN string"); if (res[3] != "NaN") throw std::runtime_error("NaN source should be NaN string"); if (res.name() != "src") throw std::runtime_error("name not preserved"); std::cout << " -> tests passed" << std::endl; } .. _example-series-pipe-148: .. dropdown:: pipe (pd_test_1_all.cpp:11164) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11154 :emphasize-lines: 11 // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_pipe() : value mismatch at " << i << std::endl; throw std::runtime_error("pd_test_func_apply_series_pipe failed: value mismatch"); } .. _example-series-resample-149: .. dropdown:: resample (pd_test_1_all.cpp:20321) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20311 :emphasize-lines: 11 "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; throw std::runtime_error("pd_test_timeseries_resample_basic failed"); } .. _example-series-rolling-150: .. dropdown:: rolling (pd_test_1_all.cpp:20667) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20657 :emphasize-lines: 11 #include #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_windowing { void pd_test_rolling_sum() { std::cout << "========= Rolling sum ==========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.rolling(3).sum(); // Window 3: // idx 0: [1] -> NaN (not enough values) // idx 1: [1,2] -> NaN (not enough values) // idx 2: [1,2,3] -> 6 // idx 3: [2,3,4] -> 9 // idx 4: [3,4,5] -> 12 bool passed = result.size() == 5; if (!passed) { std::cout << " [FAIL] : in pd_test_rolling_sum() : result size should be 5" << std::endl; .. _example-series-transform-151: .. dropdown:: transform (pd_test_1_all.cpp:11071) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11061 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_func_apply_series_transform() { std::cout << "========= Series transform ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Transform must return same shape auto result = s.transform([](double x) { return x * 2 + 1; }); bool passed = true; if (result.size() != s.size()) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_transform() : size changed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_transform failed: size changed"); } std::vector expected = {3.0, 5.0, 7.0, 9.0}; for (size_t i = 0; i < result.size(); ++i) { .. _example-series-transform-152: .. dropdown:: transform (pd_test_1_all.cpp:11071) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11061 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_func_apply_series_transform() { std::cout << "========= Series transform ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0}, "values"); // Transform must return same shape auto result = s.transform([](double x) { return x * 2 + 1; }); bool passed = true; if (result.size() != s.size()) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_transform() : size changed" << std::endl; throw std::runtime_error("pd_test_func_apply_series_transform failed: size changed"); } std::vector expected = {3.0, 5.0, 7.0, 9.0}; for (size_t i = 0; i < result.size(); ++i) { .. _example-series-transform_named_list-153: .. dropdown:: transform_named_list (pd_test_3_all.cpp:27185) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27175 :emphasize-lines: 11 auto result = s.map_dict(mapping); check(result[0] == 100.0, "mapped 1->100"); check(std::isnan(result[1]), "unmapped 5->NaN"); check(result[2] == 300.0, "mapped 3->300"); } // Test 6: transform multi (list of named functions -> DataFrame) void pd_test_transform_multi() { std::cout << " -- pd_test_transform_multi --" << std::endl; Series s({4.0, 9.0, 16.0}, std::string("nums")); auto df = s.transform_named_list({"sqrt", "abs"}); check(df.ncols() == 2, "2 columns"); check(df.nrows() == 3, "3 rows"); // Get sqrt column by index (first column = index 0) size_t sqrt_idx = df.get_column_index("sqrt"); auto sqrt_series = df.column_to_series_f64(sqrt_idx); check(std::abs(sqrt_series[0] - 2.0) < 1e-10, "sqrt(4)==2"); check(std::abs(sqrt_series[1] - 3.0) < 1e-10, "sqrt(9)==3"); check(std::abs(sqrt_series[2] - 4.0) < 1e-10, "sqrt(16)==4"); size_t abs_idx = df.get_column_index("abs"); auto abs_series = df.column_to_series_f64(abs_idx); .. _example-series-transform_resolved-154: .. dropdown:: transform_resolved (pd_test_5_all.cpp:98227) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 98217 :emphasize-lines: 11 switch (cid) { case CbId::Int: cb = cb_int(hist); break; case CbId::Bool: cb = cb_bool(hist); break; case CbId::Float: cb = cb_float(hist); break; case CbId::Str: cb = cb_string(hist); break; case CbId::Mixed: cb = cb_mixed(hist); break; } pandas::Result r; try { r = s.transform_resolved(cb, hist); } catch (const std::exception& e) { std::string tag = std::string("transform src=") + src_name(sid) + " cb=" + cb_name(cid) + " mode=" + mode_name(mid); std::cout << "[FAIL] : in f_27a_core_3094022_apply_resolved_typed_post_cb_dtype() " << tag << " unexpected exception: " << e.what() << "\n"; ++pandas_tests::g_failed; ++local_fail; ++pandas_tests::g_failed; ++local_fail; ++pandas_tests::g_failed; ++local_fail; return; } .. _example-series-add-155: .. dropdown:: add (pd_test_1_all.cpp:4844) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4834 :emphasize-lines: 11 namespace dataframe_tests { namespace dataframe_tests_arithmetic { void pd_test_arithmetic_series_named_ops() { std::cout << "========= Series named ops ======================"; pandas::Series a({1.0, 2.0, 3.0}); pandas::Series b({4.0, 5.0, 6.0}); auto sum = a.add(b); bool passed = std::abs(sum[0] - 5.0) < 0.001 && std::abs(sum[1] - 7.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : add failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: add failed"); } auto diff = a.sub(b); passed = std::abs(diff[0] - (-3.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : sub failed" << std::endl; .. _example-series-add-156: .. dropdown:: add (pd_test_1_all.cpp:4844) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4834 :emphasize-lines: 11 namespace dataframe_tests { namespace dataframe_tests_arithmetic { void pd_test_arithmetic_series_named_ops() { std::cout << "========= Series named ops ======================"; pandas::Series a({1.0, 2.0, 3.0}); pandas::Series b({4.0, 5.0, 6.0}); auto sum = a.add(b); bool passed = std::abs(sum[0] - 5.0) < 0.001 && std::abs(sum[1] - 7.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : add failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: add failed"); } auto diff = a.sub(b); passed = std::abs(diff[0] - (-3.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : sub failed" << std::endl; .. _example-series-add_dateoffset-157: .. dropdown:: add_dateoffset (pd_test_4_all.cpp:6330) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6320 :emphasize-lines: 11 std::vector v(ns.size()); for (size_t i = 0; i < ns.size(); ++i) v[i] = static_cast(ns[i]); pandas::Series s(v); s.set_dtype_override("datetime64[ns]"); return s; } void test_add_dateoffset_tick_day() { auto s = make_dt_series({0, 86400000000000LL}); pandas::Day d(5); auto out = s.add_dateoffset(d); EXPECT(out.size() == 2); EXPECT(static_cast(out[0]) == 5LL * 86400000000000LL); EXPECT(static_cast(out[1]) == 6LL * 86400000000000LL); EXPECT(out.dtype_name() == "datetime64[ns]"); } void test_sub_dateoffset_calendar_monthend() { // 2024-01-31 in ns int64_t jan31 = 1706659200LL * 1000000000LL; auto s = make_dt_series({jan31}); .. _example-series-add_dateoffset_to_timedelta-158: .. dropdown:: add_dateoffset_to_timedelta (pd_test_4_all.cpp:6397) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6387 :emphasize-lines: 11 } void test_add_dateoffset_to_timedelta_precision() { int64_t a = 1LL << 54; int64_t b = a + 1; std::vector v = {static_cast(a), static_cast(b)}; pandas::Series s(v); s.set_dtype_override("timedelta64[ns]"); pandas::Day d(1); auto out = s.add_dateoffset_to_timedelta(d); int64_t add = 86400000000000LL; EXPECT(static_cast(out[0]) == a + add); // Note: float64 cannot precisely represent (a+1); this only verifies the // int64-space computation, not full lossless storage. EXPECT(static_cast(out[1]) - (a + add) <= 2); } void test_groupby_by_callable_int_index() { std::vector v = {1.0, 2.0, 3.0, 4.0}; pandas::Series s(v); .. _example-series-add_prefix-159: .. dropdown:: add_prefix (pd_test_2_all.cpp:4) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1 :emphasize-lines: 4 // ------------------- pd_test_add_prefix.cpp (start) ----------------------------- // dataframe_tests/pd_test_add_prefix.cpp // Tests for DataFrame.add_prefix() and add_suffix() methods (pandas 2.0+ API) #include #include #include #include #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_groupby.h" // CRITICAL: No using namespace directives .. _example-series-add_suffix-160: .. dropdown:: add_suffix (pd_test_2_all.cpp:4) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1 :emphasize-lines: 4 // ------------------- pd_test_add_prefix.cpp (start) ----------------------------- // dataframe_tests/pd_test_add_prefix.cpp // Tests for DataFrame.add_prefix() and add_suffix() methods (pandas 2.0+ API) #include #include #include #include #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_groupby.h" // CRITICAL: No using namespace directives .. _example-series-div-161: .. dropdown:: div (pd_test_1_all.cpp:4865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4855 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: sub failed"); } auto prod = a.mul(b); passed = std::abs(prod[0] - 4.0) < 0.001 && std::abs(prod[1] - 10.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : mul failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: mul failed"); } auto quot = a.div(b); passed = std::abs(quot[0] - 0.25) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : div failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: div failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_arithmetic_series_floordiv_mod() { .. _example-series-div-162: .. dropdown:: div (pd_test_1_all.cpp:4865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4855 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: sub failed"); } auto prod = a.mul(b); passed = std::abs(prod[0] - 4.0) < 0.001 && std::abs(prod[1] - 10.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : mul failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: mul failed"); } auto quot = a.div(b); passed = std::abs(quot[0] - 0.25) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : div failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: div failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_arithmetic_series_floordiv_mod() { .. _example-series-divide-163: .. dropdown:: divide (pd_test_3_all.cpp:555) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 545 :emphasize-lines: 11 if (mul_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : multiply() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: multiply()"); } // 10*2=20 if (std::abs(mul_result[static_cast(0)] - 20.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : multiply() value mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: multiply() value"); } // Test divide() pandas::Series div_result = s1.divide(s2); if (div_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : divide() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: divide()"); } // 10/2=5 if (std::abs(div_result[static_cast(0)] - 5.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : divide() value mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: divide() value"); } .. _example-series-divmod-164: .. dropdown:: divmod (pd_test_3_all.cpp:12077) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12067 :emphasize-lines: 11 auto cov_val = s1.cov(s2); if (!cov_val.has_value()) { std::cout << " [FAIL] : covariance should have a value" << std::endl; throw std::runtime_error("pd_test_series_corr_cov failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Test 9: divmod() // ============================================================================ void pd_test_series_divmod() { std::cout << "========= Series.divmod() =========================="; std::vector vals = {10.0, 20.0, 30.0}; pandas::Series s(vals, "test"); auto [quot, rem] = s.divmod(7.0); // 10/7 = 1 remainder 3 .. _example-series-divmod-165: .. dropdown:: divmod (pd_test_3_all.cpp:12077) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12067 :emphasize-lines: 11 auto cov_val = s1.cov(s2); if (!cov_val.has_value()) { std::cout << " [FAIL] : covariance should have a value" << std::endl; throw std::runtime_error("pd_test_series_corr_cov failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Test 9: divmod() // ============================================================================ void pd_test_series_divmod() { std::cout << "========= Series.divmod() =========================="; std::vector vals = {10.0, 20.0, 30.0}; pandas::Series s(vals, "test"); auto [quot, rem] = s.divmod(7.0); // 10/7 = 1 remainder 3 .. _example-series-dot-166: .. dropdown:: dot (pd_test_1_all.cpp:22594) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22584 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_all_any test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_all_any.cpp (end) ----------------------------- // ------------------- pd_test_dot.cpp (start) ----------------------------- // dataframe_tests/pd_test_dot.cpp // Test DataFrame.dot() method - matrix multiplication #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { namespace dataframe_tests_dot { .. _example-series-dot-167: .. dropdown:: dot (pd_test_1_all.cpp:22594) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22584 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_all_any test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_all_any.cpp (end) ----------------------------- // ------------------- pd_test_dot.cpp (start) ----------------------------- // dataframe_tests/pd_test_dot.cpp // Test DataFrame.dot() method - matrix multiplication #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { namespace dataframe_tests_dot { .. _example-series-floordiv-168: .. dropdown:: floordiv (pd_test_1_all.cpp:4881) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4871 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_arithmetic_series_floordiv_mod() { std::cout << "========= Series floordiv/mod ==================="; pandas::Series a({7.0, 8.0, 9.0}); pandas::Series b({2.0, 3.0, 4.0}); auto fd = a.floordiv(b); bool passed = std::abs(fd[0] - 3.0) < 0.001; // 7 // 2 = 3 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : floordiv failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_floordiv_mod failed: floordiv failed"); } auto m = a.mod(b); passed = std::abs(m[0] - 1.0) < 0.001; // 7 % 2 = 1 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : mod failed" << std::endl; .. _example-series-floordiv-169: .. dropdown:: floordiv (pd_test_1_all.cpp:4881) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4871 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_arithmetic_series_floordiv_mod() { std::cout << "========= Series floordiv/mod ==================="; pandas::Series a({7.0, 8.0, 9.0}); pandas::Series b({2.0, 3.0, 4.0}); auto fd = a.floordiv(b); bool passed = std::abs(fd[0] - 3.0) < 0.001; // 7 // 2 = 3 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : floordiv failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_floordiv_mod failed: floordiv failed"); } auto m = a.mod(b); passed = std::abs(m[0] - 1.0) < 0.001; // 7 % 2 = 1 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : mod failed" << std::endl; .. _example-series-mod-170: .. dropdown:: mod (pd_test_1_all.cpp:4888) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4878 :emphasize-lines: 11 pandas::Series a({7.0, 8.0, 9.0}); pandas::Series b({2.0, 3.0, 4.0}); auto fd = a.floordiv(b); bool passed = std::abs(fd[0] - 3.0) < 0.001; // 7 // 2 = 3 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : floordiv failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_floordiv_mod failed: floordiv failed"); } auto m = a.mod(b); passed = std::abs(m[0] - 1.0) < 0.001; // 7 % 2 = 1 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : mod failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_floordiv_mod failed: mod failed"); } // Scalar operations auto fd_scalar = a.floordiv(2.0); passed = std::abs(fd_scalar[0] - 3.0) < 0.001 && std::abs(fd_scalar[1] - 4.0) < 0.001; if (!passed) { .. _example-series-mod-171: .. dropdown:: mod (pd_test_1_all.cpp:4888) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4878 :emphasize-lines: 11 pandas::Series a({7.0, 8.0, 9.0}); pandas::Series b({2.0, 3.0, 4.0}); auto fd = a.floordiv(b); bool passed = std::abs(fd[0] - 3.0) < 0.001; // 7 // 2 = 3 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : floordiv failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_floordiv_mod failed: floordiv failed"); } auto m = a.mod(b); passed = std::abs(m[0] - 1.0) < 0.001; // 7 % 2 = 1 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_floordiv_mod() : mod failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_floordiv_mod failed: mod failed"); } // Scalar operations auto fd_scalar = a.floordiv(2.0); passed = std::abs(fd_scalar[0] - 3.0) < 0.001 && std::abs(fd_scalar[1] - 4.0) < 0.001; if (!passed) { .. _example-series-mul-172: .. dropdown:: mul (pd_test_1_all.cpp:4858) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4848 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: add failed"); } auto diff = a.sub(b); passed = std::abs(diff[0] - (-3.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : sub failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: sub failed"); } auto prod = a.mul(b); passed = std::abs(prod[0] - 4.0) < 0.001 && std::abs(prod[1] - 10.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : mul failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: mul failed"); } auto quot = a.div(b); passed = std::abs(quot[0] - 0.25) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : div failed" << std::endl; .. _example-series-mul-173: .. dropdown:: mul (pd_test_1_all.cpp:4858) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4848 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: add failed"); } auto diff = a.sub(b); passed = std::abs(diff[0] - (-3.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : sub failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: sub failed"); } auto prod = a.mul(b); passed = std::abs(prod[0] - 4.0) < 0.001 && std::abs(prod[1] - 10.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : mul failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: mul failed"); } auto quot = a.div(b); passed = std::abs(quot[0] - 0.25) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : div failed" << std::endl; .. _example-series-multiindex-174: .. dropdown:: multiindex (pd_test_1_all.cpp:27024) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27014 :emphasize-lines: 11 pandas::DataFrame df(data); auto result = df.value_counts(); auto& counts = std::get>(result); if (!counts.has_multiindex()) { std::cout << " [FAIL] : expected MultiIndex" << std::endl; throw std::runtime_error("pd_test_value_counts_multiindex_levels failed: no multiindex"); } const auto& midx = counts.multiindex(); // Should have 2 levels if (midx.nlevels() != 2) { std::cout << " [FAIL] : expected 2 levels, got " << midx.nlevels() << std::endl; throw std::runtime_error("pd_test_value_counts_multiindex_levels failed: wrong nlevels"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-multiply-175: .. dropdown:: multiply (pd_test_3_all.cpp:543) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 533 :emphasize-lines: 11 if (sub_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : subtract() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: subtract()"); } // 10-2=8 if (std::abs(sub_result[static_cast(0)] - 8.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : subtract() value mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: subtract() value"); } // Test multiply() pandas::Series mul_result = s1.multiply(s2); if (mul_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : multiply() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: multiply()"); } // 10*2=20 if (std::abs(mul_result[static_cast(0)] - 20.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : multiply() value mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: multiply() value"); } .. _example-series-pow-176: .. dropdown:: pow (pd_test_1_all.cpp:4911) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4901 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void pd_test_arithmetic_series_pow() { std::cout << "========= Series pow ============================"; pandas::Series a({2.0, 3.0, 4.0}); auto p = a.pow(2.0); bool passed = std::abs(p[0] - 4.0) < 0.001 && std::abs(p[1] - 9.0) < 0.001 && std::abs(p[2] - 16.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_pow() : pow scalar failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_pow failed: pow scalar failed"); } // Series pow Series pandas::Series exp({1.0, 2.0, 0.5}); auto p2 = a.pow(exp); passed = std::abs(p2[0] - 2.0) < 0.001 && std::abs(p2[1] - 9.0) < 0.001; // 3^2=9 .. _example-series-pow-177: .. dropdown:: pow (pd_test_1_all.cpp:4911) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4901 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void pd_test_arithmetic_series_pow() { std::cout << "========= Series pow ============================"; pandas::Series a({2.0, 3.0, 4.0}); auto p = a.pow(2.0); bool passed = std::abs(p[0] - 4.0) < 0.001 && std::abs(p[1] - 9.0) < 0.001 && std::abs(p[2] - 16.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_pow() : pow scalar failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_pow failed: pow scalar failed"); } // Series pow Series pandas::Series exp({1.0, 2.0, 0.5}); auto p2 = a.pow(exp); passed = std::abs(p2[0] - 2.0) < 0.001 && std::abs(p2[1] - 9.0) < 0.001; // 3^2=9 .. _example-series-radd-178: .. dropdown:: radd (pd_test_2_all.cpp:7440) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7430 :emphasize-lines: 11 if (std::isinf(a) && std::isinf(b)) return (a > 0) == (b > 0); return std::abs(a - b) < tol; } // Helper to get double value from DataFrame at position double get_val(const pandas::DataFrame& df, size_t row, size_t col) { return df.iloc(row, col); } void pd_test_radd_scalar() { std::cout << "========= radd() with scalar ====================="; // Create DataFrame: angles=[0, 3, 4], degrees=[360, 180, 360] std::map> data = { {"angles", {0.0, 3.0, 4.0}}, {"degrees", {360.0, 180.0, 360.0}} }; pandas::DataFrame df(data); // df.radd(1) should be equivalent to 1 + df pandas::DataFrame result = df.radd(1.0); .. _example-series-radd-179: .. dropdown:: radd (pd_test_2_all.cpp:7440) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7430 :emphasize-lines: 11 if (std::isinf(a) && std::isinf(b)) return (a > 0) == (b > 0); return std::abs(a - b) < tol; } // Helper to get double value from DataFrame at position double get_val(const pandas::DataFrame& df, size_t row, size_t col) { return df.iloc(row, col); } void pd_test_radd_scalar() { std::cout << "========= radd() with scalar ====================="; // Create DataFrame: angles=[0, 3, 4], degrees=[360, 180, 360] std::map> data = { {"angles", {0.0, 3.0, 4.0}}, {"degrees", {360.0, 180.0, 360.0}} }; pandas::DataFrame df(data); // df.radd(1) should be equivalent to 1 + df pandas::DataFrame result = df.radd(1.0); .. _example-series-rdiv-180: .. dropdown:: rdiv (pd_test_2_all.cpp:7713) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7703 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rmul_with_fill_value failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rdiv_scalar() { std::cout << "========= rdiv() with scalar ====================="; // From pandas docs example: df.rdiv(10) divides 10 BY the dataframe std::map> data = { {"angles", {0.0, 3.0, 4.0}}, {"degrees", {360.0, 180.0, 360.0}} }; pandas::DataFrame df(data); // df.rdiv(10) = 10 / df pandas::DataFrame result = df.rdiv(10.0); .. _example-series-rdivmod-181: .. dropdown:: rdivmod (pd_test_3_all.cpp:9176) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9166 :emphasize-lines: 11 // Check: val[2]=3, no condition matches -> default 0.0 if (std::abs(result[static_cast(2)] - 0.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_case_when() : default value wrong" << std::endl; throw std::runtime_error("pd_test_3_all_series_case_when failed: default"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_series_rdivmod() { std::cout << "========= Series.rdivmod() ========================"; std::vector vals = {2.0, 3.0, 4.0, 5.0}; pandas::Series s(vals, "test"); // rdivmod: scalar / series -> (quotient, remainder) // 10 / 2 = 5, 10 % 2 = 0 // 10 / 3 = 3, 10 % 3 = 1 // 10 / 4 = 2, 10 % 4 = 2 // 10 / 5 = 2, 10 % 5 = 0 auto [quotients, remainders] = s.rdivmod(10.0); .. _example-series-rfloordiv-182: .. dropdown:: rfloordiv (pd_test_2_all.cpp:7909) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7899 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rtruediv_with_fill_value failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rfloordiv_scalar() { std::cout << "========= rfloordiv() with scalar ================"; std::map> data = { {"A", {3.0, 4.0}}, {"B", {7.0, 8.0}} }; pandas::DataFrame df(data); // df.rfloordiv(10) = 10 // df (floor division) pandas::DataFrame result = df.rfloordiv(10.0); .. _example-series-rmod-183: .. dropdown:: rmod (pd_test_2_all.cpp:8121) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8111 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rfloordiv_division_by_zero failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rmod_scalar() { std::cout << "========= rmod() with scalar ====================="; std::map> data = { {"A", {3.0, 4.0}} }; pandas::DataFrame df(data); // df.rmod(10) = 10 % df pandas::DataFrame result = df.rmod(10.0); bool passed = true; .. _example-series-rmul-184: .. dropdown:: rmul (pd_test_2_all.cpp:7591) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7581 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rsub_dataframe failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rmul_scalar() { std::cout << "========= rmul() with scalar ====================="; std::map> data = { {"A", {2.0, 3.0}}, {"B", {4.0, 5.0}} }; pandas::DataFrame df(data); // df.rmul(10) = 10 * df pandas::DataFrame result = df.rmul(10.0); .. _example-series-rmul-185: .. dropdown:: rmul (pd_test_2_all.cpp:7591) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7581 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rsub_dataframe failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rmul_scalar() { std::cout << "========= rmul() with scalar ====================="; std::map> data = { {"A", {2.0, 3.0}}, {"B", {4.0, 5.0}} }; pandas::DataFrame df(data); // df.rmul(10) = 10 * df pandas::DataFrame result = df.rmul(10.0); .. _example-series-rpow-186: .. dropdown:: rpow (pd_test_2_all.cpp:8327) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8317 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rmod_modulo_by_zero failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rpow_scalar() { std::cout << "========= rpow() with scalar ====================="; std::map> data = { {"A", {2.0, 3.0}}, {"B", {0.0, 1.0}} }; pandas::DataFrame df(data); // df.rpow(2) = 2 ** df pandas::DataFrame result = df.rpow(2.0); .. _example-series-rsub-187: .. dropdown:: rsub (pd_test_2_all.cpp:7520) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7510 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_radd_dataframe failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rsub_scalar() { std::cout << "========= rsub() with scalar ====================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // df.rsub(10) = 10 - df pandas::DataFrame result = df.rsub(10.0); .. _example-series-rsub-188: .. dropdown:: rsub (pd_test_2_all.cpp:7520) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7510 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_radd_dataframe failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rsub_scalar() { std::cout << "========= rsub() with scalar ====================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // df.rsub(10) = 10 - df pandas::DataFrame result = df.rsub(10.0); .. _example-series-rtruediv-189: .. dropdown:: rtruediv (pd_test_2_all.cpp:7795) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7785 :emphasize-lines: 11 } if (!passed) { throw std::runtime_error("pd_test_rdiv_dataframe failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rtruediv_scalar() { std::cout << "========= rtruediv() with scalar ================="; std::map> data = { {"A", {2.0, 4.0}} }; pandas::DataFrame df(data); // rtruediv is alias for rdiv pandas::DataFrame result = df.rtruediv(10.0); bool passed = true; .. _example-series-sub-190: .. dropdown:: sub (pd_test_1_all.cpp:4851) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4841 :emphasize-lines: 11 pandas::Series a({1.0, 2.0, 3.0}); pandas::Series b({4.0, 5.0, 6.0}); auto sum = a.add(b); bool passed = std::abs(sum[0] - 5.0) < 0.001 && std::abs(sum[1] - 7.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : add failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: add failed"); } auto diff = a.sub(b); passed = std::abs(diff[0] - (-3.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : sub failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: sub failed"); } auto prod = a.mul(b); passed = std::abs(prod[0] - 4.0) < 0.001 && std::abs(prod[1] - 10.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : mul failed" << std::endl; .. _example-series-sub-191: .. dropdown:: sub (pd_test_1_all.cpp:4851) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4841 :emphasize-lines: 11 pandas::Series a({1.0, 2.0, 3.0}); pandas::Series b({4.0, 5.0, 6.0}); auto sum = a.add(b); bool passed = std::abs(sum[0] - 5.0) < 0.001 && std::abs(sum[1] - 7.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : add failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: add failed"); } auto diff = a.sub(b); passed = std::abs(diff[0] - (-3.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : sub failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_named_ops failed: sub failed"); } auto prod = a.mul(b); passed = std::abs(prod[0] - 4.0) < 0.001 && std::abs(prod[1] - 10.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_named_ops() : mul failed" << std::endl; .. _example-series-sub_dateoffset-192: .. dropdown:: sub_dateoffset (pd_test_4_all.cpp:6342) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6332 :emphasize-lines: 11 EXPECT(static_cast(out[0]) == 5LL * 86400000000000LL); EXPECT(static_cast(out[1]) == 6LL * 86400000000000LL); EXPECT(out.dtype_name() == "datetime64[ns]"); } void test_sub_dateoffset_calendar_monthend() { // 2024-01-31 in ns int64_t jan31 = 1706659200LL * 1000000000LL; auto s = make_dt_series({jan31}); pandas::MonthEnd me(1); auto out = s.sub_dateoffset(me); auto neg = me.negate(); auto ref = s.add_dateoffset(*neg); EXPECT(out.size() == 1); EXPECT(static_cast(out[0]) == static_cast(ref[0])); EXPECT(out.dtype_name() == "datetime64[ns]"); } void test_sub_dateoffset_equals_add_negated() { int64_t jan31 = 1706659200LL * 1000000000LL; auto s = make_dt_series({jan31, jan31 + 86400000000000LL}); .. _example-series-subtract-193: .. dropdown:: subtract (pd_test_3_all.cpp:531) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 521 :emphasize-lines: 11 // ============================================================================ void pd_test_3_all_series_arithmetic() { std::cout << "========= Series.subtract/multiply/divide/truediv() ="; std::vector vals1 = {10.0, 20.0, 30.0, 40.0}; std::vector vals2 = {2.0, 4.0, 6.0, 8.0}; pandas::Series s1(vals1, "s1"); pandas::Series s2(vals2, "s2"); // Test subtract() pandas::Series sub_result = s1.subtract(s2); if (sub_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : subtract() size mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: subtract()"); } // 10-2=8 if (std::abs(sub_result[static_cast(0)] - 8.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : subtract() value mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: subtract() value"); } .. _example-series-truediv-194: .. dropdown:: truediv (pd_test_3_all.cpp:524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 514 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 3: Series Arithmetic Operations // ============================================================================ void pd_test_3_all_series_arithmetic() { std::cout << "========= Series.subtract/multiply/divide/truediv() ="; std::vector vals1 = {10.0, 20.0, 30.0, 40.0}; std::vector vals2 = {2.0, 4.0, 6.0, 8.0}; pandas::Series s1(vals1, "s1"); pandas::Series s2(vals2, "s2"); // Test subtract() pandas::Series sub_result = s1.subtract(s2); if (sub_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : subtract() size mismatch" << std::endl; .. _example-series-truediv-195: .. dropdown:: truediv (pd_test_3_all.cpp:524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 514 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 3: Series Arithmetic Operations // ============================================================================ void pd_test_3_all_series_arithmetic() { std::cout << "========= Series.subtract/multiply/divide/truediv() ="; std::vector vals1 = {10.0, 20.0, 30.0, 40.0}; std::vector vals2 = {2.0, 4.0, 6.0, 8.0}; pandas::Series s1(vals1, "s1"); pandas::Series s2(vals2, "s2"); // Test subtract() pandas::Series sub_result = s1.subtract(s2); if (sub_result.size() != 4) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : subtract() size mismatch" << std::endl; .. _example-series-compare-196: .. dropdown:: compare (pd_test_1_all.cpp:13989) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13979 :emphasize-lines: 11 if (!approx_equal(std::stod(b_col.get_value_str(0)), 10.0)) { passed = false; std::cout << " [FAIL] : in pd_test_joining_update() : column B was changed" << std::endl; throw std::runtime_error("pd_test_joining_update failed: B changed"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // compare() Tests // ===================================================================== void pd_test_joining_compare() { std::cout << "========= compare ====================================="; std::map> left_data = { {"A", {1.0, 2.0, 3.0}}, {"B", {10.0, 20.0, 30.0}} }; pandas::DataFrame left(left_data); .. _example-series-eq-197: .. dropdown:: eq (pd_test_2_all.cpp:19680) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19670 :emphasize-lines: 11 std::vector> cols; cols.push_back(pandas::Series({1.0, 2.0}, "A")); cols.push_back(pandas::Series({3.0, 3.0}, "B")); pandas::DataFrame df(cols, {"A", "B"}); pandas::Series s({1.0, 3.0}, "vals"); s.set_index(std::make_unique>( std::vector{"A", "B"})); auto result = df.eq(s, 1); check(approx(result["A"].get_value_double(0), 1.0), "eq_A_r0_true"); check(approx(result["A"].get_value_double(1), 0.0), "eq_A_r1_false"); check(approx(result["B"].get_value_double(0), 1.0), "eq_B_r0_true"); check(approx(result["B"].get_value_double(1), 1.0), "eq_B_r1_true"); } // Test 5: mul scalar broadcast (verify existing behavior still works) void pd_test_broadcasting_mul_scalar() { std::cout << " -- pd_test_broadcasting_mul_scalar --" << std::endl; .. _example-series-eq-198: .. dropdown:: eq (pd_test_2_all.cpp:19680) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19670 :emphasize-lines: 11 std::vector> cols; cols.push_back(pandas::Series({1.0, 2.0}, "A")); cols.push_back(pandas::Series({3.0, 3.0}, "B")); pandas::DataFrame df(cols, {"A", "B"}); pandas::Series s({1.0, 3.0}, "vals"); s.set_index(std::make_unique>( std::vector{"A", "B"})); auto result = df.eq(s, 1); check(approx(result["A"].get_value_double(0), 1.0), "eq_A_r0_true"); check(approx(result["A"].get_value_double(1), 0.0), "eq_A_r1_false"); check(approx(result["B"].get_value_double(0), 1.0), "eq_B_r0_true"); check(approx(result["B"].get_value_double(1), 1.0), "eq_B_r1_true"); } // Test 5: mul scalar broadcast (verify existing behavior still works) void pd_test_broadcasting_mul_scalar() { std::cout << " -- pd_test_broadcasting_mul_scalar --" << std::endl; .. _example-series-equals-199: .. dropdown:: equals (pd_test_1_all.cpp:5866) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5856 :emphasize-lines: 11 std::cout << "========= equals ======================================"; pandas::CategoricalArray arr1({"a", "b", "a"}); pandas::CategoricalArray arr2({"a", "b", "a"}); pandas::CategoricalArray arr3({"a", "b", "c"}); pandas::CategoricalIndex idx1(arr1); pandas::CategoricalIndex idx2(arr2); pandas::CategoricalIndex idx3(arr3); bool passed = (idx1.equals(idx2) && !idx1.equals(idx3)); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_equals()" << std::endl; throw std::runtime_error("pd_test_categorical_index_equals failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_identical() { std::cout << "========= identical ==================================="; .. _example-series-ge-200: .. dropdown:: ge (pd_test_3_all.cpp:303) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 293 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 2: DataFrame Comparison Operations // ============================================================================ void pd_test_3_all_comparison_ops() { std::cout << "========= DataFrame.eq/ne/lt/le/gt/ge() ============="; std::map> data1 = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; std::map> data2 = { {"A", {1.0, 3.0, 3.0}}, {"B", {4.0, 4.0, 7.0}} }; pandas::DataFrame df1(data1); .. _example-series-ge-201: .. dropdown:: ge (pd_test_3_all.cpp:303) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 293 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 2: DataFrame Comparison Operations // ============================================================================ void pd_test_3_all_comparison_ops() { std::cout << "========= DataFrame.eq/ne/lt/le/gt/ge() ============="; std::map> data1 = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; std::map> data2 = { {"A", {1.0, 3.0, 3.0}}, {"B", {4.0, 4.0, 7.0}} }; pandas::DataFrame df1(data1); .. _example-series-gen-202: .. dropdown:: gen (pd_test_5_all.cpp:35852) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 35842 :emphasize-lines: 11 double pc = pct_change_pc(a, b); double pd = pct_change_pd(a, b); pandas_tests::check(std::abs(pc - pd) < 1e-12, "case_12.formulas_within_ULP", local_fail); } void bin_edge_412638_case_13_entropy_pct_change_invariance(int& local_fail) { // Generate prices via deterministic walk; compute returns by both // formulas; bin both; entropy should be IDENTICAL (bin assignments // not shifted by ULP-scale formula drift). Cycle-1 finding. std::mt19937_64 gen(42); std::normal_distribution nd(0.0003, 0.02); std::vector prices; prices.reserve(500); double s = 100.0; for (int i = 0; i < 500; ++i) { if (i > 0) s = s * std::exp(nd(gen)); prices.push_back(s); } std::vector r_pc, r_pd; for (size_t i = 1; i < prices.size(); ++i) { .. _example-series-gt-203: .. dropdown:: gt (pd_test_3_all.cpp:344) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 334 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_comparison_ops failed: lt() shape"); } // Test le() pandas::DataFrame le_result = df1.le(df2); if (le_result.nrows() != 3 || le_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : le() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: le() shape"); } // Test gt() pandas::DataFrame gt_result = df1.gt(df2); if (gt_result.nrows() != 3 || gt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : gt() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: gt() shape"); } // Test ge() pandas::DataFrame ge_result = df1.ge(df2); if (ge_result.nrows() != 3 || ge_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : ge() shape mismatch" << std::endl; .. _example-series-gt-204: .. dropdown:: gt (pd_test_3_all.cpp:344) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 334 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_comparison_ops failed: lt() shape"); } // Test le() pandas::DataFrame le_result = df1.le(df2); if (le_result.nrows() != 3 || le_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : le() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: le() shape"); } // Test gt() pandas::DataFrame gt_result = df1.gt(df2); if (gt_result.nrows() != 3 || gt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : gt() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: gt() shape"); } // Test ge() pandas::DataFrame ge_result = df1.ge(df2); if (ge_result.nrows() != 3 || ge_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : ge() shape mismatch" << std::endl; .. _example-series-le-205: .. dropdown:: le (pd_test_3_all.cpp:337) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 327 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_comparison_ops failed: ne() shape"); } // Test lt() pandas::DataFrame lt_result = df1.lt(df2); if (lt_result.nrows() != 3 || lt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : lt() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: lt() shape"); } // Test le() pandas::DataFrame le_result = df1.le(df2); if (le_result.nrows() != 3 || le_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : le() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: le() shape"); } // Test gt() pandas::DataFrame gt_result = df1.gt(df2); if (gt_result.nrows() != 3 || gt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : gt() shape mismatch" << std::endl; .. _example-series-le-206: .. dropdown:: le (pd_test_3_all.cpp:337) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 327 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_comparison_ops failed: ne() shape"); } // Test lt() pandas::DataFrame lt_result = df1.lt(df2); if (lt_result.nrows() != 3 || lt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : lt() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: lt() shape"); } // Test le() pandas::DataFrame le_result = df1.le(df2); if (le_result.nrows() != 3 || le_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : le() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: le() shape"); } // Test gt() pandas::DataFrame gt_result = df1.gt(df2); if (gt_result.nrows() != 3 || gt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : gt() shape mismatch" << std::endl; .. _example-series-lt-207: .. dropdown:: lt (pd_test_3_all.cpp:330) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 320 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_comparison_ops failed: eq() shape"); } // Test ne() pandas::DataFrame ne_result = df1.ne(df2); if (ne_result.nrows() != 3 || ne_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : ne() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: ne() shape"); } // Test lt() pandas::DataFrame lt_result = df1.lt(df2); if (lt_result.nrows() != 3 || lt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : lt() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: lt() shape"); } // Test le() pandas::DataFrame le_result = df1.le(df2); if (le_result.nrows() != 3 || le_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : le() shape mismatch" << std::endl; .. _example-series-lt-208: .. dropdown:: lt (pd_test_3_all.cpp:330) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 320 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_comparison_ops failed: eq() shape"); } // Test ne() pandas::DataFrame ne_result = df1.ne(df2); if (ne_result.nrows() != 3 || ne_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : ne() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: ne() shape"); } // Test lt() pandas::DataFrame lt_result = df1.lt(df2); if (lt_result.nrows() != 3 || lt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : lt() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: lt() shape"); } // Test le() pandas::DataFrame le_result = df1.le(df2); if (le_result.nrows() != 3 || le_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : le() shape mismatch" << std::endl; .. _example-series-ne-209: .. dropdown:: ne (pd_test_3_all.cpp:323) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 313 :emphasize-lines: 11 pandas::DataFrame df1(data1); pandas::DataFrame df2(data2); // Test eq() pandas::DataFrame eq_result = df1.eq(df2); if (eq_result.nrows() != 3 || eq_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : eq() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: eq() shape"); } // Test ne() pandas::DataFrame ne_result = df1.ne(df2); if (ne_result.nrows() != 3 || ne_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : ne() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: ne() shape"); } // Test lt() pandas::DataFrame lt_result = df1.lt(df2); if (lt_result.nrows() != 3 || lt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : lt() shape mismatch" << std::endl; .. _example-series-ne-210: .. dropdown:: ne (pd_test_3_all.cpp:323) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 313 :emphasize-lines: 11 pandas::DataFrame df1(data1); pandas::DataFrame df2(data2); // Test eq() pandas::DataFrame eq_result = df1.eq(df2); if (eq_result.nrows() != 3 || eq_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : eq() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: eq() shape"); } // Test ne() pandas::DataFrame ne_result = df1.ne(df2); if (ne_result.nrows() != 3 || ne_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : ne() shape mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_comparison_ops failed: ne() shape"); } // Test lt() pandas::DataFrame lt_result = df1.lt(df2); if (lt_result.nrows() != 3 || lt_result.ncols() != 2) { std::cout << " [FAIL] : in pd_test_3_all_comparison_ops() : lt() shape mismatch" << std::endl; .. _example-series-argsort-211: .. dropdown:: argsort (pd_test_1_all.cpp:1304) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1294 :emphasize-lines: 11 std::cout << "========= DatetimeArray: sorting ======================= "; pandas::DatetimeArray arr(std::vector{ "2023-06-15", "NaT", "2023-01-01", "2023-12-31" }); // argsort ascending auto indices = arr.argsort(true, "last"); // Expected order: 2023-01-01(2), 2023-06-15(0), 2023-12-31(3), NaT(1) if (indices.getElementAt({0}) != 2) { std::cout << " [FAIL] : argsort: first should be index 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argsort first"); } if (indices.getElementAt({3}) != 1) { std::cout << " [FAIL] : argsort: last should be index 1 (NaT)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: NaT position"); } .. _example-series-rank-212: .. dropdown:: rank (pd_test_1_all.cpp:6451) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6441 :emphasize-lines: 11 // ===================================================================== // Test: Rank // ===================================================================== void pd_test_dataframe_rank() { std::cout << "========= rank ============================="; // Test Series rank with default method (average) { std::vector data = {3.0, 1.0, 4.0, 1.0, 5.0}; pandas::Series s(data, "test"); auto ranked = s.rank(); // Values: 3, 1, 4, 1, 5 -> Sorted: 1, 1, 3, 4, 5 // Ranks (average): 1.5, 1.5, 3, 4, 5 // Original positions: 3->3, 1->1.5, 4->4, 1->1.5, 5->5 double r0 = std::stod(ranked.get_value_str(0)); // 3.0 -> rank 3 double r1 = std::stod(ranked.get_value_str(1)); // 1.0 -> rank 1.5 if (std::abs(r0 - 3.0) > 1e-10) { std::cout << " [FAIL] : in pd_test_dataframe_rank() : value 3.0 should have rank 3, got " << r0 << std::endl; throw std::runtime_error("pd_test_dataframe_rank failed: value 3.0 rank"); .. _example-series-searchsorted-213: .. dropdown:: searchsorted (pd_test_1_all.cpp:18958) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18948 :emphasize-lines: 11 // ========================================================================= // Search Tests // ========================================================================= void pd_test_range_index_searchsorted() { std::cout << "========= searchsorted ================================ "; pandas::RangeIndex ri(0, 10, 2); // [0, 2, 4, 6, 8] bool passed = (ri.searchsorted(4, "left") == 2 && ri.searchsorted(4, "right") == 3 && ri.searchsorted(3, "left") == 2 && // 3 would go between 2 and 4 ri.searchsorted(-1, "left") == 0 && // Before all ri.searchsorted(10, "left") == 5); // After all if (!passed) { std::cout << " [FAIL] : searchsorted" << std::endl; throw std::runtime_error("pd_test_range_index_searchsorted failed"); } .. _example-series-sort_index-214: .. dropdown:: sort_index (pd_test_3_all.cpp:583) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 573 :emphasize-lines: 11 // 10/2=5 if (std::abs(truediv_result[static_cast(0)] - 5.0) > 0.001) { std::cout << " [FAIL] : in pd_test_3_all_series_arithmetic() : truediv() value mismatch" << std::endl; throw std::runtime_error("pd_test_3_all_series_arithmetic failed: truediv() value"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_series_sort_index() { std::cout << "========= Series.sort_index() ========================"; // NOTE: Series.sort_index() has an implementation issue: // It calls index_->argsort() but argsort() is not virtual in IndexBase. // This test verifies the function signature exists. // When the implementation is fixed, this test should be updated. std::vector vals = {30.0, 10.0, 20.0}; pandas::Series s(vals, "test"); // Verify the Series was created correctly .. _example-series-sort_values-215: .. dropdown:: sort_values (pd_test_1_all.cpp:6408) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6398 :emphasize-lines: 11 void pd_test_dataframe_sorting() { std::cout << "========= sorting =========================="; std::map> data; data["A"] = {3.0, 1.0, 4.0, 1.0, 5.0}; data["B"] = {9.0, 2.0, 6.0, 5.0, 3.0}; pandas::DataFrame df(data); // Test sort_values ascending auto sorted_asc = df.sort_values("A", true); // First value should be smallest (1.0) std::string first_val = sorted_asc["A"].get_value_str(0); if (std::stod(first_val) != 1.0) { std::cout << " [FAIL] : in pd_test_dataframe_sorting() : sort_values asc first != 1" << std::endl; throw std::runtime_error("pd_test_dataframe_sorting failed: sort_values asc first != 1"); } // Test sort_values descending auto sorted_desc = df.sort_values("A", false); first_val = sorted_desc["A"].get_value_str(0); .. _example-series-t_-216: .. dropdown:: T_ (pd_test_1_all.cpp:16634) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16624 :emphasize-lines: 11 // ===================================================================== // Transpose Tests // ===================================================================== void pd_test_ndframe_transpose() { std::cout << "========= transpose ============================================" << std::endl; pandas::Series s({1, 2, 3}); // For Series, T_() returns a copy auto transposed = s.T_(); bool passed = transposed.size() == s.size(); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_transpose() : T_() size" << std::endl; throw std::runtime_error("pd_test_ndframe_transpose failed: T_() size"); } passed = transposed[0] == 1 && transposed[1] == 2 && transposed[2] == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_transpose() : T_() values" << std::endl; .. _example-series-explode-217: .. dropdown:: explode (pd_test_1_all.cpp:6868) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6858 :emphasize-lines: 11 } } // Test explode { std::map> data; data["id"] = {"1", "2"}; data["tags"] = {"a,b,c", "d,e"}; pandas::DataFrame df(data); auto exploded = df.explode("tags"); if (exploded.nrows() != 5) { // 3 + 2 = 5 rows std::cout << " [FAIL] : in pd_test_dataframe_reshape() : explode nrows != 5, got " << exploded.nrows() << std::endl; throw std::runtime_error("pd_test_dataframe_reshape failed: explode nrows"); } } // Test squeeze { std::map> data; data["A"] = {1}; .. _example-series-squeeze-218: .. dropdown:: squeeze (pd_test_1_all.cpp:6881) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6871 :emphasize-lines: 11 throw std::runtime_error("pd_test_dataframe_reshape failed: explode nrows"); } } // Test squeeze { std::map> data; data["A"] = {1}; pandas::DataFrame df(data); auto squeezed = df.squeeze(); // Should return without error for 1x1 DataFrame } // Test stack { std::map> data; data["A"] = {1, 2}; data["B"] = {3, 4}; pandas::DataFrame df(data); .. _example-series-swapaxes-219: .. dropdown:: swapaxes (pd_test_3_all.cpp:2276) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2266 :emphasize-lines: 11 auto sorted_desc = arr.sort_values(false, "last"); if (*sorted_desc[0] != "c" || *sorted_desc[1] != "b" || *sorted_desc[2] != "a" || sorted_desc[3].has_value()) { throw std::runtime_error("sort_values descending failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_categorical_swapaxes() { std::cout << "========= CategoricalArray.swapaxes() ================="; std::vector> values = {"a", "b", "c"}; pandas::CategoricalArray arr(values); auto result = arr.swapaxes(0, 0); if (result.size() != 3) { throw std::runtime_error("swapaxes failed"); } bool threw = false; .. _example-series-to_frame-220: .. dropdown:: to_frame (pd_test_3_all.cpp:4931) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4921 :emphasize-lines: 11 size_t usage = mi.memory_usage(true); if (usage == 0) { throw std::runtime_error("memory_usage() should return > 0"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_multiindex_to_frame() { std::cout << "========= MultiIndex.to_frame() ======================="; std::vector> arrays = {{"a", "b"}, {"x", "y"}}; std::vector> names = {"first", "second"}; pandas::MultiIndex mi = pandas::MultiIndex::from_arrays(arrays, names); auto frame = mi.to_frame(); if (frame.find("first") == frame.end() || frame.find("second") == frame.end()) { throw std::runtime_error("to_frame() missing columns"); } .. _example-series-transpose-221: .. dropdown:: transpose (pd_test_1_all.cpp:16648) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16638 :emphasize-lines: 11 std::cout << " [FAIL] : in pd_test_ndframe_transpose() : T_() size" << std::endl; throw std::runtime_error("pd_test_ndframe_transpose failed: T_() size"); } passed = transposed[0] == 1 && transposed[1] == 2 && transposed[2] == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_transpose() : T_() values" << std::endl; throw std::runtime_error("pd_test_ndframe_transpose failed: T_() values"); } // Test transpose() alias auto transposed2 = s.transpose(); passed = transposed2.size() == s.size(); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_transpose() : transpose() size" << std::endl; throw std::runtime_error("pd_test_ndframe_transpose failed: transpose() size"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-unstack-222: .. dropdown:: unstack (pd_test_3_all.cpp:1739) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1729 :emphasize-lines: 11 } if (s.size() != 3) { std::cout << " [FAIL] : in pd_test_3_all_chainable_mutators() : Case H size" << std::endl; throw std::runtime_error("pd_test_3_all_chainable_mutators failed: Case H size"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_dataframe_unstack() { std::cout << "========= DataFrame.unstack() ========================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // Without MultiIndex, unstack() returns self (matches pandas behavior) pandas::DataFrame result = df.unstack(); .. _example-series-align-223: .. dropdown:: align (pd_test_1_all.cpp:14035) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14025 :emphasize-lines: 11 if (!approx_equal(a_s1, 2.0) || !approx_equal(a_o1, 99.0)) { passed = false; std::cout << " [FAIL] : in pd_test_joining_compare() : difference at row 1 not shown" << std::endl; throw std::runtime_error("pd_test_joining_compare failed: diff values"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // align() Tests // ===================================================================== void pd_test_joining_align() { std::cout << "========= align ======================================="; std::map> left_data = { {"A", {1.0, 2.0}} }; std::vector left_idx = {"x", "y"}; pandas::DataFrame left(left_data, std::make_unique>(left_idx)); .. _example-series-aligned_binary_op-224: .. dropdown:: aligned_binary_op (pd_test_3_all.cpp:26690) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26680 :emphasize-lines: 11 void pd_test_aligned_arith_same_index() { std::cout << " same index add ... "; ::pandas::Series<::numpy::float64> s1({1.0, 2.0, 3.0}, std::string("val")); s1.set_index(std::make_unique<::pandas::Index>( std::vector{"a", "b", "c"})); ::pandas::Series<::numpy::float64> s2({10.0, 20.0, 30.0}, std::string("val")); s2.set_index(std::make_unique<::pandas::Index>( std::vector{"a", "b", "c"})); auto result = s1.aligned_binary_op(s2, [](double a, double b) { return a + b; }); check(result.size() == 3, "size == 3"); check(result[0] == 11.0, "a -> 11"); check(result[1] == 22.0, "b -> 22"); check(result[2] == 33.0, "c -> 33"); check(result.name() == "val", "name preserved"); std::cout << "test passed" << std::endl; } void pd_test_aligned_arith_different_index() { std::cout << " different index add ... "; .. _example-series-aligned_binary_op_cross-225: .. dropdown:: aligned_binary_op_cross (pd_test_3_all.cpp:26821) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26811 :emphasize-lines: 11 void pd_test_aligned_arith_cross_type() { std::cout << " cross-type alignment (int64 x float64) ... "; ::pandas::Series<::numpy::int64> s1({1, 2, 3}, std::string("v")); s1.set_index(std::make_unique<::pandas::Index>( std::vector{"a", "b", "c"})); ::pandas::Series<::numpy::float64> s2({10.5, 20.5, 30.5}, std::string("v")); s2.set_index(std::make_unique<::pandas::Index>( std::vector{"b", "c", "d"})); auto result = s1.aligned_binary_op_cross(s2, [](double a, double b) { return a + b; }); check(result.size() == 4, "size == 4"); check(std::isnan(result[0]), "a -> NaN"); check(result[1] == 12.5, "b -> 12.5"); check(result[2] == 23.5, "c -> 23.5"); check(std::isnan(result[3]), "d -> NaN"); std::cout << "test passed" << std::endl; } void pd_test_aligned_arith_dtype_coercion() { // Tests dtype coercion rules matching pandas 2.x behavior for all 5-type combos. .. _example-series-combine-226: .. dropdown:: combine (pd_test_2_all.cpp:1700) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1690 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_between_time test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_between_time.cpp (end) ----------------------------- // ------------------- pd_test_combine.cpp (start) ----------------------------- // dataframe_tests/pd_test_combine.cpp // Test for DataFrame.combine() - column-wise combine with another DataFrame #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { namespace dataframe_tests_combine { .. _example-series-combine_first-227: .. dropdown:: combine_first (pd_test_1_all.cpp:13889) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13879 :emphasize-lines: 11 if (!approx_equal(b1, 10.0) || !approx_equal(b2, 20.0)) { passed = false; std::cout << " [FAIL] : in pd_test_joining_join_index() : matched rows wrong" << std::endl; throw std::runtime_error("pd_test_joining_join_index failed: match values"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // combine_first() Tests // ===================================================================== void pd_test_joining_combine_first() { std::cout << "========= combine_first ==============================="; std::map> left_data = { {"A", {1.0, std::nan(""), 3.0}}, {"B", {std::nan(""), 5.0, std::nan("")}} }; std::vector left_idx = {"x", "y", "z"}; .. _example-series-concat-228: .. dropdown:: concat (pd_test_1_all.cpp:17717) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 17707 :emphasize-lines: 11 } void pd_test_period_index_concat() { std::cout << "========= concat factory =============================="; std::vector ordinals1 = {0, 1}; std::vector ordinals2 = {2, 3}; pandas::PeriodIndex idx1(ordinals1, "D"); pandas::PeriodIndex idx2(ordinals2, "D"); pandas::PeriodIndex concatenated = pandas::PeriodIndex::concat({idx1, idx2}); bool passed = (concatenated.size() == 4); if (!passed) { std::cout << " [FAIL] : in pd_test_period_index_concat()" << std::endl; throw std::runtime_error("pd_test_period_index_concat failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-asfreq-229: .. dropdown:: asfreq (pd_test_1_all.cpp:2869) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2859 :emphasize-lines: 11 std::cout << "========= PeriodArray: asfreq ======================= "; // Monthly to quarterly pandas::PeriodArray arr_m(std::vector{ "2024-01", "2024-04", "2024-07", "NaT" }, "M"); auto arr_q = arr_m.asfreq("Q"); if (arr_q.size() != 4) { std::cout << " [FAIL] : asfreq size should be 4" << std::endl; throw std::runtime_error("pd_test_period_array_asfreq failed: size"); } if (arr_q.freqstr() != "Q") { std::cout << " [FAIL] : asfreq freqstr should be 'Q'" << std::endl; throw std::runtime_error("pd_test_period_array_asfreq failed: freqstr"); } // Check NaT is preserved .. _example-series-asof-230: .. dropdown:: asof (pd_test_2_all.cpp:366) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 356 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_add_prefix test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_add_prefix.cpp (end) ----------------------------- // ------------------- pd_test_asof.cpp (start) ----------------------------- // dataframe_tests/pd_test_asof.cpp // Test for DataFrame.asof() method #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { .. _example-series-asof-231: .. dropdown:: asof (pd_test_2_all.cpp:366) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 356 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_add_prefix test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_add_prefix.cpp (end) ----------------------------- // ------------------- pd_test_asof.cpp (start) ----------------------------- // dataframe_tests/pd_test_asof.cpp // Test for DataFrame.asof() method #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { .. _example-series-asof-232: .. dropdown:: asof (pd_test_2_all.cpp:366) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 356 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_add_prefix test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_add_prefix.cpp (end) ----------------------------- // ------------------- pd_test_asof.cpp (start) ----------------------------- // dataframe_tests/pd_test_asof.cpp // Test for DataFrame.asof() method #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives namespace dataframe_tests { .. _example-series-between_time-233: .. dropdown:: between_time (pd_test_2_all.cpp:1154) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1144 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_at_time test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_at_time.cpp (end) ----------------------------- // ------------------- pd_test_between_time.cpp (start) ----------------------------- // dataframe_tests/pd_test_between_time.cpp // Tests for DataFrame.between_time() method (pandas 2.0+ API) // Selects values between particular times of day from datetime-indexed DataFrame #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-diff-234: .. dropdown:: diff (pd_test_1_all.cpp:5171) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5161 :emphasize-lines: 11 } void pd_test_arithmetic_dataframe_diff_shift() { std::cout << "========= DataFrame diff/shift =================="; std::map> data; data["A"] = {1.0, 3.0, 6.0, 10.0}; pandas::DataFrame df(data); // diff: [NaN, 2, 3, 4] auto d = df.diff(); std::string val = d["A"].get_value_str(1); bool passed = std::abs(std::stod(val) - 2.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : diff failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: diff failed"); } // First element should be NaN val = d["A"].get_value_str(0); passed = std::isnan(std::stod(val)); .. _example-series-pct_change-235: .. dropdown:: pct_change (pd_test_1_all.cpp:4621) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4611 :emphasize-lines: 11 throw std::runtime_error("pd_test_aggregation_series_skew_kurt failed: kurtosis alias failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_series_pct_change() { std::cout << "========= Series pct_change ====================="; pandas::Series s({100.0, 110.0, 121.0}); auto pct = s.pct_change(); // First element should be NaN bool passed = std::isnan(pct[0]); if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_pct_change() : first element should be NaN" << std::endl; throw std::runtime_error("pd_test_aggregation_series_pct_change failed: first element should be NaN"); } // Second element should be 0.1 (10% increase) passed = std::abs(pct[1] - 0.1) < 0.001; .. _example-series-shift-236: .. dropdown:: shift (pd_test_1_all.cpp:5188) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5178 :emphasize-lines: 11 // First element should be NaN val = d["A"].get_value_str(0); passed = std::isnan(std::stod(val)); if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : diff NaN failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: diff NaN failed"); } // shift: [NaN, 1, 3, 6] auto s = df.shift(); val = s["A"].get_value_str(1); passed = std::abs(std::stod(val) - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : shift failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: shift failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-to_period-237: .. dropdown:: to_period (pd_test_2_all.cpp:14554) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14544 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_parquet test suite ========================" << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_parquet.cpp (end) ----------------------------- // ------------------- pd_test_to_period.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_period.cpp // Test suite for DataFrame.to_period() method #include #include #include #include #include #include "../pandas/pd_dataframe.h" // CRITICAL: No using namespace directives .. _example-series-to_timestamp-238: .. dropdown:: to_timestamp (pd_test_1_all.cpp:2830) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2820 :emphasize-lines: 11 void pd_test_period_array_to_timestamp() { std::cout << "========= PeriodArray: to_timestamp ======================= "; pandas::PeriodArray arr(std::vector{ "2024-01", "2024-06", "NaT" }, "M"); // to_timestamp with start auto ts_start = arr.to_timestamp("start"); if (ts_start.size() != 3) { std::cout << " [FAIL] : to_timestamp size should be 3" << std::endl; throw std::runtime_error("pd_test_period_array_to_timestamp failed: size"); } auto ts0 = ts_start[0]; if (!ts0.has_value()) { std::cout << " [FAIL] : ts_start[0] should have value" << std::endl; throw std::runtime_error("pd_test_period_array_to_timestamp failed: ts_start[0]"); } .. _example-series-tz_convert-239: .. dropdown:: tz_convert (pd_test_2_all.cpp:17874) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 17864 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_transform test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_transform.cpp (end) ----------------------------- // ------------------- pd_test_tz_convert.cpp (start) ----------------------------- // dataframe_tests/pd_test_tz_convert.cpp // Test for DataFrame.tz_convert() method #include #include #include #include "../pandas/pd_dataframe.h" namespace dataframe_tests { namespace dataframe_tests_tz_convert { void pd_test_tz_convert_basic() { .. _example-series-tz_localize-240: .. dropdown:: tz_localize (pd_test_1_all.cpp:1431) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1421 :emphasize-lines: 11 "2023-06-15" }); // Initially should be timezone-naive if (arr.is_tz_aware()) { std::cout << " [FAIL] : array should be timezone-naive initially" << std::endl; throw std::runtime_error("pd_test_datetime_array_timezone failed: naive"); } // Localize to UTC auto localized = arr.tz_localize("UTC"); if (!localized.is_tz_aware()) { std::cout << " [FAIL] : localized array should be timezone-aware" << std::endl; throw std::runtime_error("pd_test_datetime_array_timezone failed: localize"); } // Verify timezone name in dtype auto dt = localized.dtype(); if (!dt.is_tz_aware()) { std::cout << " [FAIL] : dtype should be timezone-aware" << std::endl; throw std::runtime_error("pd_test_datetime_array_timezone failed: dtype tz"); .. _example-series-to_clipboard-241: .. dropdown:: to_clipboard (pd_test_2_all.cpp:10176) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10166 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_swaplevel test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_swaplevel.cpp (end) ----------------------------- // ------------------- pd_test_to_clipboard.cpp (start) ----------------------------- // pd_test_to_clipboard.cpp // Tests for DataFrame.to_clipboard() method #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-to_csv-242: .. dropdown:: to_csv (pd_test_1_all.cpp:6967) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6957 :emphasize-lines: 11 void pd_test_dataframe_io() { std::cout << "========= I/O methods ======================"; std::map> data; data["A"] = {1, 2, 3}; data["B"] = {4, 5, 6}; pandas::DataFrame df(data); // Test to_csv std::string csv = df.to_csv(false); if (csv.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_io() : to_csv empty" << std::endl; throw std::runtime_error("pd_test_dataframe_io failed: to_csv empty"); } if (csv.find("A") == std::string::npos) { std::cout << " [FAIL] : in pd_test_dataframe_io() : to_csv missing column name" << std::endl; throw std::runtime_error("pd_test_dataframe_io failed: to_csv missing column name"); } // Test to_json .. _example-series-to_dict-243: .. dropdown:: to_dict (pd_test_1_all.cpp:13343) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13333 :emphasize-lines: 11 void pd_test_io_to_dict() { std::cout << "========= to_dict ================================"; std::map> data; data["A"] = {1.0, 2.0, 3.0}; data["B"] = {4.0, 5.0, 6.0}; pandas::DataFrame df(data); // Test list orientation auto dict_list = df.to_dict("list"); bool passed = (dict_list.count("A") > 0 && dict_list.count("B") > 0); passed = passed && (dict_list["A"].size() == 3); passed = passed && (dict_list["B"].size() == 3); if (!passed) { std::cout << " [FAIL] : in pd_test_io_to_dict() : to_dict list failed" << std::endl; throw std::runtime_error("pd_test_io_to_dict failed"); } .. _example-series-to_excel-244: .. dropdown:: to_excel (pd_test_2_all.cpp:3427) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3417 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_eval test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_eval.cpp (end) ----------------------------- // ------------------- pd_test_excel.cpp (start) ----------------------------- // dataframe_tests/pd_test_excel.cpp // Test file for DataFrame.to_excel() method #include #include #include #include #include #include #include #include .. _example-series-to_feather-245: .. dropdown:: to_feather (pd_test_2_all.cpp:11158) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11148 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_dict test suite ==========================" << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_dict.cpp (end) ----------------------------- // ------------------- pd_test_to_feather.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_feather.cpp // Comprehensive tests for DataFrame.to_feather() method (pandas-compatible) #include #include #include #include #include #include #include #include #include .. _example-series-to_hdf-246: .. dropdown:: to_hdf (pd_test_2_all.cpp:11613) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11603 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_feather test suite ==========================" << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_feather.cpp (end) ----------------------------- // ------------------- pd_test_to_hdf.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_hdf.cpp // Tests for DataFrame.to_hdf() method - Updated for real HDF5 format #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-to_json-247: .. dropdown:: to_json (pd_test_1_all.cpp:6978) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6968 :emphasize-lines: 11 if (csv.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_io() : to_csv empty" << std::endl; throw std::runtime_error("pd_test_dataframe_io failed: to_csv empty"); } if (csv.find("A") == std::string::npos) { std::cout << " [FAIL] : in pd_test_dataframe_io() : to_csv missing column name" << std::endl; throw std::runtime_error("pd_test_dataframe_io failed: to_csv missing column name"); } // Test to_json std::string json = df.to_json("columns"); if (json.empty()) { std::cout << " [FAIL] : in pd_test_dataframe_io() : to_json empty" << std::endl; throw std::runtime_error("pd_test_dataframe_io failed: to_json empty"); } if (json.find("{") == std::string::npos) { std::cout << " [FAIL] : in pd_test_dataframe_io() : to_json not JSON" << std::endl; throw std::runtime_error("pd_test_dataframe_io failed: to_json not JSON"); } // Test to_string .. _example-series-to_latex-248: .. dropdown:: to_latex (pd_test_2_all.cpp:9446) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9436 :emphasize-lines: 11 void pd_test_styler_to_latex() { std::cout << "========= to_latex ================================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); auto styler = df.style(); std::string latex = styler.to_latex(); if (!contains(latex, "\\begin{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not produce tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not produce tabular environment"); } if (!contains(latex, "\\end{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not close tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not close tabular environment"); } .. _example-series-to_latex-249: .. dropdown:: to_latex (pd_test_2_all.cpp:9446) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9436 :emphasize-lines: 11 void pd_test_styler_to_latex() { std::cout << "========= to_latex ================================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); auto styler = df.style(); std::string latex = styler.to_latex(); if (!contains(latex, "\\begin{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not produce tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not produce tabular environment"); } if (!contains(latex, "\\end{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not close tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not close tabular environment"); } .. _example-series-to_latex-250: .. dropdown:: to_latex (pd_test_2_all.cpp:9446) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9436 :emphasize-lines: 11 void pd_test_styler_to_latex() { std::cout << "========= to_latex ================================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); auto styler = df.style(); std::string latex = styler.to_latex(); if (!contains(latex, "\\begin{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not produce tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not produce tabular environment"); } if (!contains(latex, "\\end{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not close tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not close tabular environment"); } .. _example-series-to_list-251: .. dropdown:: to_list (pd_test_1_all.cpp:10247) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10237 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_to_list() { std::cout << "========= to_list ========================="; pandas::CategoricalArray arr({"x", "y", "z"}); pandas::CategoricalIndex idx(arr); auto list = idx.to_list(); bool passed = (list.size() == 3 && list[0].has_value() && *list[0] == "x" && list[1].has_value() && *list[1] == "y" && list[2].has_value() && *list[2] == "z"); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_to_list() : to_list check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_to_list failed"); } .. _example-series-to_markdown-252: .. dropdown:: to_markdown (pd_test_1_all.cpp:13466) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13456 :emphasize-lines: 11 void pd_test_io_to_markdown() { std::cout << "========= to_markdown ============================"; std::map> data; data["X"] = {10.0, 20.0}; data["Y"] = {30.0, 40.0}; pandas::DataFrame df(data); std::string md = df.to_markdown(); // Check for markdown table elements bool has_pipe = (md.find("|") != std::string::npos); bool has_separator = (md.find("---") != std::string::npos); bool passed = has_pipe && has_separator; if (!passed) { std::cout << " [FAIL] : in pd_test_io_to_markdown() : invalid markdown format" << std::endl; throw std::runtime_error("pd_test_io_to_markdown failed"); .. _example-series-to_markdown-253: .. dropdown:: to_markdown (pd_test_1_all.cpp:13466) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13456 :emphasize-lines: 11 void pd_test_io_to_markdown() { std::cout << "========= to_markdown ============================"; std::map> data; data["X"] = {10.0, 20.0}; data["Y"] = {30.0, 40.0}; pandas::DataFrame df(data); std::string md = df.to_markdown(); // Check for markdown table elements bool has_pipe = (md.find("|") != std::string::npos); bool has_separator = (md.find("---") != std::string::npos); bool passed = has_pipe && has_separator; if (!passed) { std::cout << " [FAIL] : in pd_test_io_to_markdown() : invalid markdown format" << std::endl; throw std::runtime_error("pd_test_io_to_markdown failed"); .. _example-series-to_markdown-254: .. dropdown:: to_markdown (pd_test_1_all.cpp:13466) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13456 :emphasize-lines: 11 void pd_test_io_to_markdown() { std::cout << "========= to_markdown ============================"; std::map> data; data["X"] = {10.0, 20.0}; data["Y"] = {30.0, 40.0}; pandas::DataFrame df(data); std::string md = df.to_markdown(); // Check for markdown table elements bool has_pipe = (md.find("|") != std::string::npos); bool has_separator = (md.find("---") != std::string::npos); bool passed = has_pipe && has_separator; if (!passed) { std::cout << " [FAIL] : in pd_test_io_to_markdown() : invalid markdown format" << std::endl; throw std::runtime_error("pd_test_io_to_markdown failed"); .. _example-series-to_numpy-255: .. dropdown:: to_numpy (pd_test_1_all.cpp:16764) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16754 :emphasize-lines: 11 // ===================================================================== // to_numpy Tests // ===================================================================== void pd_test_ndframe_to_numpy() { std::cout << "========= to_numpy =============================================" << std::endl; pandas::Series s({10, 20, 30}); auto arr = s.to_numpy(); bool passed = arr.getSize() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_to_numpy() : size" << std::endl; throw std::runtime_error("pd_test_ndframe_to_numpy failed: size"); } passed = arr.getElementAt({0}) == 10 && arr.getElementAt({1}) == 20 && arr.getElementAt({2}) == 30; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_to_numpy() : values" << std::endl; .. _example-series-to_numpy-256: .. dropdown:: to_numpy (pd_test_1_all.cpp:16764) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16754 :emphasize-lines: 11 // ===================================================================== // to_numpy Tests // ===================================================================== void pd_test_ndframe_to_numpy() { std::cout << "========= to_numpy =============================================" << std::endl; pandas::Series s({10, 20, 30}); auto arr = s.to_numpy(); bool passed = arr.getSize() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_to_numpy() : size" << std::endl; throw std::runtime_error("pd_test_ndframe_to_numpy failed: size"); } passed = arr.getElementAt({0}) == 10 && arr.getElementAt({1}) == 20 && arr.getElementAt({2}) == 30; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_to_numpy() : values" << std::endl; .. _example-series-to_numpy-257: .. dropdown:: to_numpy (pd_test_1_all.cpp:16764) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16754 :emphasize-lines: 11 // ===================================================================== // to_numpy Tests // ===================================================================== void pd_test_ndframe_to_numpy() { std::cout << "========= to_numpy =============================================" << std::endl; pandas::Series s({10, 20, 30}); auto arr = s.to_numpy(); bool passed = arr.getSize() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_to_numpy() : size" << std::endl; throw std::runtime_error("pd_test_ndframe_to_numpy failed: size"); } passed = arr.getElementAt({0}) == 10 && arr.getElementAt({1}) == 20 && arr.getElementAt({2}) == 30; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_to_numpy() : values" << std::endl; .. _example-series-to_orc-258: .. dropdown:: to_orc (pd_test_2_all.cpp:13769) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13759 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_markdown test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_markdown.cpp (end) ----------------------------- // ------------------- pd_test_to_orc.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_orc.cpp // Tests for DataFrame.to_orc() method #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-to_parquet-259: .. dropdown:: to_parquet (pd_test_2_all.cpp:14117) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14107 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_orc test suite ==========================" << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_orc.cpp (end) ----------------------------- // ------------------- pd_test_to_parquet.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_parquet.cpp // Tests for DataFrame.to_parquet() method #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-to_pickle-260: .. dropdown:: to_pickle (pd_test_2_all.cpp:14906) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14896 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_period test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_period.cpp (end) ----------------------------- // ------------------- pd_test_to_pickle.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_pickle.cpp // Tests for DataFrame.to_pickle() method #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-to_sql-261: .. dropdown:: to_sql (pd_test_2_all.cpp:15576) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15566 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_records test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_records.cpp (end) ----------------------------- // ------------------- pd_test_to_sql.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_sql.cpp // Tests for DataFrame.to_sql() method #include #include #include #include #include #include #include #include .. _example-series-to_stata-262: .. dropdown:: to_stata (pd_test_2_all.cpp:16133) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16123 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_sql test suite ==========================" << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_sql.cpp (end) ----------------------------- // ------------------- pd_test_to_stata.cpp (start) ----------------------------- // pd_test_to_stata.cpp // Tests for DataFrame.to_stata() method #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-to_string-263: .. 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-series-to_string_full-264: .. dropdown:: to_string_full (pd_test_5_all.cpp:64951) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 64941 :emphasize-lines: 11 static pandas::Series make_s( const std::vector& v, const std::optional& name = std::nullopt) { return pandas::Series(v, name); } static const double PINF = std::numeric_limits::infinity(); static const double NINF = -std::numeric_limits::infinity(); static const double DNAN = std::numeric_limits::quiet_NaN(); // Convenience: call to_string_full() with pandas defaults (index=true, // length=false, dtype=true, name=true). We strip the trailer here too so we // match `pd.Series(...).to_string()`'s captured output, which OMITS the dtype // trailer (Python `Series.to_string()` defaults `dtype=False`). static std::string call_to_string_full( const pandas::Series& s) { std::string raw = s.to_string_full( /*buf=*/nullptr, /*na_rep=*/"NaN", /*float_format=*/"", /*header=*/false, .. _example-series-to_string_vector-265: .. 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-series-to_xarray-266: .. dropdown:: to_xarray (pd_test_2_all.cpp:16928) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16918 :emphasize-lines: 11 std::cout << "====================================== [OK] pd_test_to_timestamp test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_to_timestamp.cpp (end) ----------------------------- // ------------------- pd_test_to_xarray.cpp (start) ----------------------------- // dataframe_tests/pd_test_to_xarray.cpp // Test for DataFrame.to_xarray() - Convert DataFrame to xarray Dataset #include #include #include #include #include #include #include #include "../pandas/pd_dataframe.h" .. _example-series-tolist-267: .. dropdown:: tolist (pd_test_3_all.cpp:2300) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2290 :emphasize-lines: 11 threw = true; } if (!threw) { throw std::runtime_error("swapaxes should throw for invalid axes"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_categorical_to_list() { std::cout << "========= CategoricalArray.to_list()/tolist() ========="; std::vector> values = {"a", "b", std::nullopt, "c"}; pandas::CategoricalArray arr(values); auto list = arr.to_list(); if (list.size() != 4 || *list[0] != "a" || *list[1] != "b" || list[2].has_value() || *list[3] != "c") { throw std::runtime_error("to_list failed"); } .. _example-series-astype-268: .. dropdown:: astype (pd_test_1_all.cpp:21292) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21282 :emphasize-lines: 11 std::cout << "========= astype all columns to float64 ============="; // Create DataFrame with int64 columns std::map> data; data["A"] = {1, 2, 3, 4, 5}; data["B"] = {10, 20, 30, 40, 50}; pandas::DataFrame df(data); // Convert all columns to float64 pandas::DataFrame df_float = df.astype("float64"); // Verify dtype changed pandas::Series dtypes = df_float.dtypes(); bool passed = true; if (dtypes[static_cast(0)] != "float64") { std::cout << " [FAIL] : in pd_test_astype_all_columns_to_float64() : column A dtype is " << dtypes[static_cast(0)] << ", expected float64" << std::endl; passed = false; } if (dtypes[static_cast(1)] != "float64") { .. _example-series-astype-269: .. dropdown:: astype (pd_test_1_all.cpp:21292) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21282 :emphasize-lines: 11 std::cout << "========= astype all columns to float64 ============="; // Create DataFrame with int64 columns std::map> data; data["A"] = {1, 2, 3, 4, 5}; data["B"] = {10, 20, 30, 40, 50}; pandas::DataFrame df(data); // Convert all columns to float64 pandas::DataFrame df_float = df.astype("float64"); // Verify dtype changed pandas::Series dtypes = df_float.dtypes(); bool passed = true; if (dtypes[static_cast(0)] != "float64") { std::cout << " [FAIL] : in pd_test_astype_all_columns_to_float64() : column A dtype is " << dtypes[static_cast(0)] << ", expected float64" << std::endl; passed = false; } if (dtypes[static_cast(1)] != "float64") { .. _example-series-astype_dtype-270: .. dropdown:: astype_dtype (pd_test_5_all.cpp:43633) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 43623 :emphasize-lines: 11 "0 a\n" "1 b\n" "2 c"; check_case("dtype_extension_dt_complex_fallback_925116_case_6", df, actual, expected, "string", local_fail); } void f_dtype_extension_dt_complex_fallback_925116_case_7_series_string_astype_string_drops_override(int& local_fail) { std::cout << "-- case_7_series_string_astype_string_drops_override\n"; pandas::Series s({"a", "b", "c"}); auto r_box = s.astype_dtype("string"); auto* r = dynamic_cast*>(r_box.get()); if (r == nullptr) { pandas_tests::check(false, "case_7.astype_returned_non_string_series", local_fail); return; } pandas::DataFrame df = r->to_frame(std::optional("v")); std::string actual = df.to_string(); std::cout << " src_dtype=" << show_dtype(s) << " astype_result_dtype=" << show_dtype(*r) << "\n"; .. _example-series-bool_-271: .. dropdown:: bool_ (pd_test_1_all.cpp:9120) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9110 :emphasize-lines: 11 void pd_test_datetime_mixin_array_constructor() { std::cout << "========= DatetimeTDMixin array constructor ========================="; // Create DatetimeArray with some values numpy::NDArray data(std::vector{3}); data.setElementAt({0}, numpy::datetime64(1000000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2001 data.setElementAt({1}, numpy::datetime64(1500000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2017 data.setElementAt({2}, numpy::datetime64(1600000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2020 numpy::NDArray mask(std::vector{3}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); mask.setElementAt({2}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeTDMixin idx(arr, "timestamps"); bool passed = (idx.size() == 3 && !idx.empty() && idx.name().has_value() && *idx.name() == "timestamps" && idx.inferred_type() == "datetime"); if (!passed) { .. _example-series-bool_vec_to_cond-272: .. dropdown:: bool_vec_to_cond (pd_test_3_all.cpp:26108) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26098 :emphasize-lines: 11 auto result = s.where(cond, other); if (!result.name().has_value() || result.name().value() != "my_series") { throw std::runtime_error("Name not preserved"); } std::cout << "PASSED" << std::endl; } void pd_test_where_series_bool_vec() { std::cout << " bool_vec_to_cond conversion... "; std::vector bvec = {true, false, true, false, true}; auto cond_arr = ::pandas::Series<::numpy::float64>::bool_vec_to_cond(bvec); if (cond_arr.getSize() != 5) { throw std::runtime_error("Size mismatch"); } for (size_t i = 0; i < 5; ++i) { if (cond_arr.getElementAt({i}) != bvec[i]) { throw std::runtime_error("Value mismatch at index " + std::to_string(i)); } } std::cout << "PASSED" << std::endl; } .. _example-series-convert_dtypes-273: .. dropdown:: convert_dtypes (pd_test_1_all.cpp:27317) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27307 :emphasize-lines: 11 void pd_test_convert_dtypes_integer_strings() { std::cout << "========= convert_dtypes: integer strings ============"; // Create DataFrame with string column containing integers std::map> data; data["a"] = {"1", "2", "3", "4", "5"}; pandas::DataFrame df(data); // Convert dtypes pandas::DataFrame converted = df.convert_dtypes(); // After conversion, should be int64 pandas::Series dtypes_after = converted.dtypes(); std::string dtype_a = dtypes_after[static_cast(0)]; // Verify the dtype was converted to Int64 (nullable integer, per pandas convert_dtypes behavior) bool passed = (dtype_a == "Int64" || dtype_a == "int64"); if (!passed) { std::cout << " [FAIL] : in pd_test_convert_dtypes_integer_strings() : expected Int64, got " << dtype_a << std::endl; throw std::runtime_error("pd_test_convert_dtypes_integer_strings failed: dtype mismatch"); .. _example-series-copy-274: .. dropdown:: copy (pd_test_1_all.cpp:5798) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5788 :emphasize-lines: 11 // ============================================================================ // Copy/Rename Tests // ============================================================================ void pd_test_categorical_index_copy() { std::cout << "========= copy ========================================"; pandas::CategoricalArray arr({"a", "b", "c"}); pandas::CategoricalIndex idx(arr, "original"); pandas::CategoricalIndex copied = idx.copy(); bool passed = (copied.size() == idx.size() && copied.name() == idx.name() && copied.categories() == idx.categories() && copied.ordered() == idx.ordered()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_copy()" << std::endl; throw std::runtime_error("pd_test_categorical_index_copy failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-copy_series_metadata_from-275: .. dropdown:: copy_series_metadata_from (pd_test_3_all.cpp:27251) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27241 :emphasize-lines: 11 // Test 9: copy_series_metadata_from void pd_test_copy_series_metadata() { std::cout << " -- pd_test_copy_series_metadata --" << std::endl; Series source({1.0, 2.0, 3.0}, std::string("src")); source.set_index(std::make_unique>( std::vector{"a", "b", "c"})); source.set_dtype_override("int64"); Series target({10.0, 20.0, 30.0}); target.copy_series_metadata_from(source); check(target.name() == "src", "name copied"); check(target.index().size() == 3, "index copied"); check(target.dtype_name() == "int64", "dtype_override copied"); } int pd_test_apply_inference_main() { std::cout << "====================================== pd_test_apply_inference ========================== " << std::endl; g_pass = 0; g_fail = 0; try { .. _example-series-infer_objects-276: .. dropdown:: infer_objects (pd_test_1_all.cpp:27595) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27585 :emphasize-lines: 11 // Create DataFrame with string column containing integers std::map> data; data["A"] = {"1", "2", "3", "4", "5"}; pandas::DataFrame df(data); // Before inference, dtype should be string/object std::string before_dtype = df["A"].dtype_name(); // Apply infer_objects pandas::DataFrame result = df.infer_objects(); // After inference, dtype should be int64 std::string after_dtype = result["A"].dtype_name(); bool passed = (after_dtype == "int64"); if (!passed) { std::cout << " [FAIL] : in pd_test_infer_objects_integer_column() : expected int64, got " << after_dtype << std::endl; throw std::runtime_error("pd_test_infer_objects_integer_column failed"); } .. _example-series-view-277: .. dropdown:: view (pd_test_3_all.cpp:2147) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2137 :emphasize-lines: 11 throw std::runtime_error("memory_usage shallow too small"); } if (deep < shallow) { throw std::runtime_error("memory_usage deep should be >= shallow"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_categorical_ravel_view() { std::cout << "========= CategoricalArray.ravel()/view() ============="; std::vector> values = {"a", "b", "c"}; pandas::CategoricalArray arr(values); auto raveled = arr.ravel(); if (raveled.size() != 3 || !raveled.equals(arr)) { throw std::runtime_error("ravel failed"); } auto viewed = arr.view(); .. _example-series-items-278: .. dropdown:: items (pd_test_1_all.cpp:16554) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16544 :emphasize-lines: 11 // ===================================================================== // Iteration Tests (items, keys) // ===================================================================== void pd_test_ndframe_items_keys() { std::cout << "========= items/keys ===========================================" << std::endl; pandas::Series s({10, 20, 30}); // Test items() std::vector collected_keys; std::vector collected_values; s.items([&](const std::string& key, int value) { collected_keys.push_back(key); collected_values.push_back(value); }); bool passed = collected_keys.size() == 3; if (!passed) { .. _example-series-items-279: .. dropdown:: items (pd_test_1_all.cpp:16554) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16544 :emphasize-lines: 11 // ===================================================================== // Iteration Tests (items, keys) // ===================================================================== void pd_test_ndframe_items_keys() { std::cout << "========= items/keys ===========================================" << std::endl; pandas::Series s({10, 20, 30}); // Test items() std::vector collected_keys; std::vector collected_values; s.items([&](const std::string& key, int value) { collected_keys.push_back(key); collected_values.push_back(value); }); bool passed = collected_keys.size() == 3; if (!passed) { .. _example-series-keys-280: .. dropdown:: keys (pd_test_1_all.cpp:16319) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16309 :emphasize-lines: 11 } // Test default value passed = attrs.get("missing", 99) == 99; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : default value" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: default value"); } // Test keys auto keys = attrs.keys(); passed = keys.size() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : keys()" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: keys()"); } // Test remove passed = attrs.remove("count") && !attrs.contains("count"); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : remove" << std::endl; .. _example-series-duplicated-281: .. dropdown:: duplicated (pd_test_1_all.cpp:10583) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10573 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_duplicated() { std::cout << "========= duplicated ========================="; pandas::CategoricalArray arr({"a", "b", "a", "c", "a"}); pandas::CategoricalIndex idx(arr); auto dup_mask = idx.duplicated("first"); bool passed = (dup_mask.getElementAt({0}) == false && dup_mask.getElementAt({1}) == false && dup_mask.getElementAt({2}) == true && dup_mask.getElementAt({3}) == false && dup_mask.getElementAt({4}) == true); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_duplicated() : duplicated check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_duplicated failed"); } .. _example-series-isin-282: .. dropdown:: isin (pd_test_1_all.cpp:5938) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5928 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_isin() { std::cout << "========= inherited isin =============================="; pandas::CategoricalArray arr({"a", "b", "c", "d"}); pandas::CategoricalIndex idx(arr); std::vector values = {"a", "c"}; numpy::NDArray mask = idx.isin(values); bool passed = (mask.getSize() == 4 && mask.getElementAt({0}) == true && // a mask.getElementAt({1}) == false && // b mask.getElementAt({2}) == true && // c mask.getElementAt({3}) == false); // d if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_isin()" << std::endl; throw std::runtime_error("pd_test_categorical_index_isin failed"); } .. _example-series-unique-283: .. dropdown:: unique (pd_test_1_all.cpp:1345) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1335 :emphasize-lines: 11 pandas::DatetimeArray arr(std::vector{ "2023-01-01", "2023-06-15", "2023-01-01", "NaT", "2023-06-15", "NaT" }); // unique auto uniq = arr.unique(); // Should have: NaT, 2023-01-01, 2023-06-15 (3 unique values) if (uniq.size() != 3) { std::cout << " [FAIL] : unique size should be 3, got " << uniq.size() << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: size"); } // factorize auto [codes, uniques] = arr.factorize(); // Codes for NaT should be -1 if (codes.getElementAt({3}) != -1) { .. _example-series-is_na_at-284: .. dropdown:: is_na_at (pd_test_5_all.cpp:35205) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 35195 :emphasize-lines: 11 pandas::DataFrame df; df.add_column_nullable("X", {true, pandas::NA_BOOL, false}); pandas_tests::check(df["X"].get_value_bool(0) == true, "case_3.idx0_true", local_fail); pandas_tests::check(df["X"].get_value_bool(1) == false, "case_3.idx1_NA_false", local_fail); pandas_tests::check(df["X"].get_value_bool(2) == false, "case_3.idx2_false", local_fail); } void bool_nullable_826495_case_4_is_na_at_mask_aware(int& local_fail) { pandas::DataFrame df; df.add_column_nullable("X", {true, pandas::NA_BOOL, false}); pandas_tests::check(df["X"].is_na_at(0) == false, "case_4.idx0_not_na", local_fail); pandas_tests::check(df["X"].is_na_at(1) == true, "case_4.idx1_is_na", local_fail); pandas_tests::check(df["X"].is_na_at(2) == false, "case_4.idx2_not_na", local_fail); } void bool_nullable_826495_case_5_fillna_preserves_dtype(int& local_fail) { pandas::DataFrame df; df.add_column_nullable("X", {true, pandas::NA_BOOL, false}); pandas_tests::check(df["X"].dtype_name() == "boolean", "case_5.pre_dtype", local_fail); auto df_filled = df.fillna(1.0); pandas_tests::check(df_filled["X"].dtype_name() == "boolean", .. _example-series-globalunlock-285: .. dropdown:: GlobalUnlock (pd_test_2_all.cpp:10220) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10210 :emphasize-lines: 11 return ""; } char* pszText = static_cast(GlobalLock(hData)); if (pszText == nullptr) { CloseClipboard(); return ""; } std::string text(pszText); GlobalUnlock(hData); CloseClipboard(); return text; } #else std::string get_clipboard_text() { // Non-Windows: just return empty (can't easily read clipboard) return ""; } #endif .. _example-series-abs-286: .. dropdown:: abs (pd_test_1_all.cpp:283) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 273 :emphasize-lines: 11 std::optional(true) }); auto s = arr.sum(); if (!s.has_value() || s.value() != 3) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: sum"); } auto m = arr.mean(); if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl; 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 ======================= "; .. _example-series-all_values_whole_number-287: .. dropdown:: all_values_whole_number (pd_test_5_all.cpp:30090) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 30080 :emphasize-lines: 11 !src_map_ov.empty() ? src_map_ov : src_ser_dt; bool is_int_like = (src_effective.find("int") != std::string::npos || src_effective.find("uint") != std::string::npos); bool comb_has_col = combined.has_column(flat); bool comb_hasnans = false, comb_allwhole = false; std::string comb_dt = ""; if (comb_has_col) { const pandas::NDFrameBase& c = combined[flat]; comb_hasnans = c.hasnans(); comb_allwhole = c.all_values_whole_number(); comb_dt = c.dtype_name(); } bool would_apply = is_int_like && comb_has_col && !comb_hasnans && comb_allwhole; std::cout << tag << " flat=" << flat << " src_effective=" << (src_effective.empty() ? "" : src_effective) << " is_int_like=" << is_int_like << " comb_dt=" << comb_dt << " comb_hasnans=" << comb_hasnans << " comb_allwhole=" << comb_allwhole .. _example-series-argmax-288: .. dropdown:: argmax (pd_test_1_all.cpp:1323) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1313 :emphasize-lines: 11 } // argmin auto min_idx = arr.argmin(); if (!min_idx.has_value() || min_idx.value() != 2) { std::cout << " [FAIL] : argmin should be 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmin"); } // argmax auto max_idx = arr.argmax(); if (!max_idx.has_value() || max_idx.value() != 3) { std::cout << " [FAIL] : argmax should be 3 (2023-12-31)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmax"); } std::cout << " -> tests passed" << std::endl; } void pd_test_datetime_array_unique() { std::cout << "========= DatetimeArray: unique/factorize ======================= "; .. _example-series-argmin-289: .. dropdown:: argmin (pd_test_1_all.cpp:1316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1306 :emphasize-lines: 11 if (indices.getElementAt({0}) != 2) { std::cout << " [FAIL] : argsort: first should be index 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argsort first"); } if (indices.getElementAt({3}) != 1) { std::cout << " [FAIL] : argsort: last should be index 1 (NaT)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: NaT position"); } // argmin auto min_idx = arr.argmin(); if (!min_idx.has_value() || min_idx.value() != 2) { std::cout << " [FAIL] : argmin should be 2 (2023-01-01)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmin"); } // argmax auto max_idx = arr.argmax(); if (!max_idx.has_value() || max_idx.value() != 3) { std::cout << " [FAIL] : argmax should be 3 (2023-12-31)" << std::endl; throw std::runtime_error("pd_test_datetime_array_sorting failed: argmax"); .. _example-series-autocorr-290: .. dropdown:: autocorr (pd_test_3_all.cpp:11904) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11894 :emphasize-lines: 11 auto result2 = s.asof("2020-01-05"); if (!result2.has_value() || std::abs(*result2 - 4.0) > 0.001) { std::cout << " [FAIL] : asof after all dates incorrect" << std::endl; throw std::runtime_error("pd_test_series_asof failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Test 3: autocorr() // ============================================================================ void pd_test_series_autocorr() { std::cout << "========= Series.autocorr() ========================"; // Create a simple series with known autocorrelation std::vector vals = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; pandas::Series s(vals, "test"); auto result = s.autocorr(1); if (!result.has_value()) { .. _example-series-axes-291: .. dropdown:: axes (pd_test_1_all.cpp:16602) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16592 :emphasize-lines: 11 // ===================================================================== // Axes Tests // ===================================================================== void pd_test_ndframe_axes() { std::cout << "========= axes =================================================" << std::endl; pandas::Series s({1.0, 2.0, 3.0}); auto axes = s.axes(); bool passed = axes.size() == 1; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_axes() : axes count" << std::endl; throw std::runtime_error("pd_test_ndframe_axes failed: axes count"); } passed = axes[0]->size() == 3; if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_axes() : axis size" << std::endl; .. _example-series-between-292: .. dropdown:: between (pd_test_1_all.cpp:19258) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19248 :emphasize-lines: 11 throw std::runtime_error("pd_test_series_comparison failed: greater than"); } auto eq2 = s == 2; passed = eq2[1] == true && eq2[0] == false; if (!passed) { std::cout << " [FAIL] : in pd_test_series_comparison() : equals failed" << std::endl; throw std::runtime_error("pd_test_series_comparison failed: equals"); } auto between_result = s.between(2, 4); passed = between_result[0] == false && between_result[1] == true && between_result[3] == true; if (!passed) { std::cout << " [FAIL] : in pd_test_series_comparison() : between failed" << std::endl; throw std::runtime_error("pd_test_series_comparison failed: between"); } std::cout << " -> tests passed" << std::endl; } void pd_test_series_transformations() { .. _example-series-canonical_dtype_name-293: .. dropdown:: canonical_dtype_name (pd_test_5_all.cpp:86367) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 86357 :emphasize-lines: 11 void case_1_series_complex_dtype_name(int& local_fail) { std::cout << "-- case_1_series_complex_dtype_name\n"; pandas::Series s({cdouble(1.0, 2.0), cdouble(3.0, 4.0)}); const std::string dt = s.dtype_name(); pandas_tests::check(dt == "complex128", "case_1_series_complex_dtype_name.dtype_is_complex128_got_" + dt, local_fail); } void case_2_series_canonical_dtype_name(int& local_fail) { std::cout << "-- case_2_series_canonical_dtype_name\n"; const std::string canon = pandas::Series::canonical_dtype_name(); pandas_tests::check(canon == "complex128", "case_2_canonical_dtype_name_is_complex128_got_" + canon, local_fail); } void case_3_empty_series_complex_dtype(int& local_fail) { std::cout << "-- case_3_empty_series_complex_dtype\n"; pandas::Series s(std::vector{}); const std::string dt = s.dtype_name(); pandas_tests::check(dt == "complex128", "case_3_empty_series_complex_dtype.dtype_is_complex128_got_" + dt, local_fail); .. _example-series-case_when-294: .. dropdown:: case_when (pd_test_3_all.cpp:9129) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9119 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Category 35: Plan 08 - Series case_when and rdivmod // ============================================================================ void pd_test_3_all_series_case_when() { std::cout << "========= Series.case_when() ======================"; std::vector vals = {1.0, 2.0, 3.0, 4.0, 5.0}; pandas::Series s(vals, "test"); // Create conditions numpy::NDArray cond1({5}); numpy::NDArray cond2({5}); for (size_t i = 0; i < 5; ++i) { cond1.setElementAt({i}, numpy::bool_(vals[i] < 2.0)); // val < 2 cond2.setElementAt({i}, numpy::bool_(vals[i] > 4.0)); // val > 4 .. _example-series-cat-295: .. dropdown:: cat (pd_test_3_all.cpp:16259) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16249 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_fillna_params() { std::cout << "========= CategoricalArray fillna params ============="; // Create CategoricalArray using vector constructor with optional values std::vector> values = {"a", "b", std::nullopt, "a"}; pandas::CategoricalArray cat(values); // Test fillna with method and limit parameters (should compile and work) auto result = cat.fillna("b", "", std::nullopt, true); bool passed = (result.size() == 4); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_fillna_params() : fillna failed" << std::endl; throw std::runtime_error("pd_test_categorical_fillna_params failed"); } .. _example-series-cat_ordered-296: .. dropdown:: cat_ordered (pd_test_2_all.cpp:20373) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20363 :emphasize-lines: 11 std::vector svals = {"a", "b", "a", "c"}; auto cs = std::make_unique>(svals, "cat"); cs->set_dtype_override("category"); cs->set_cat_categories({"a", "b", "c"}); cs->set_cat_ordered(true); df.insert(0, "cat", std::move(cs), true); auto s = df.get_column_as_string_series("cat"); check(s.dtype_name() == "category", "cat dtype"); check(s.has_cat_categories(), "cat has_categories"); check(s.cat_ordered() == true, "cat ordered"); auto cats = s.get_cat_categories(); check(cats.size() == 3, "cat categories size"); std::set cat_set(cats.begin(), cats.end()); check(cat_set.count("a") && cat_set.count("b") && cat_set.count("c"), "cat categories content"); } void pd_test_getitem_dispatch_index_propagation() { std::cout << "pd_test_getitem_dispatch_index_propagation" << std::endl; // Test DatetimeIndex freq propagation .. _example-series-ceil-297: .. dropdown:: ceil (pd_test_1_all.cpp:4949) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4939 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_series_round failed: round failed"); } auto f = a.floor(); passed = std::abs(f[0] - 1.0) < 0.001 && std::abs(f[2] - 3.0) < 0.001 && std::abs(f[3] - (-2.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : floor failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: floor failed"); } auto c = a.ceil(); passed = std::abs(c[0] - 2.0) < 0.001 && std::abs(c[2] - 4.0) < 0.001 && std::abs(c[3] - (-1.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : ceil failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: ceil failed"); } // Round with decimals pandas::Series b({1.234, 2.567, 3.891}); auto r2 = b.round(2); passed = std::abs(r2[0] - 1.23) < 0.001 && std::abs(r2[1] - 2.57) < 0.001; .. _example-series-clear_cache-298: .. 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-series-clip-299: .. dropdown:: clip (pd_test_1_all.cpp:5099) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5089 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_dataframe_abs_clip failed: abs failed"); } val = a["A"].get_value_str(2); passed = std::abs(std::stod(val) - 3.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_abs_clip() : abs for -3 failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_abs_clip failed: abs for -3 failed"); } auto c = df.clip(-2.0, 2.0); val = c["A"].get_value_str(2); passed = std::abs(std::stod(val) - (-2.0)) < 0.001; // -3 clipped to -2 if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_abs_clip() : clip lower failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_abs_clip failed: clip lower failed"); } val = c["A"].get_value_str(3); passed = std::abs(std::stod(val) - 2.0) < 0.001; // 4 clipped to 2 if (!passed) { .. _example-series-clone-300: .. 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-series-corr-301: .. dropdown:: corr (pd_test_1_all.cpp:4655) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4645 :emphasize-lines: 11 } void pd_test_aggregation_dataframe_corr() { std::cout << "========= DataFrame corr ========================"; std::map> data; data["A"] = {1.0, 2.0, 3.0, 4.0, 5.0}; data["B"] = {2.0, 4.0, 6.0, 8.0, 10.0}; // Perfect correlation pandas::DataFrame df(data); 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); .. _example-series-cov-302: .. dropdown:: cov (pd_test_1_all.cpp:4690) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4680 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_dataframe_cov() { std::cout << "========= DataFrame cov ========================="; std::map> data; data["A"] = {1.0, 2.0, 3.0}; pandas::DataFrame df(data); auto cov_df = df.cov(); // Check dimensions bool passed = cov_df.nrows() == 1 && cov_df.ncols() == 1; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_dataframe_cov() : cov should be 1x1" << std::endl; throw std::runtime_error("pd_test_aggregation_dataframe_cov failed: cov should be 1x1"); } // Var(A) = 1.0 with ddof=1 std::string aa = cov_df["A"].get_value_str(0); .. _example-series-datetime_array-303: .. dropdown:: datetime_array (pd_test_extension_array.cpp:255) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 245 :emphasize-lines: 11 } pandas::Series s(dt_vec); s.set_datetime_array(ea); // to_frame() must propagate the EA into the resulting DataFrame's column. pandas::DataFrame df = s.to_frame(std::optional{"d"}); check(df.has_column("d"), "to_frame: column 'd' present"); auto& col = df["d"]; auto* col_dt = dynamic_cast*>(&col); check(col_dt != nullptr, "to_frame: column dynamic_casts to Series"); if (col_dt) { const auto& da_opt = col_dt->datetime_array(); check(da_opt.has_value() && *da_opt, "to_frame: column has datetime_array EA populated"); if (da_opt.has_value() && *da_opt) { // Pointer equality: the SAME shared_ptr should propagate (no clone). check((*da_opt).get() == ea.get(), "to_frame: datetime_array shared_ptr identity preserved"); } } return g_errors - errors_before; } .. _example-series-datetime_ns_to_string-304: .. dropdown:: datetime_ns_to_string (pd_test_3_all.cpp:27760) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27750 :emphasize-lines: 11 } if (fail == 0) std::cout << " OK" << std::endl; } void pd_test_astype_datetime_to_string() { std::cout << " -- pd_test_astype_datetime_to_string --" << std::endl; int fail = 0; double ns_val = 1577836800000000000.0; std::string formatted = pandas::Series::datetime_ns_to_string(ns_val); if (formatted != "2020-01-01") { std::cout << " FAIL: expected '2020-01-01', got '" << formatted << "'" << std::endl; fail++; } double ns_val2 = 1577836800000000000.0 + 12*3600000000000.0 + 30*60000000000.0 + 45*1000000000.0; std::string formatted2 = pandas::Series::datetime_ns_to_string(ns_val2); if (formatted2 != "2020-01-01 12:30:45") { std::cout << " FAIL: expected '2020-01-01 12:30:45', got '" << formatted2 << "'" << std::endl; fail++; .. _example-series-dt-305: .. dropdown:: dt (pd_test_3_all.cpp:18239) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18229 :emphasize-lines: 11 if (offset.freqstr() != "D") { std::cout << " [FAIL] : Day freqstr() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: freqstr() failed"); } if (offset.name() != "Day") { std::cout << " [FAIL] : Day name() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: name() failed"); } // Test apply numpy::datetime64 dt("2020-01-15"); auto result = offset.apply(dt); std::tm tm = result.toTm(); if (tm.tm_mday != 20) { std::cout << " [FAIL] : Day apply() failed, got day " << tm.tm_mday << std::endl; throw std::runtime_error("pd_test_day_offset: apply() failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-dt-306: .. dropdown:: dt (pd_test_3_all.cpp:18239) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18229 :emphasize-lines: 11 if (offset.freqstr() != "D") { std::cout << " [FAIL] : Day freqstr() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: freqstr() failed"); } if (offset.name() != "Day") { std::cout << " [FAIL] : Day name() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: name() failed"); } // Test apply numpy::datetime64 dt("2020-01-15"); auto result = offset.apply(dt); std::tm tm = result.toTm(); if (tm.tm_mday != 20) { std::cout << " [FAIL] : Day apply() failed, got day " << tm.tm_mday << std::endl; throw std::runtime_error("pd_test_day_offset: apply() failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-dtype_name-307: .. 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-series-dtype_name_full-308: .. dropdown:: dtype_name_full (pd_test_5_all.cpp:26384) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26374 :emphasize-lines: 11 pandas::DataFrame df; df.add_column("group", {"A", "A", "B"}); df.add_column("flag", {true, false, true}); // Promote the column's dtype override to the PandasPython-origin sub-type. df.set_column_dtype("flag", "object:bool"); // Pre-check: dtype_name strips the colon, dtype_name_full keeps it. pandas_tests::check(df["flag"].dtype_name() == "object", "b21.pre: df[flag].dtype_name()==object (got '" + df["flag"].dtype_name() + "')", local_fail); pandas_tests::check(df["flag"].dtype_name_full() == "object:bool", "b21.pre: df[flag].dtype_name_full()==object:bool (got '" + df["flag"].dtype_name_full() + "')", local_fail); auto gg = df.groupby("group").get_group("A"); // FIX VERIFIED: Option 2 via iloc_rows + take_indices preserves the // dtype_override ("object:bool"); dtype_name() strips the colon and // returns "object". std::string gg_dt = gg["flag"].dtype_name(); std::string gg_dt_full = gg["flag"].dtype_name_full(); .. _example-series-dtype_override-309: .. dropdown:: dtype_override (pd_test_5_all.cpp:26391) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26381 :emphasize-lines: 11 pandas_tests::check(df["flag"].dtype_name() == "object", "b21.pre: df[flag].dtype_name()==object (got '" + df["flag"].dtype_name() + "')", local_fail); pandas_tests::check(df["flag"].dtype_name_full() == "object:bool", "b21.pre: df[flag].dtype_name_full()==object:bool (got '" + df["flag"].dtype_name_full() + "')", local_fail); auto gg = df.groupby("group").get_group("A"); // FIX VERIFIED: Option 2 via iloc_rows + take_indices preserves the // dtype_override ("object:bool"); dtype_name() strips the colon and // returns "object". std::string gg_dt = gg["flag"].dtype_name(); std::string gg_dt_full = gg["flag"].dtype_name_full(); pandas_tests::check(gg_dt == "object", "b21.gg[flag].dtype_name()==object (FIX VERIFIED; got '" + gg_dt + "')", local_fail); pandas_tests::check(gg_dt_full == "object:bool", "b21.gg[flag].dtype_name_full()==object:bool (FIX VERIFIED; got '" + gg_dt_full + "')", local_fail); } .. _example-series-empty-310: .. 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-series-factorize-311: .. dropdown:: factorize (pd_test_1_all.cpp:1353) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1343 :emphasize-lines: 11 // unique auto uniq = arr.unique(); // Should have: NaT, 2023-01-01, 2023-06-15 (3 unique values) if (uniq.size() != 3) { std::cout << " [FAIL] : unique size should be 3, got " << uniq.size() << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: size"); } // factorize auto [codes, uniques] = arr.factorize(); // Codes for NaT should be -1 if (codes.getElementAt({3}) != -1) { std::cout << " [FAIL] : factorize: NaT code should be -1" << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: NaT code"); } // Same values should have same codes if (codes.getElementAt({0}) != codes.getElementAt({2})) { std::cout << " [FAIL] : factorize: 2023-01-01 values should have same code" << std::endl; throw std::runtime_error("pd_test_datetime_array_unique failed: same code"); } .. _example-series-file-312: .. dropdown:: file (pd_test_2_all.cpp:3463) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3453 :emphasize-lines: 11 {"C", {100, 200, 300, 400, 500}} }; pandas::DataFrame df(data); // Export to Excel std::string filepath = "temp/pd_test_excel_basic.xlsx"; df.to_excel(filepath); // Verify file was created std::ifstream file(filepath, std::ios::binary); if (!file.good()) { std::cout << " [FAIL] : in pd_test_excel_basic() : File was not created" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: file not created"); } // Check file size is reasonable (valid XLSX should be > 1KB) file.seekg(0, std::ios::end); auto size = file.tellg(); if (size < 1000) { std::cout << " [FAIL] : in pd_test_excel_basic() : File size too small: " << size << std::endl; .. _example-series-file-313: .. dropdown:: file (pd_test_2_all.cpp:3463) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3453 :emphasize-lines: 11 {"C", {100, 200, 300, 400, 500}} }; pandas::DataFrame df(data); // Export to Excel std::string filepath = "temp/pd_test_excel_basic.xlsx"; df.to_excel(filepath); // Verify file was created std::ifstream file(filepath, std::ios::binary); if (!file.good()) { std::cout << " [FAIL] : in pd_test_excel_basic() : File was not created" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: file not created"); } // Check file size is reasonable (valid XLSX should be > 1KB) file.seekg(0, std::ios::end); auto size = file.tellg(); if (size < 1000) { std::cout << " [FAIL] : in pd_test_excel_basic() : File size too small: " << size << std::endl; .. _example-series-file-314: .. dropdown:: file (pd_test_2_all.cpp:3463) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3453 :emphasize-lines: 11 {"C", {100, 200, 300, 400, 500}} }; pandas::DataFrame df(data); // Export to Excel std::string filepath = "temp/pd_test_excel_basic.xlsx"; df.to_excel(filepath); // Verify file was created std::ifstream file(filepath, std::ios::binary); if (!file.good()) { std::cout << " [FAIL] : in pd_test_excel_basic() : File was not created" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: file not created"); } // Check file size is reasonable (valid XLSX should be > 1KB) file.seekg(0, std::ios::end); auto size = file.tellg(); if (size < 1000) { std::cout << " [FAIL] : in pd_test_excel_basic() : File size too small: " << size << std::endl; .. _example-series-filter-315: .. dropdown:: filter (pd_test_3_all.cpp:2805) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2795 :emphasize-lines: 11 threw = true; } if (!threw) { throw std::runtime_error("bool_() should throw for multi-element DataFrame"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_df_filter() { std::cout << "========= DataFrame.filter() ============================="; std::map> data = { {"col_a", {1.0, 2.0, 3.0}}, {"col_b", {4.0, 5.0, 6.0}}, {"other", {7.0, 8.0, 9.0}} }; pandas::DataFrame df(data); // Test filter by items pandas::DataFrame filtered_items = df.filter({"col_a", "col_b"}); .. _example-series-filter_by_bool_mask-316: .. dropdown:: filter_by_bool_mask (pd_test_5_all.cpp:92595) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 92585 :emphasize-lines: 11 std::vector> data = {{1.0, 2.0}}; std::vector cols = {"x", "y"}; return pandas::DataFrame::from_records(data, cols); } void case_1_filter_mask_f64() { std::cout << "-- case_1_filter_mask_f64()\n"; int local_fail = 0; auto s = make_f64_series_5(); auto mask = make_mask_TFTFT(); auto result = s.filter_by_bool_mask(mask); pandas_tests::check(result.size() == 3, "f_test_25_filter_mask_f64_393211.size_eq_3", local_fail); pandas_tests::check(result.iat(0) == 1.0, "f_test_25_filter_mask_f64_393211.iat0_eq_1", local_fail); pandas_tests::check(result.iat(1) == 3.0, "f_test_25_filter_mask_f64_393211.iat1_eq_3", local_fail); pandas_tests::check(result.iat(2) == 5.0, "f_test_25_filter_mask_f64_393211.iat2_eq_5", local_fail); } .. _example-series-filter_by_bool_series-317: .. dropdown:: filter_by_bool_series (pd_test_5_all.cpp:92669) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 92659 :emphasize-lines: 11 pandas_tests::check(threw, "f_test_25_filter_mask_length_mismatch_throws_604411.value_error", local_fail); } void case_6_filter_bool_series_happy() { std::cout << "-- case_6_filter_bool_series_happy()\n"; int local_fail = 0; auto s = make_f64_series_5(); auto cond = make_bool_series_TFTFT(); auto result = s.filter_by_bool_series(cond); pandas_tests::check(result.size() == 3, "f_test_25_filter_bool_series_happy_490201.size_eq_3", local_fail); pandas_tests::check(result.iat(0) == 1.0, "f_test_25_filter_bool_series_happy_490201.iat0_eq_1", local_fail); pandas_tests::check(result.iat(1) == 3.0, "f_test_25_filter_bool_series_happy_490201.iat1_eq_3", local_fail); } void case_7_filter_bool_series_mismatch_throws() { std::cout << "-- case_7_filter_bool_series_mismatch_throws()\n"; .. _example-series-floor-318: .. dropdown:: floor (pd_test_1_all.cpp:4942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4932 :emphasize-lines: 11 pandas::Series a({1.4, 2.5, 3.6, -1.4, -2.5}); auto r = a.round(); bool passed = std::abs(r[0] - 1.0) < 0.001 && std::abs(r[2] - 4.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : round failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: round failed"); } auto f = a.floor(); passed = std::abs(f[0] - 1.0) < 0.001 && std::abs(f[2] - 3.0) < 0.001 && std::abs(f[3] - (-2.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : floor failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: floor failed"); } auto c = a.ceil(); passed = std::abs(c[0] - 2.0) < 0.001 && std::abs(c[2] - 4.0) < 0.001 && std::abs(c[3] - (-1.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : ceil failed" << std::endl; .. _example-series-func-319: .. dropdown:: func (pd_test_3_all.cpp:13837) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13827 :emphasize-lines: 11 // ============================================================================ // Read Stubs Tests (verify they throw correctly) // ============================================================================ void pd_test_top_level_read_stubs() { std::cout << "========= read_* stubs ================================"; // Test that read functions throw as expected (they are stubs) auto test_throws = [](const std::string& name, auto func) { try { func(); std::cout << " [FAIL] : " << name << " should throw" << std::endl; return false; } catch (const std::exception&) { return true; } }; bool all_passed = true; all_passed &= test_throws("read_clipboard", []() { pandas::read_clipboard(); }); all_passed &= test_throws("read_excel", []() { pandas::read_excel("test.xlsx"); }); .. _example-series-has_cached_values-320: .. 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-series-has_cat_categories-321: .. dropdown:: has_cat_categories (pd_test_2_all.cpp:20372) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20362 :emphasize-lines: 11 pandas::DataFrame df; std::vector svals = {"a", "b", "a", "c"}; auto cs = std::make_unique>(svals, "cat"); cs->set_dtype_override("category"); cs->set_cat_categories({"a", "b", "c"}); cs->set_cat_ordered(true); df.insert(0, "cat", std::move(cs), true); auto s = df.get_column_as_string_series("cat"); check(s.dtype_name() == "category", "cat dtype"); check(s.has_cat_categories(), "cat has_categories"); check(s.cat_ordered() == true, "cat ordered"); auto cats = s.get_cat_categories(); check(cats.size() == 3, "cat categories size"); std::set cat_set(cats.begin(), cats.end()); check(cat_set.count("a") && cat_set.count("b") && cat_set.count("c"), "cat categories content"); } void pd_test_getitem_dispatch_index_propagation() { std::cout << "pd_test_getitem_dispatch_index_propagation" << std::endl; .. _example-series-has_mask-322: .. dropdown:: has_mask (pd_test_3_all.cpp:27708) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27698 :emphasize-lines: 11 auto* bool_s = dynamic_cast*>(result.get()); if (!bool_s) { std::cout << " FAIL: expected Series" << std::endl; fail++; } else { if (bool_s->dtype_name() != "boolean") { std::cout << " FAIL: dtype should be boolean, got " << bool_s->dtype_name() << std::endl; fail++; } if (!bool_s->has_mask()) { std::cout << " FAIL: should have mask for NA" << std::endl; fail++; } else { if (!bool_s->mask_at(2)) { std::cout << " FAIL: position 2 should be masked (NA)" << std::endl; fail++; } } } .. _example-series-has_multiindex-323: .. dropdown:: has_multiindex (pd_test_1_all.cpp:27019) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27009 :emphasize-lines: 11 std::map> data = { {"A", {"a", "a", "b", "b"}}, {"B", {"x", "x", "y", "y"}} }; pandas::DataFrame df(data); auto result = df.value_counts(); auto& counts = std::get>(result); if (!counts.has_multiindex()) { std::cout << " [FAIL] : expected MultiIndex" << std::endl; throw std::runtime_error("pd_test_value_counts_multiindex_levels failed: no multiindex"); } const auto& midx = counts.multiindex(); // Should have 2 levels if (midx.nlevels() != 2) { std::cout << " [FAIL] : expected 2 levels, got " << midx.nlevels() << std::endl; throw std::runtime_error("pd_test_value_counts_multiindex_levels failed: wrong nlevels"); .. _example-series-hasnans-324: .. 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-series-index-325: .. 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-series-index_mut-326: .. dropdown:: index_mut (pd_test_5_all.cpp:40329) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 40319 :emphasize-lines: 11 pandas_tests::check(r.string_na_sentinel_disabled() == true, "case4.string_na_sentinel_disabled_propagates", local_fail); std::cout << " source flag=" << s.string_na_sentinel_disabled() << " result flag=" << r.string_na_sentinel_disabled() << "\n"; } void case_5_index_name_propagates(int& local_fail) { std::cout << "----- case_5_index_name_propagates -----\n"; auto s = make_series_3({10, 20, 30}); s.index_mut().set_name(std::optional("idx_name")); auto r = s.reindex({"0", "1", "2"}); auto src_name = s.index().name(); auto res_name = r.index().name(); pandas_tests::check(res_name.has_value(), "case5.index_name_present_after_reindex", local_fail); pandas_tests::check(res_name.has_value() && *res_name == "idx_name", "case5.index_name_value_is_idx_name", local_fail); .. _example-series-info-327: .. dropdown:: info (pd_test_1_all.cpp:7122) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7112 :emphasize-lines: 11 } if (!empty_params_error) { std::cout << " [FAIL] : select_dtypes empty params should throw" << std::endl; throw std::runtime_error("pd_test_dataframe_select_dtypes failed: empty params error"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== // Test: info() method // ===================================================================== void pd_test_dataframe_info() { std::cout << "========= info ========================"; // Test basic info() with stringstream std::map> data = { {"A", {1, 2, 3, 4, 5}}, {"B", {10, 20, 30, 40, 50}}, {"C", {100, 200, 300, 400, 500}} }; .. _example-series-item-328: .. dropdown:: item (pd_test_3_all.cpp:3712) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3702 :emphasize-lines: 11 // Test is_interval (always false for base Index) if (int_idx.is_interval()) { throw std::runtime_error("base Index should not be interval"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_index_item() { std::cout << "========= Index.item() ============================="; pandas::Index idx1({42}); numpy::int64 val = idx1.item(); if (val != 42) { throw std::runtime_error("item() should return 42"); } // Test error for size != 1 pandas::Index idx2({1, 2, 3}); .. _example-series-memcpy-329: .. dropdown:: memcpy (pd_test_5_all.cpp:33658) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 33648 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // --- f_test_formatter_to_chars_9.cpp --- namespace f_test_formatter_to_chars_9_ns { static double bits_to_double(std::uint64_t bits) { double v; std::memcpy(&v, &bits, sizeof(v)); return v; } static int format_current(char* buf, std::size_t bufsz, int digits, double v) { if (digits < 0 || !std::isfinite(v)) { return std::snprintf(buf, bufsz, "%.*f", digits, v); } long double scale = 1.0L; for (int k = 0; k < digits; ++k) scale *= 10.0L; long double scaled = static_cast(v) * scale; .. _example-series-memory_usage-330: .. dropdown:: memory_usage (pd_test_1_all.cpp:27063) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27053 :emphasize-lines: 11 } std::cout << "====================================== [OK] pd_test_value_counts test suite ========================== " << std::endl; return 0; } } // namespace dataframe_tests // ------------------- pd_test_value_counts.cpp (end) ----------------------------- // ------------------- pd_test_memory_usage.cpp (start) ----------------------------- // Tests for DataFrame.memory_usage() - pandas-compatible memory usage reporting namespace dataframe_tests { namespace dataframe_tests_memory_usage { void pd_test_memory_usage_basic() { std::cout << "========= basic memory_usage ======================="; // Create a simple DataFrame with multiple columns std::map> data; data["A"] = {1.0, 2.0, 3.0, 4.0, 5.0}; .. _example-series-mixed_tz_array-331: .. dropdown:: mixed_tz_array (pd_test_extension_array.cpp:287) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 277 :emphasize-lines: 11 dt_vec.push_back(numpy::datetime64(t->value(), numpy::DateTimeUnit::Nanosecond)); } pandas::Series s(dt_vec); s.set_mixed_tz_array(mta); pandas::DataFrame df = s.to_frame(std::optional{"m"}); check(df.has_column("m"), "to_frame mixed-tz: column 'm' present"); auto& col = df["m"]; auto* col_dt = dynamic_cast*>(&col); check(col_dt != nullptr, "to_frame mixed-tz: column is Series"); if (col_dt) { const auto& mta_opt = col_dt->mixed_tz_array(); check(mta_opt.has_value() && *mta_opt, "to_frame mixed-tz: column has mixed_tz_array EA populated"); if (mta_opt.has_value() && *mta_opt) { check((*mta_opt).get() == mta.get(), "to_frame mixed-tz: shared_ptr identity preserved"); } } return g_errors - errors_before; } .. _example-series-name-332: .. 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-series-nbytes-333: .. 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-series-ndim-334: .. 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-series-ravel-335: .. dropdown:: ravel (pd_test_3_all.cpp:2147) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2137 :emphasize-lines: 11 throw std::runtime_error("memory_usage shallow too small"); } if (deep < shallow) { throw std::runtime_error("memory_usage deep should be >= shallow"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_categorical_ravel_view() { std::cout << "========= CategoricalArray.ravel()/view() ============="; std::vector> values = {"a", "b", "c"}; pandas::CategoricalArray arr(values); auto raveled = arr.ravel(); if (raveled.size() != 3 || !raveled.equals(arr)) { throw std::runtime_error("ravel failed"); } auto viewed = arr.view(); .. _example-series-repeat-336: .. dropdown:: repeat (pd_test_3_all.cpp:2166) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2156 :emphasize-lines: 11 auto viewed = arr.view(); if (viewed.size() != 3 || !viewed.equals(arr)) { throw std::runtime_error("view failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_categorical_repeat() { std::cout << "========= CategoricalArray.repeat() ==================="; std::vector> values = {"a", "b"}; pandas::CategoricalArray arr(values); auto result = arr.repeat(3); if (result.size() != 6 || *result[0] != "a" || *result[2] != "a" || *result[3] != "b" || *result[5] != "b") { throw std::runtime_error("repeat scalar failed"); } .. _example-series-repr-337: .. 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-series-resolve_multiindex_level-338: .. dropdown:: resolve_multiindex_level (pd_test_3_all.cpp:23441) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23431 :emphasize-lines: 11 if (gb.group_keys_order().size() != 2) throw std::runtime_error("expected 2 groups"); auto sums = gb.sum(); if (sums[0] != 40.0 || sums[1] != 20.0) throw std::runtime_error("sum mismatch"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_resolve_level() { std::cout << "========= resolve_multiindex_level() =================="; pandas::Series s({1.0, 2.0}); std::vector> level_values = {{"a", "b"}, {"x", "y"}}; std::vector> level_names = {"first", "second"}; auto mi = pandas::MultiIndex::from_arrays(level_values, level_names); s.set_multiindex(mi); if (s.resolve_multiindex_level("first") != 0 || s.resolve_multiindex_level("second") != 1) throw std::runtime_error("string level resolution failed"); if (s.resolve_multiindex_level(0) != 0 || s.resolve_multiindex_level(-1) != 1) .. _example-series-resolve_multiindex_level-339: .. dropdown:: resolve_multiindex_level (pd_test_3_all.cpp:23441) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23431 :emphasize-lines: 11 if (gb.group_keys_order().size() != 2) throw std::runtime_error("expected 2 groups"); auto sums = gb.sum(); if (sums[0] != 40.0 || sums[1] != 20.0) throw std::runtime_error("sum mismatch"); std::cout << " -> tests passed" << std::endl; } void pd_test_groupby_resolve_level() { std::cout << "========= resolve_multiindex_level() =================="; pandas::Series s({1.0, 2.0}); std::vector> level_values = {{"a", "b"}, {"x", "y"}}; std::vector> level_names = {"first", "second"}; auto mi = pandas::MultiIndex::from_arrays(level_values, level_names); s.set_multiindex(mi); if (s.resolve_multiindex_level("first") != 0 || s.resolve_multiindex_level("second") != 1) throw std::runtime_error("string level resolution failed"); if (s.resolve_multiindex_level(0) != 0 || s.resolve_multiindex_level(-1) != 1) .. _example-series-result-340: .. dropdown:: result (pd_test_1_all.cpp:15406) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15396 :emphasize-lines: 11 data.setElementAt({0}, numpy::datetime64(100LL, numpy::DateTimeUnit::Nanosecond)); data.setElementAt({1}, numpy::datetime64(200LL, numpy::DateTimeUnit::Nanosecond)); numpy::NDArray mask(std::vector{2}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeIndexBase idx(arr, "original"); // Create join result (int64 values) numpy::NDArray join_result(std::vector{3}); join_result.setElementAt({0}, numpy::int64(500LL)); join_result.setElementAt({1}, numpy::int64(600LL)); join_result.setElementAt({2}, numpy::int64(700LL)); auto new_idx = idx._from_join_target(join_result); bool passed = (new_idx.size() == 3 && new_idx.name().has_value() && *new_idx.name() == "original"); if (!passed) { .. _example-series-result-341: .. dropdown:: result (pd_test_1_all.cpp:15406) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15396 :emphasize-lines: 11 data.setElementAt({0}, numpy::datetime64(100LL, numpy::DateTimeUnit::Nanosecond)); data.setElementAt({1}, numpy::datetime64(200LL, numpy::DateTimeUnit::Nanosecond)); numpy::NDArray mask(std::vector{2}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeIndexBase idx(arr, "original"); // Create join result (int64 values) numpy::NDArray join_result(std::vector{3}); join_result.setElementAt({0}, numpy::int64(500LL)); join_result.setElementAt({1}, numpy::int64(600LL)); join_result.setElementAt({2}, numpy::int64(700LL)); auto new_idx = idx._from_join_target(join_result); bool passed = (new_idx.size() == 3 && new_idx.name().has_value() && *new_idx.name() == "original"); if (!passed) { .. _example-series-round-342: .. dropdown:: round (pd_test_1_all.cpp:1688) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1678 :emphasize-lines: 11 void pd_test_floating_array_rounding() { std::cout << "========= FloatingArray: rounding ======================= "; pandas::FloatingArray arr({ std::optional(1.234), std::optional(2.567), std::nullopt }); auto rounded = arr.round(2); if (std::abs(rounded[0].value() - 1.23) > 0.001 || std::abs(rounded[1].value() - 2.57) > 0.001) { std::cout << " [FAIL] : in pd_test_floating_array_rounding() : round(2)" << std::endl; throw std::runtime_error("pd_test_floating_array_rounding failed: round(2)"); } if (!rounded.is_na(2)) { std::cout << " [FAIL] : in pd_test_floating_array_rounding() : round should preserve NA" << std::endl; throw std::runtime_error("pd_test_floating_array_rounding failed: NA preservation"); } .. _example-series-s-343: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-344: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-345: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-346: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-347: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-348: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-349: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-s-350: .. dropdown:: s (pd_test_1_all.cpp:4524) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4514 :emphasize-lines: 11 #include #include "../pandas/pd_dataframe.h" #include "../pandas/pd_series.h" namespace dataframe_tests { namespace dataframe_tests_aggregation { void pd_test_aggregation_series_sem() { std::cout << "========= Series sem ============================"; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto sem_val = s.sem(); // std(ddof=1) = sqrt(2.5), sem = sqrt(2.5)/sqrt(5) ≈ 0.707 bool passed = sem_val.has_value() && std::abs(*sem_val - 0.707) < 0.01; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_series_sem() : sem value incorrect" << std::endl; throw std::runtime_error("pd_test_aggregation_series_sem failed: sem value incorrect"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-set_cat_categories-351: .. dropdown:: set_cat_categories (pd_test_2_all.cpp:20366) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20356 :emphasize-lines: 11 check(sub.columns().get_value_str(0) == "col", "dup col0 name"); check(sub.columns().get_value_str(1) == "col", "dup col1 name"); } void pd_test_getitem_dispatch_category_metadata() { std::cout << "pd_test_getitem_dispatch_category_metadata" << std::endl; pandas::DataFrame df; std::vector svals = {"a", "b", "a", "c"}; auto cs = std::make_unique>(svals, "cat"); cs->set_dtype_override("category"); cs->set_cat_categories({"a", "b", "c"}); cs->set_cat_ordered(true); df.insert(0, "cat", std::move(cs), true); auto s = df.get_column_as_string_series("cat"); check(s.dtype_name() == "category", "cat dtype"); check(s.has_cat_categories(), "cat has_categories"); check(s.cat_ordered() == true, "cat ordered"); auto cats = s.get_cat_categories(); check(cats.size() == 3, "cat categories size"); std::set cat_set(cats.begin(), cats.end()); .. _example-series-set_cat_ordered-352: .. dropdown:: set_cat_ordered (pd_test_2_all.cpp:20367) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20357 :emphasize-lines: 11 check(sub.columns().get_value_str(1) == "col", "dup col1 name"); } void pd_test_getitem_dispatch_category_metadata() { std::cout << "pd_test_getitem_dispatch_category_metadata" << std::endl; pandas::DataFrame df; std::vector svals = {"a", "b", "a", "c"}; auto cs = std::make_unique>(svals, "cat"); cs->set_dtype_override("category"); cs->set_cat_categories({"a", "b", "c"}); cs->set_cat_ordered(true); df.insert(0, "cat", std::move(cs), true); auto s = df.get_column_as_string_series("cat"); check(s.dtype_name() == "category", "cat dtype"); check(s.has_cat_categories(), "cat has_categories"); check(s.cat_ordered() == true, "cat ordered"); auto cats = s.get_cat_categories(); check(cats.size() == 3, "cat categories size"); std::set cat_set(cats.begin(), cats.end()); check(cat_set.count("a") && cat_set.count("b") && cat_set.count("c"), "cat categories content"); .. _example-series-set_datetime_array-353: .. dropdown:: set_datetime_array (pd_test_extension_array.cpp:247) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 237 :emphasize-lines: 11 pandas::Timestamp(1577836800000000000LL), pandas::Timestamp(1609459200000000000LL), }; auto ea = std::make_shared( pandas::DatetimeArray::from_timestamps(ts, /*uniform_tz=*/"")); std::vector dt_vec; for (const auto& t : ts) { dt_vec.push_back(numpy::datetime64(t->value(), numpy::DateTimeUnit::Nanosecond)); } pandas::Series s(dt_vec); s.set_datetime_array(ea); // to_frame() must propagate the EA into the resulting DataFrame's column. pandas::DataFrame df = s.to_frame(std::optional{"d"}); check(df.has_column("d"), "to_frame: column 'd' present"); auto& col = df["d"]; auto* col_dt = dynamic_cast*>(&col); check(col_dt != nullptr, "to_frame: column dynamic_casts to Series"); if (col_dt) { const auto& da_opt = col_dt->datetime_array(); check(da_opt.has_value() && *da_opt, "to_frame: column has datetime_array EA populated"); .. _example-series-set_dtype_override-354: .. dropdown:: set_dtype_override (pd_test_2_all.cpp:20225) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20215 :emphasize-lines: 11 std::vector vals = {1.0, 2.0, 3.0}; df.insert(0, "A", std::make_unique>(vals, "A"), true); auto t = df.classify_column_access("A"); check(t == pandas::DataFrame::ColumnAccessType::NumericColumn, "float64 -> NumericColumn"); // int64 column pandas::DataFrame df2; std::vector ivals = {10, 20, 30}; auto iseries = std::make_unique>(ivals, "B"); iseries->set_dtype_override("int64"); df2.insert(0, "B", std::move(iseries), true); auto t2 = df2.classify_column_access("B"); check(t2 == pandas::DataFrame::ColumnAccessType::NumericColumn, "int64 -> NumericColumn"); } void pd_test_getitem_dispatch_classify_bool() { std::cout << "pd_test_getitem_dispatch_classify_bool" << std::endl; pandas::DataFrame df; std::vector bvals = {true, false, true}; df.insert(0, "flag", std::make_unique>(bvals, "flag"), true); .. _example-series-set_freq-355: .. dropdown:: set_freq (pd_test_1_all.cpp:8254) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8244 :emphasize-lines: 11 void pd_test_datetime_mixin_set_freq() { std::cout << "========= set_freq ===================================="; std::vector> values = { numpy::datetime64(0LL, numpy::DateTimeUnit::Nanosecond) }; pandas::DatetimeArray arr(values); pandas::DatetimeMixinIndex idx(arr); idx.set_freq("D"); auto f = idx.freq(); bool passed = (f.has_value() && *f == "D"); if (!passed) { std::cout << " [FAIL] : in pd_test_datetime_mixin_set_freq()" << std::endl; throw std::runtime_error("pd_test_datetime_mixin_set_freq failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-series-set_mask-356: .. dropdown:: set_mask (pd_test_3_all.cpp:25879) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25869 :emphasize-lines: 11 std::cout << " PASSED" << std::endl; } void pd_test_cat_constructor_helpers_mask() { std::cout << "========= cat_constructor_helpers mask ==================" << std::endl; ::pandas::Series s(std::vector{"x", "y", "z"}); ::numpy::NDArray<::numpy::bool_> mask(std::vector{3}); mask.setElementAt({0}, ::numpy::bool_(false)); mask.setElementAt({1}, ::numpy::bool_(true)); mask.setElementAt({2}, ::numpy::bool_(false)); s.set_mask(mask); auto result = ::pandas::series_to_optional_string_vector(s); if (result.size() != 3) throw std::runtime_error("Expected size 3"); if (!result[0].has_value()) throw std::runtime_error("Expected value at index 0"); if (result[1].has_value()) throw std::runtime_error("Expected nullopt at index 1 (masked)"); if (!result[2].has_value()) throw std::runtime_error("Expected value at index 2"); std::cout << " PASSED" << std::endl; } void pd_test_cat_constructor_helpers_empty() { std::cout << "========= cat_constructor_helpers empty =================" << std::endl; .. _example-series-set_mixed_tz_array-357: .. dropdown:: set_mixed_tz_array (pd_test_extension_array.cpp:280) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 270 :emphasize-lines: 11 pandas::Timestamp(1577836800000000000LL, "UTC"), pandas::Timestamp(1609459200000000000LL, "US/Eastern"), }; auto mta = std::make_shared( pandas::MixedTzDatetimeArray::from_timestamps(ts)); std::vector dt_vec; for (const auto& t : ts) { dt_vec.push_back(numpy::datetime64(t->value(), numpy::DateTimeUnit::Nanosecond)); } pandas::Series s(dt_vec); s.set_mixed_tz_array(mta); pandas::DataFrame df = s.to_frame(std::optional{"m"}); check(df.has_column("m"), "to_frame mixed-tz: column 'm' present"); auto& col = df["m"]; auto* col_dt = dynamic_cast*>(&col); check(col_dt != nullptr, "to_frame mixed-tz: column is Series"); if (col_dt) { const auto& mta_opt = col_dt->mixed_tz_array(); check(mta_opt.has_value() && *mta_opt, "to_frame mixed-tz: column has mixed_tz_array EA populated"); if (mta_opt.has_value() && *mta_opt) { .. _example-series-set_multiindex-358: .. dropdown:: set_multiindex (pd_test_2_all.cpp:20409) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20399 :emphasize-lines: 11 check(s.get_freq().value() == "D", "freq value D"); } // Test MultiIndex propagation pandas::DataFrame df2; std::vector vals2 = {10.0, 20.0}; df2.insert(0, "A", std::make_unique>(vals2, "A"), true); std::vector> arrays = {{"x", "y"}, {"1", "2"}}; std::vector> names = {std::string("first"), std::string("second")}; auto mi = pandas::MultiIndex::from_arrays(arrays, names); df2.set_multiindex(mi); auto s2 = df2.extract_column_as_numeric_series("A"); check(s2.has_multiindex(), "multiindex propagated"); } } // namespace dataframe_tests_getitem_dispatch int pd_test_getitem_dispatch_main() { std::cout << "====================================== pd_test_getitem_dispatch test suite ==========================" << std::endl; dataframe_tests_getitem_dispatch::g_pass = 0; .. _example-series-set_name-359: .. 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-series-shape-360: .. 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-series-size-361: .. 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) .. _example-series-size-362: .. 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) .. _example-series-size-363: .. 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) .. _example-series-size-364: .. 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) .. _example-series-size-365: .. 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) .. _example-series-size-366: .. 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) .. _example-series-size-367: .. 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) .. _example-series-size-368: .. 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) .. _example-series-size-369: .. 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) .. _example-series-size-370: .. 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) .. _example-series-size-371: .. 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) .. _example-series-size-372: .. 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) .. _example-series-size-373: .. 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) .. _example-series-size-374: .. 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) .. _example-series-size-375: .. 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) .. _example-series-size-376: .. 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) .. _example-series-size-377: .. 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) .. _example-series-size-378: .. 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) .. _example-series-size-379: .. 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) .. _example-series-size-380: .. 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) .. _example-series-size-381: .. 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) .. _example-series-size-382: .. 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) .. _example-series-size-383: .. 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) .. _example-series-size-384: .. 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) .. _example-series-size-385: .. 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) .. _example-series-sparse-386: .. dropdown:: sparse (pd_test_3_all.cpp:20627) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20617 :emphasize-lines: 11 #include #include "../pandas/pd_series.h" // CRITICAL: No using namespace directives namespace dataframe_tests { namespace dataframe_tests_sparse_accessor { // ============================================================================ // Test sparse().density() and sparse().npoints() // ============================================================================ void pd_test_sparse_density_npoints() { std::cout << "========= Series.sparse().density/npoints() ============="; // Create a series with some zeros (sparse values) pandas::Series s({0.0, 1.0, 0.0, 2.0, 0.0, 3.0}); auto sparse = s.sparse(0.0); // 0.0 is the fill value .. _example-series-str-387: .. dropdown:: str (pd_test_1_all.cpp:7137) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7127 :emphasize-lines: 11 // Test basic info() with stringstream std::map> data = { {"A", {1, 2, 3, 4, 5}}, {"B", {10, 20, 30, 40, 50}}, {"C", {100, 200, 300, 400, 500}} }; pandas::DataFrame df(data); std::ostringstream oss; df.info(oss); std::string output = oss.str(); // Verify key components if (output.find("") == std::string::npos) { std::cout << " [FAIL] : info missing class name" << std::endl; throw std::runtime_error("pd_test_dataframe_info failed: missing class name"); } if (output.find("RangeIndex:") == std::string::npos) { std::cout << " [FAIL] : info missing RangeIndex" << std::endl; throw std::runtime_error("pd_test_dataframe_info failed: missing RangeIndex"); } .. _example-series-truncate-388: .. dropdown:: truncate (pd_test_1_all.cpp:20467) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20457 :emphasize-lines: 11 std::vector dates = { "2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05" }; df.set_index(std::make_unique>(dates)); // Truncate to keep only dates from 2020-01-02 to 2020-01-04 pandas::DataFrame result = df.truncate("2020-01-02", "2020-01-04"); bool passed = (result.nrows() == 3); if (!passed) { std::cout << " [FAIL] : in pd_test_timeseries_truncate() : expected 3 rows, got " << result.nrows() << std::endl; throw std::runtime_error("pd_test_timeseries_truncate failed"); } std::cout << " -> tests passed" << std::endl; .. _example-series-values-389: .. dropdown:: values (pd_test_1_all.cpp:364) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 354 :emphasize-lines: 11 pandas::CategoricalArray arr1; if (arr1.size() != 0) { std::cout << " [FAIL] : in pd_test_categorical_array_constructors() : default constructor size != 0" << std::endl; throw std::runtime_error("pd_test_categorical_array_constructors failed: default constructor size != 0"); } if (arr1.ordered()) { std::cout << " [FAIL] : in pd_test_categorical_array_constructors() : default should be unordered" << std::endl; throw std::runtime_error("pd_test_categorical_array_constructors failed: default should be unordered"); } // Constructor from values (infer categories) std::vector> values = { std::optional("a"), std::optional("b"), std::optional("a"), std::optional("c") }; pandas::CategoricalArray arr2(values); if (arr2.size() != 4) { std::cout << " [FAIL] : in pd_test_categorical_array_constructors() : values constructor size != 4" << std::endl; throw std::runtime_error("pd_test_categorical_array_constructors failed: values constructor size != 4");