SeriesResampler =============== .. cpp:class:: numpy::SeriesResampler numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use SeriesResampler SeriesResampler obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``SeriesResampler(const Series& series, const std::string& freq, const std::string& closed = "left", const std::string& label = "left")`` - df_resampler.h:205 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``int64_t get_period_key(int64_t epoch_ns) const`` - int64_t - df_resampler.h:233 - Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series max() const`` - Series - df_resampler.h:214 - :ref:`View ` * - ``Series mean() const`` - Series - df_resampler.h:212 - :ref:`View ` * - ``Series median() const`` - Series - df_resampler.h:218 - :ref:`View ` * - ``Series min() const`` - Series - df_resampler.h:213 - :ref:`View ` * - ``Series std\_(int ddof = 1) const`` - Series - df_resampler.h:215 - * - ``Series sum() const`` - Series - df_resampler.h:211 - :ref:`View ` * - ``Series var(int ddof = 1) const`` - Series - df_resampler.h:216 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series bfill() const`` - Series - df_resampler.h:226 - * - ``void build_groups()`` - void - df_resampler.h:232 - * - ``Series count() const`` - Series - df_resampler.h:217 - :ref:`View ` * - ``Series ffill() const`` - Series - df_resampler.h:225 - * - ``Series first() const`` - Series - df_resampler.h:221 - :ref:`View ` * - ``Series last() const`` - Series - df_resampler.h:222 - * - ``size_t ngroups() const`` - size_t - df_resampler.h:229 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-seriesresampler-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-seriesresampler-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-seriesresampler-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-seriesresampler-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-seriesresampler-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-seriesresampler-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-seriesresampler-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-seriesresampler-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