DataFrameEWM ============ .. cpp:class:: pandas::DataFrameEWM Window operation class for rolling/expanding calculations. Example ------- .. code-block:: cpp #include using namespace pandas; // Use DataFrameEWM DataFrameEWM obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``DataFrameEWM(const DataFrame& df, double span, bool adjust = true, bool ignore_na = false)`` - pd_dataframe.h:12801 - Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame mean() const`` - DataFrame - pd_dataframe.h:12816 - :ref:`View ` * - ``DataFrame std(int ddof = 1) const`` - DataFrame - pd_dataframe.h:12829 - :ref:`View ` * - ``DataFrame var(int ddof = 1) const`` - DataFrame - pd_dataframe.h:12835 - :ref:`View ` Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DataFrame apply_time_based(Func&& func) const`` - DataFrame - pd_dataframe.h:12875 - * - ``DataFrame apply_to_columns(Func&& func) const`` - DataFrame - pd_dataframe.h:12849 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool adjust() const`` - bool - pd_dataframe.h:12843 - * - ``const DataFrame& dataframe() const`` - const DataFrame& - pd_dataframe.h:12845 - :ref:`View ` * - ``bool ignore_na() const`` - bool - pd_dataframe.h:12844 - * - ``void set_precomputed_mean(std::map> data, std::unique_ptr idx)`` - void - pd_dataframe.h:12804 - * - ``void set_time_based(std::vector timestamps, double halflife_seconds)`` - void - pd_dataframe.h:12810 - * - ``double span() const`` - double - pd_dataframe.h:12842 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-dataframeewm-mean-0: .. 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-dataframeewm-std-1: .. dropdown:: std (pd_test_1_all.cpp:4526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4516 :emphasize-lines: 11 #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; } void pd_test_aggregation_series_quantile() { .. _example-dataframeewm-var-2: .. 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-dataframeewm-dataframe-3: .. dropdown:: dataframe (pd_test_2_all.cpp:11742) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11732 :emphasize-lines: 11 std::cout << " [FAIL] : wrong dimensions" << std::endl; std::remove(temp_path.c_str()); throw std::runtime_error("pd_test_to_hdf_mixed_types failed"); } std::remove(temp_path.c_str()); std::cout << " -> tests passed" << std::endl; } void pd_test_to_hdf_empty_dataframe() { std::cout << "========= to_hdf empty dataframe (real HDF5) ==================="; pandas::DataFrame df; std::string temp_path = "temp/test_hdf5_empty.h5"; df.to_hdf(temp_path, "df", "w"); // Just verify file was created std::ifstream file(temp_path); if (!file.is_open()) { std::cout << " [FAIL] : file not created" << std::endl; throw std::runtime_error("pd_test_to_hdf_empty_dataframe failed"); .. _example-dataframeewm-span-4: .. dropdown:: span (pd_test_5_all.cpp:57296) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 57286 :emphasize-lines: 11 f_test_astype_reindex_chain_coverage_23_main_ns::f_test_astype_reindex_chain_coverage_23_main(); } // --- f_test_format_helpers_coverage_9.cpp --- namespace f_test_format_helpers_coverage_9_main_ns { static void f_format_helpers_coverage_8b2f47_case_A01_empty_span_returns_min(int& local_fail) { std::cout << "-- case_A01_empty_span_returns_min\n"; std::vector v; int p = pandas::detail::infer_float_repr_precision(std::span(v)); pandas_tests::check(p == 1, "format_helpers_coverage_8b2f47_case_A01.empty_returns_min1", local_fail); } static void f_format_helpers_coverage_8b2f47_case_A02_all_integer_returns_min(int& local_fail) { std::cout << "-- case_A02_all_integer_returns_min\n"; std::vector v = {1.0, 2.0, 3.0}; int p = pandas::detail::infer_float_repr_precision(std::span(v)); pandas_tests::check(p == 1, "format_helpers_coverage_8b2f47_case_A02.all_integer_returns_min1", local_fail);