DataFrameGroupByResampler ========================= .. cpp:class:: pandas::DataFrameGroupByResampler pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use DataFrameGroupByResampler DataFrameGroupByResampler obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``DataFrameGroupByResampler(const DataFrameGroupBy& gb, const std::string& freq, const std::string& closed = "left", const std::string& label = "left")`` - pd_groupby.h:4877 - Missing Data ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame bfill() const`` - DataFrame - pd_groupby.h:5001 - :ref:`View ` * - ``DataFrame ffill() const`` - DataFrame - pd_groupby.h:4915 - :ref:`View ` Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame agg_impl_(const std::string& method) const`` - DataFrame - pd_groupby.h:4382 - * - ``void aggregate_string_cols_( const DataFrame& sub_df, const DataFrame& result, size_t nrows, const std::string& method, const std::vector& col_order, const std::set& string_cols, std::map>& combined_str_data) const`` - void - pd_groupby.h:4561 - * - ``const DataFrameGroupBy& groupby() const`` - const DataFrameGroupBy& - pd_groupby.h:4883 - :ref:`View ` * - ``static DataFrame resample_one_(DataFrameResampler& resampler, const std::string& method)`` - static DataFrame - pd_groupby.h:4543 - * - ``DataFrameResampler resampler(sub_df, freq_str_, closed_, label_)`` - DataFrameResampler - pd_groupby.h:4419 - Combining --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static void append_group_levels_( std::vector>& group_level_vals, const std::string& gkey, const std::vector& by_cols)`` - static void - pd_groupby.h:4723 - * - ``append_group_levels_(group_level_vals, gkey, by_cols)`` - - pd_groupby.h:4970 - * - ``append_group_levels_(group_level_vals, gkey, by_cols)`` - - pd_groupby.h:4982 - * - ``append_group_levels_(group_level_vals, gkey, by_cols)`` - - pd_groupby.h:5049 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector> arrays(nlev + 1)`` - std::vector> - pd_groupby.h:4642 - :ref:`View ` * - ``std::vector> arrays(nlev + 1)`` - std::vector> - pd_groupby.h:4693 - :ref:`View ` * - ``DatetimeIndex build_group_grid_(const DatetimeIndex& group_idx) const`` - DatetimeIndex - pd_groupby.h:4334 - * - ``void build_multiindex_( DataFrame& df, const std::vector& level0_vals, const std::vector& level1_vals, const std::vector& by_cols, const std::vector& group_keys) const`` - void - pd_groupby.h:4625 - * - ``build_multiindex_presplit_(final_result, group_level_vals, level_dt_vals, by_cols, group_keys)`` - - pd_groupby.h:4535 - * - ``void build_multiindex_presplit_( DataFrame& df, const std::vector>& group_level_vals, const std::vector& dt_vals, const std::vector& by_cols, const std::vector& group_keys) const`` - void - pd_groupby.h:4677 - * - ``static void classify_columns_( const DataFrame& sub_df, std::vector& col_order, std::set& numeric_cols, std::set& string_cols)`` - static void - pd_groupby.h:4821 - * - ``classify_columns_(sub_df, col_order, numeric_cols, string_cols)`` - - pd_groupby.h:4936 - * - ``classify_columns_(sub_df, col_order, numeric_cols, string_cols)`` - - pd_groupby.h:5021 - * - ``const std::string& closed() const`` - const std::string& - pd_groupby.h:4885 - :ref:`View ` * - ``DataFrame finalize_fill_result_( std::map>& combined_num_data, std::map>& combined_str_data, const std::vector& col_order, const std::set& string_cols, const std::vector>& group_level_vals, const std::vector& level_dt_vals, const std::vector& by_cols, const std::vector& group_keys) const`` - DataFrame - pd_groupby.h:4772 - * - ``std::string format_bucket_end_label_(const std::string& result_date_str) const`` - std::string - pd_groupby.h:4311 - * - ``format_bucket_end_label_(result.index().get_value_str(r)))`` - - pd_groupby.h:4457 - * - ``std::string format_grid_label_(const DatetimeIndex& grid, size_t gi) const`` - std::string - pd_groupby.h:4325 - * - ``const std::string& freq() const`` - const std::string& - pd_groupby.h:4884 - :ref:`View ` * - ``const DateOffset& freq_offset_() const`` - const DateOffset& - pd_groupby.h:4275 - * - ``std::vector> group_level_vals(by_cols.size())`` - std::vector> - pd_groupby.h:4391 - * - ``std::vector> group_level_vals(by_cols.size())`` - std::vector> - pd_groupby.h:4920 - * - ``std::vector> group_level_vals(by_cols.size())`` - std::vector> - pd_groupby.h:5006 - * - ``DatetimeIndex idx(values, sub_df.index().name())`` - DatetimeIndex - pd_groupby.h:4854 - :ref:`View ` * - ``const std::string& label() const`` - const std::string& - pd_groupby.h:4886 - :ref:`View ` * - ``static GroupDateMap_ parse_index_to_datetime_index_(const DataFrame& sub_df)`` - static GroupDateMap_ - pd_groupby.h:4848 - * - ``static void push_value_( const DataFrame& sub_df, const std::string& col, size_t src_row, const std::set& numeric_cols, std::map>& combined_num_data, std::map>& combined_str_data)`` - static void - pd_groupby.h:4747 - * - ``DatetimeIndex result(out, group_idx.name())`` - DatetimeIndex - pd_groupby.h:4371 - :ref:`View ` * - ``static std::string strip_time_if_day_or_larger_(const std::string& s, int64_t stride_ns)`` - static std::string - pd_groupby.h:4301 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-dataframegroupbyresampler-bfill-0: .. 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-dataframegroupbyresampler-ffill-1: .. 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-dataframegroupbyresampler-groupby-2: .. 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-dataframegroupbyresampler-arrays-3: .. dropdown:: arrays (pd_test_1_all.cpp:10642) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10632 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Set Operations Tests // ============================================================================ void pd_test_extension_index_append() { std::cout << "========= append ========================="; // Use same categories for both arrays (required by CategoricalArray::concat) std::vector cats = {"a", "b", "c", "d"}; pandas::CategoricalArray arr1({"a", "b"}, cats); pandas::CategoricalIndex idx1(arr1); pandas::CategoricalArray arr2({"c", "d"}, cats); pandas::CategoricalIndex idx2(arr2); auto appended = idx1.append(idx2); bool passed = (appended.size() == 4); .. _example-dataframegroupbyresampler-arrays-4: .. dropdown:: arrays (pd_test_1_all.cpp:10642) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10632 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // ============================================================================ // Set Operations Tests // ============================================================================ void pd_test_extension_index_append() { std::cout << "========= append ========================="; // Use same categories for both arrays (required by CategoricalArray::concat) std::vector cats = {"a", "b", "c", "d"}; pandas::CategoricalArray arr1({"a", "b"}, cats); pandas::CategoricalIndex idx1(arr1); pandas::CategoricalArray arr2({"c", "d"}, cats); pandas::CategoricalIndex idx2(arr2); auto appended = idx1.append(idx2); bool passed = (appended.size() == 4); .. _example-dataframegroupbyresampler-closed-5: .. dropdown:: closed (pd_test_1_all.cpp:1903) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1893 :emphasize-lines: 11 // ============================================================================ void test_constructors() { std::cout << "========= IntervalArray: constructors ======================= "; // Default constructor pandas::IntervalArrayFloat64 empty; if (empty.size() != 0) { std::cout << "[FAIL] : in test_constructors() : default constructor size" << std::endl; return; } if (empty.closed() != pandas::IntervalClosed::Right) { std::cout << "[FAIL] : in test_constructors() : default closure" << std::endl; return; } // Constructor from left/right arrays numpy::NDArray left(std::vector{3}); numpy::NDArray right(std::vector{3}); left.setElementAt({0}, 0.0); right.setElementAt({0}, 1.0); left.setElementAt({1}, 1.0); right.setElementAt({1}, 2.0); left.setElementAt({2}, 2.0); right.setElementAt({2}, 3.0); .. _example-dataframegroupbyresampler-freq-6: .. dropdown:: freq (pd_test_1_all.cpp:8233) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8223 :emphasize-lines: 11 std::cout << "========= freq property ==============================="; std::vector> values = { numpy::datetime64(0LL, numpy::DateTimeUnit::Nanosecond), numpy::datetime64(86400000000000LL, numpy::DateTimeUnit::Nanosecond) // 1 day }; pandas::DatetimeArray arr(values); pandas::DatetimeMixinIndex idx(arr); // Default freq is nullopt or inferred auto f = idx.freq(); std::string fs = idx.freqstr(); bool passed = true; // freq may or may not be set if (!passed) { std::cout << " [FAIL] : in pd_test_datetime_mixin_freq()" << std::endl; throw std::runtime_error("pd_test_datetime_mixin_freq failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-dataframegroupbyresampler-idx-7: .. dropdown:: idx (pd_test_1_all.cpp:5266) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5256 :emphasize-lines: 11 throw std::runtime_error("pd_test_categorical_index_default_constructor failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_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.inferred_type() == "categorical"); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_array_constructor()" << std::endl; throw std::runtime_error("pd_test_categorical_index_array_constructor failed"); } std::cout << " -> tests passed" << std::endl; .. _example-dataframegroupbyresampler-label-8: .. dropdown:: label (pd_test_4_all.cpp:4935) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4925 :emphasize-lines: 11 // Helper: compare and report // ---------------------------------------------------------------------------- static void check_str(const std::string& label, const std::string& expected, const std::string& actual) { int _f = 0; pandas_tests::check_str_ws(label, expected, actual, _f); if (_f > 0) throw std::runtime_error(label + ": str mismatch"); } // Slugify a python compare-test label ("a.b.c" → "a_b_c") matching the // scheme in scripts/gen_repr_mismatch_fixtures.py. static std::string slugify_label(const std::string& label) { std::string out = label; for (char& ch : out) { if (ch == '.') ch = '_'; } return out; } // Load a captured pandas-generated expected output file. The file is written .. _example-dataframegroupbyresampler-result-9: .. 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) {