DataFrameResampler ================== .. cpp:class:: numpy::DataFrameResampler numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use DataFrameResampler DataFrameResampler obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``DataFrameResampler(const DataFrame& df, const std::string& freq, const std::string& closed = "left", const std::string& label = "left")`` - df_resampler.h:106 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector get_numeric_columns() const`` - std::vector - df_resampler.h:179 - * - ``int64_t get_period_key(int64_t epoch_ns) const`` - int64_t - df_resampler.h:177 - Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame max() const`` - DataFrame - df_resampler.h:118 - :ref:`View ` * - ``DataFrame mean() const`` - DataFrame - df_resampler.h:116 - :ref:`View ` * - ``DataFrame median() const`` - DataFrame - df_resampler.h:122 - :ref:`View ` * - ``DataFrame min() const`` - DataFrame - df_resampler.h:117 - :ref:`View ` * - ``DataFrame std\_(int ddof = 1) const`` - DataFrame - df_resampler.h:119 - * - ``DataFrame sum() const`` - DataFrame - df_resampler.h:115 - :ref:`View ` * - ``DataFrame var(int ddof = 1) const`` - DataFrame - df_resampler.h:120 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame agg(const std::string& func_name) const`` - DataFrame - df_resampler.h:128 - * - ``DataFrame agg(const std::vector& funcs) const`` - DataFrame - df_resampler.h:134 - * - ``std::vector aggregate_column(size_t col_idx, const std::string& func) const`` - std::vector - df_resampler.h:182 - * - ``DataFrame bfill() const`` - DataFrame - df_resampler.h:160 - * - ``void build_groups()`` - void - df_resampler.h:176 - * - ``static double compute_agg(const std::vector& values, const std::string& func, int ddof = 1)`` - static double - df_resampler.h:185 - * - ``DataFrame count() const`` - DataFrame - df_resampler.h:121 - :ref:`View ` * - ``DataFrame ffill() const`` - DataFrame - df_resampler.h:157 - * - ``DataFrame first() const`` - DataFrame - df_resampler.h:141 - :ref:`View ` * - ``const std::string& freq() const`` - const std::string& - df_resampler.h:173 - * - ``DataFrame last() const`` - DataFrame - df_resampler.h:144 - * - ``size_t ngroups() const`` - size_t - df_resampler.h:170 - * - ``DataFrame ohlc() const`` - DataFrame - df_resampler.h:150 - * - ``int64_t period_key_to_timestamp(int64_t key) const`` - int64_t - df_resampler.h:178 - * - ``DataFrame size() const`` - DataFrame - df_resampler.h:167 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-dataframeresampler-max-0: .. dropdown:: max (np_test_1_all.cpp:7274) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7264 :emphasize-lines: 11 if (sizeof(uintp) == sizeof(void*)) { // std::cout << " -> uintp size matches pointer size"; } else { // std::cout << " ✗ uintp size doesn't match pointer size" << std::endl; } // Test range limits // std::cout << "Range Information:" << std::endl; // std::cout << " intp min: " << std::numeric_limits::min() << std::endl; // std::cout << " intp max: " << std::numeric_limits::max() << std::endl; // std::cout << " uintp max: " << std::numeric_limits::max() << std::endl; // std::cout << " longdouble digits: " << std::numeric_limits::digits << std::endl; std::cout << " -> tests passed" << std::endl; } void testComplexArithmeticExtendedTypes() { std::cout << "========= testComplexArithmeticExtendedTypes ======================="; clongdouble c1(3.0L, 4.0L); // 3 + 4i .. _example-dataframeresampler-mean-1: .. dropdown:: mean (np_test_1_all.cpp:11714) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11704 :emphasize-lines: 11 // Create test array auto array = createInt32Array({ 2, 3 }, 0); array.setElementAt({ 0, 0 }, 1); array.setElementAt({ 0, 1 }, 2); array.setElementAt({ 0, 2 }, 3); array.setElementAt({ 1, 0 }, 4); array.setElementAt({ 1, 1 }, 5); array.setElementAt({ 1, 2 }, 6); // Test mean without axis auto mean_all = mean(array); if (!(approx_equal(mean_all.getElementAt({ 0 }), 3.5, 1e-10))) { std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mean_all.getElementAt({ 0 }), 3.5, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] mean (all elements) works correctly\n"; // Test mean along axis 0 auto mean_axis0 = mean(array, 0); if (!(mean_axis0.getShape()[0] == 3)) { .. _example-dataframeresampler-median-2: .. dropdown:: median (np_test_1_all.cpp:11882) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11872 :emphasize-lines: 11 std::cout << "========= testMedianAndPercentiles ======================="; // Test median with odd number of elements auto odd_array = createInt32Array({ 5 }, 0); odd_array.setElementAt({ 0 }, 1); odd_array.setElementAt({ 1 }, 3); odd_array.setElementAt({ 2 }, 5); odd_array.setElementAt({ 3 }, 7); odd_array.setElementAt({ 4 }, 9); auto median_odd = median(odd_array); if (!(approx_equal(median_odd.getElementAt({ 0 }), 5.0, 1e-10))) { std::string description = std::string("testMedianAndPercentiles():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(median_odd.getElementAt({ 0 }), 5.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Median with odd count works correctly\n"; // Test median with even number of elements auto even_array = createInt32Array({ 4 }, 0); even_array.setElementAt({ 0 }, 1); .. _example-dataframeresampler-min-3: .. dropdown:: min (np_test_1_all.cpp:2350) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2340 :emphasize-lines: 11 if (i % 3 == 0) { large_array.setElementAt({i}, object_(static_cast(i))); } else if (i % 3 == 1) { large_array.setElementAt({i}, object_(static_cast(i) * 0.5)); } else { large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i))); } } // Verify pattern for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) { // Check first 100 object_ obj = large_array.getElementAt({i}); if (i % 3 == 0) { if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } else if (i % 3 == 1) { if (!(obj.is_type())) { std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; .. _example-dataframeresampler-sum-4: .. dropdown:: sum (np_test_1_all.cpp:11766) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11756 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(approx_equal(mean_axis1.getElementAt({ 1 }), 5.0, 1e-10))) { std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mean_axis1.getElementAt({ 1 }), 5.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] mean along axis 1 works correctly\n"; // Test sum auto sum_all = sum(array); if (!(sum_all.getElementAt({ 0 }) == 21)) { std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(sum_all.getElementAt({ 0 }) == 21)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] sum works correctly\n"; // Test min and max auto min_all = min(array); auto max_all = max(array); .. _example-dataframeresampler-var-5: .. dropdown:: var (np_test_1_all.cpp:11816) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11806 :emphasize-lines: 11 std::cout << "========= testVarianceAndStandardDeviation ======================="; // Create test array with known variance auto array = createFloat64Array({ 4 }, 0); array.setElementAt({ 0 }, 1.0); array.setElementAt({ 1 }, 2.0); array.setElementAt({ 2 }, 3.0); array.setElementAt({ 3 }, 4.0); // Test variance (population) auto var_result = var(array, std::nullopt, 0); // ddof=0 for population variance double expected_var = 1.25; // Known variance for [1,2,3,4] if (!(approx_equal(var_result.getElementAt({ 0 }), expected_var, 1e-10))) { std::string description = std::string("testVarianceAndStandardDeviation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(var_result.getElementAt({ 0 }), expected_var, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Population variance works correctly\n"; // Test variance (sample) auto var_sample = var(array, std::nullopt, 1); // ddof=1 for sample variance .. _example-dataframeresampler-count-6: .. dropdown:: count (np_test_1_all.cpp:3616) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3606 :emphasize-lines: 11 // Create larger arrays for performance testing auto large_arr = NDArray::createOnes({100, 100}); auto broadcast_arr = NDArray::createOnes({1, 100}); // Time the operation (basic timing) auto start = std::chrono::high_resolution_clock::now(); auto result = large_arr.addArrays(broadcast_arr); auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(end - start); // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl; // Verify result shape if (!((result.getShape() == std::vector{100, 100}))) { std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector{100, 100}))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(result.getElementAt({50, 50}) == 2.0)) { std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({50, 50}) == 2.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-dataframeresampler-first-7: .. dropdown:: first (np_test_4_all.cpp:12410) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12400 :emphasize-lines: 11 // std::cout << "[OK] NaN-ignoring behavior tests\n"; } // Test both NaN returns first NaN void test_fmax_both_nan() { std::cout << "========= test_fmax_both_nan ===="; double nan1 = std::numeric_limits::quiet_NaN(); double nan2 = std::numeric_limits::quiet_NaN(); // If both are NaN, return first (which is still NaN) double result = numpy::fmax(nan1, nan2); if (!std::isnan(result)) { std::cout << " [FAIL] : test_fmax_both_nan failed - fmax(NaN, NaN) should be NaN"; throw std::runtime_error("fmax(NaN, NaN) should return NaN"); } // std::cout << "[OK] Both NaN returns NaN test\n"; } // Test type preservation .. _example-dataframeresampler-size-8: .. dropdown:: size (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; using namespace numpy::benchmark; using namespace numpy::char_; // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0);