EWM#
-
class pandas::EWM#
Window operation class for rolling/expanding calculations.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Use EWM
EWM obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
pd_ewm.h:56 |
Construction#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
static EWM |
pd_ewm.h:128 |
|
|
static EWM |
pd_ewm.h:94 |
|
|
static EWM |
pd_ewm.h:111 |
|
|
static EWM |
pd_ewm.h:77 |
|
|
static EWM |
pd_ewm.h:146 |
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
double |
pd_ewm.h:498 |
Statistics#
Aggregation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
EWM |
pd_ewm.h:155 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
pd_ewm.h:489 |
|
|
double |
pd_ewm.h:488 |
|
|
Series<double> |
pd_ewm.h:454 |
|
|
Series<double> |
pd_ewm.h:364 |
|
|
bool |
pd_ewm.h:490 |
|
|
pd_ewm.h:174 |
||
|
void |
pd_ewm.h:509 |
|
|
pd_ewm.h:583 |
||
|
pd_ewm.h:663 |
||
|
pd_ewm.h:740 |
||
|
const Series<T>& |
pd_ewm.h:492 |
|
|
void |
pd_ewm.h:162 |
|
|
Series<double> |
pd_ewm.h:670 |
|
|
Series<double> |
pd_ewm.h:539 |
|
|
Series<double> |
pd_ewm.h:590 |
|
|
double |
pd_ewm.h:530 |
Code Examples#
The following examples are extracted from the test suite.
get_value (pd_test_2_all.cpp:17379)
17369 namespace dataframe_tests_transform {
17370
17371 // Helper to check if two doubles are approximately equal
17372 bool approx_equal(double a, double b, double tol = 1e-9) {
17373 if (std::isnan(a) && std::isnan(b)) return true;
17374 if (std::isnan(a) || std::isnan(b)) return false;
17375 return std::abs(a - b) < tol;
17376 }
17377
17378 // Helper to get double value from DataFrame
17379 double get_value(const pandas::DataFrame& df, size_t row, size_t col) {
17380 return df.iloc<double>(row, col);
17381 }
17382
17383 void pd_test_transform_callable() {
17384 std::cout << "========= transform with callable =====================";
17385
17386 std::map<std::string, std::vector<double>> data = {
17387 {"A", {1.0, 4.0, 9.0, 16.0}},
17388 {"B", {2.0, 3.0, 4.0, 5.0}}
17389 };
mean (pd_test_1_all.cpp:282)
272 std::optional<bool>(true),
273 std::optional<bool>(true)
274 });
275
276 auto s = arr.sum();
277 if (!s.has_value() || s.value() != 3) {
278 std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl;
279 throw std::runtime_error("pd_test_boolean_array_reductions failed: sum");
280 }
281
282 auto m = arr.mean();
283 if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) {
284 std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl;
285 throw std::runtime_error("pd_test_boolean_array_reductions failed: mean");
286 }
287
288 std::cout << " -> tests passed" << std::endl;
289 }
290
291 void pd_test_boolean_array_dtype() {
292 std::cout << "========= BooleanArray: dtype ======================= ";
std_ (pd_test_1_all.cpp:20752)
20742 throw std::runtime_error("pd_test_rolling_min_periods failed: with min_periods=1, idx 1 should be 3.0");
20743 }
20744
20745 std::cout << " -> tests passed" << std::endl;
20746 }
20747
20748 void pd_test_rolling_std() {
20749 std::cout << "========= Rolling std ===========================";
20750
20751 pandas::Series<double> s({1.0, 2.0, 3.0, 4.0, 5.0});
20752 auto result = s.rolling(3).std_();
20753
20754 // std([1,2,3]) = 1.0 (ddof=1)
20755 // std([2,3,4]) = 1.0
20756 // std([3,4,5]) = 1.0
20757 bool passed = std::abs(result[2] - 1.0) < 0.001;
20758 if (!passed) {
20759 std::cout << " [FAIL] : in pd_test_rolling_std() : rolling std should be 1.0" << std::endl;
20760 throw std::runtime_error("pd_test_rolling_std failed: rolling std should be 1.0");
20761 }
var (pd_test_1_all.cpp:20890)
20880 throw std::runtime_error("pd_test_expanding_std failed: expanding std values incorrect");
20881 }
20882
20883 std::cout << " -> tests passed" << std::endl;
20884 }
20885
20886 void pd_test_expanding_var() {
20887 std::cout << "========= Expanding var =========================";
20888
20889 pandas::Series<double> s({1.0, 2.0, 3.0, 4.0, 5.0});
20890 auto result = s.expanding().var();
20891
20892 // Expanding var (ddof=1): NaN, 0.5, 1.0, 1.6667, 2.5
20893 bool passed = std::isnan(result[0]) &&
20894 std::abs(result[1] - 0.5) < 0.001 &&
20895 std::abs(result[2] - 1.0) < 0.001 &&
20896 std::abs(result[3] - 1.6667) < 0.001 &&
20897 std::abs(result[4] - 2.5) < 0.001;
20898 if (!passed) {
20899 std::cout << " [FAIL] : in pd_test_expanding_var() : expanding var values incorrect" << std::endl;
20900 throw std::runtime_error("pd_test_expanding_var failed: expanding var values incorrect");
ewm (pd_test_3_all.cpp:2961)
2951 // Test expanding sum
2952 pandas::DataFrame expanding_sum = df.expanding().sum();
2953 if (expanding_sum.nrows() != 5 || expanding_sum.ncols() != 2) {
2954 throw std::runtime_error("expanding().sum() shape failed");
2955 }
2956
2957 std::cout << " -> tests passed" << std::endl;
2958}
2959
2960void pd_test_3_all_df_ewm() {
2961 std::cout << "========= DataFrame.ewm() ================================";
2962
2963 std::map<std::string, std::vector<double>> data = {
2964 {"A", {1.0, 2.0, 3.0, 4.0, 5.0}},
2965 {"B", {10.0, 20.0, 30.0, 40.0, 50.0}}
2966 };
2967 pandas::DataFrame df(data);
2968
2969 // Test ewm mean with span=3
2970 pandas::DataFrame ewm_mean = df.ewm(std::nullopt, 3.0).mean();
2971 if (ewm_mean.nrows() != 5 || ewm_mean.ncols() != 2) {
corr (pd_test_1_all.cpp:4655)
4645 }
4646
4647 void pd_test_aggregation_dataframe_corr() {
4648 std::cout << "========= DataFrame corr ========================";
4649
4650 std::map<std::string, std::vector<double>> data;
4651 data["A"] = {1.0, 2.0, 3.0, 4.0, 5.0};
4652 data["B"] = {2.0, 4.0, 6.0, 8.0, 10.0}; // Perfect correlation
4653 pandas::DataFrame df(data);
4654
4655 auto corr_df = df.corr();
4656
4657 // Check dimensions
4658 bool passed = corr_df.nrows() == 2 && corr_df.ncols() == 2;
4659 if (!passed) {
4660 std::cout << " [FAIL] : in pd_test_aggregation_dataframe_corr() : corr should be 2x2" << std::endl;
4661 throw std::runtime_error("pd_test_aggregation_dataframe_corr failed: corr should be 2x2");
4662 }
4663
4664 // Diagonal should be 1.0
4665 std::string aa = corr_df["A"].get_value_str(0);
cov (pd_test_1_all.cpp:4690)
4680 std::cout << " -> tests passed" << std::endl;
4681 }
4682
4683 void pd_test_aggregation_dataframe_cov() {
4684 std::cout << "========= DataFrame cov =========================";
4685
4686 std::map<std::string, std::vector<double>> data;
4687 data["A"] = {1.0, 2.0, 3.0};
4688 pandas::DataFrame df(data);
4689
4690 auto cov_df = df.cov();
4691
4692 // Check dimensions
4693 bool passed = cov_df.nrows() == 1 && cov_df.ncols() == 1;
4694 if (!passed) {
4695 std::cout << " [FAIL] : in pd_test_aggregation_dataframe_cov() : cov should be 1x1" << std::endl;
4696 throw std::runtime_error("pd_test_aggregation_dataframe_cov failed: cov should be 1x1");
4697 }
4698
4699 // Var(A) = 1.0 with ddof=1
4700 std::string aa = cov_df["A"].get_value_str(0);
series (pd_test_2_all.cpp:2307)
2297 std::vector<std::string> index = {"a", "b", "c", "d", "e"};
2298
2299 std::map<std::string, std::vector<numpy::float64>> data1;
2300 data1["col1"] = {1.0, 2.0, 3.0, 4.0, 5.0};
2301 data1["col2"] = {2.0, 4.0, 6.0, 8.0, 10.0}; // Perfectly correlated with col1
2302
2303 pandas::DataFrame df1(data1, std::make_unique<pandas::Index<std::string>>(index));
2304
2305 // Series with same index and values that correlate with df columns
2306 pandas::Series<numpy::float64> series({1.0, 2.0, 3.0, 4.0, 5.0});
2307 series.set_index(pandas::Index<std::string>(index));
2308
2309 pandas::Series<numpy::float64> result = df1.corrwith(series);
2310
2311 bool passed = true;
2312 // col1 should have correlation 1.0 with series
2313 if (!approx_equal(result[0], 1.0)) {
2314 std::cout << "\n [FAIL] : Expected correlation 1.0 for col1, got " << result[0] << std::endl;
2315 passed = false;
2316 }