Signature | Description | Parameters | |
---|---|---|---|
template<typename T> std::size_t replace(const char *col_name, const std::vector<T> &old_values, const std::vector<T> &new_values, int limit = -1); |
It iterates over the column named col_name (or index, if col_name == "INDEX") and replaces all values in old_values with the corresponding values in new_values up to the limit. If limit is omitted, all values will be replaced. It returns number of items replaced. |
T: Type on column col_name. If this is index it would be the same as I. N: Size of old_values and new_values vectors col_name: Name of the column old_values: A vector of values to be replaced in col_name column new_values: A vector of values to replace the old_values in col_name column limit: Limit of how many items to replace. Default is to replace all. |
|
template<typename T> std::future<std::size_t> replace_async(const char *col_name, const std::vector<T> &old_values, const std::vector<T> &new_values, int limit = -1); |
Same as replace() above, but executed asynchronously NOTE: multiple instances of replace_async() maybe executed for different columns at the same time with no problem. |
||
template<typename T, typename F> void replace(const char *col_name, F &functor); |
This is similar to replace() above but it lets a functor replace the values in the named column. The functor is passed every value of the column along with a const reference of the corresponding index value. Unlike the replace version above, this replace can only work on data columns. It will not work on index column. The functor must have the following interface at minimum: bool operator() (const IndexType &ts, T &value);A false return from the above operator method stops the iteration through named column values. |
T: Type on column col_name. If this is index it would be the same as I. F: The functor type col_name: Name of the column functor: An instance of the functor | |
template<typename T, typename F> std::future<void> replace_async(const char *col_name, F &functor); |
Same as replace() above, but executed asynchronously NOTE: multiple instances of replace_async() maybe executed for different columns at the same time with no problem. |
||
std::size_t replace_index(const std::vector<IndexType> &old_values, const std::vector<IndexType> &new_values, int limit = -1); |
This does the same thing as replace() above for the index column |
N: Size of old_values and new_values vectors old_values: A vector of values to be replaced in col_name column new_values: A vector of values to replace the old_values in col_name column limit: Limit of how many items to replace. Default is to replace all. |
static void test_replace_1() { std::cout << "\nTesting replace(1) ..." << std::endl; std::vector<double> d1 = { 1.0, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<double> d2 = { 1.0, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<double> d3 = { 1.1, 10.09, 8.2, 18.03, 19.4, 15.9, 20.8, 17.1, 19.9, 3.3, 2.2, 10.8, 7.4, 5.3, 9.1, 14.9, 14.8, 13.2, 12.6, 6.1, 4.4 }; std::vector<double> d4 = { 0.1, 9.09, 7.2, 17.03, 18.4, 14.9, 19.8, 16.1, 18.9, 2.3, 1.2, 9.8, 6.4, 4.3, 8.1, 13.9, 13.8, 12.2, 11.6, 5.1, 3.4 }; std::vector<double> d5 = { 20.0, 10.1, -30.2, 18.5, 1.1, 16.2, 30.8, -1.56, 20.1, 25.5, 30.89, 11.1, 7.4, 5.3, 19, 15.1, 1.3, 1.2, 12.6, 23.2, 40.1 }; MyDataFrame df; df.load_data(MyDataFrame::gen_datetime_index( "01/01/2018", "01/22/2018", time_frequency::daily), std::make_pair("dblcol_1", d1), std::make_pair("dblcol_2", d2), std::make_pair("dblcol_3", d3), std::make_pair("dblcol_4", d4), std::make_pair("dblcol_5", d5)); assert(df.get_column<double>("dblcol_1")[0] == 1.0); assert(df.get_column<double>("dblcol_1")[20] == 4.0); assert(df.get_column<double>("dblcol_1")[1] == 10.0); assert(df.get_column<double>("dblcol_1")[2] == 8.0); assert(df.get_column<double>("dblcol_1")[6] == 21.0); assert(df.get_column<double>("dblcol_1")[7] == 17.0); assert(df.get_column<double>("dblcol_1")[11] == 11.0); assert(df.get_column<double>("dblcol_1")[15] == 15.0); assert(df.get_column<double>("dblcol_5")[0] == 20.0); assert(df.get_column<double>("dblcol_5")[20] == 40.1); assert(df.get_column<double>("dblcol_5")[1] == 10.1); assert(df.get_column<double>("dblcol_5")[2] == -30.2); assert(df.get_column<double>("dblcol_5")[3] == 18.5); assert(df.get_column<double>("dblcol_5")[10] == 30.89); assert(df.get_column<double>("dblcol_5")[11] == 11.1); assert(df.get_column<double>("dblcol_5")[17] == 1.2); assert(df.get_column<double>("dblcol_5")[19] == 23.2); auto result1 = df.replace_async<double>("dblcol_1", { 10.0, 21.0, 11.0 }, { 1000.0, 2100.0, 1100.0 }); auto idx_result = df.replace_index({ 20180101, 20180102, 20180103 }, { 1000, 2100, 1100 }); auto result2 = df.replace_async<double>("dblcol_5", { -45.0, -100.0, -30.2, 30.89, 40.1, 1.2 }, { 0.0, 0.0, 300.0, 210.0, 110.0, 1200.0 }, 3); auto count = result1.get(); assert(count == 3); assert(df.get_column<double>("dblcol_1")[0] == 1.0); assert(df.get_column<double>("dblcol_1")[20] == 4.0); assert(df.get_column<double>("dblcol_1")[1] == 1000.0); assert(df.get_column<double>("dblcol_1")[2] == 8.0); assert(df.get_column<double>("dblcol_1")[6] == 2100.0); assert(df.get_column<double>("dblcol_1")[7] == 17.0); assert(df.get_column<double>("dblcol_1")[11] == 1100.0); assert(df.get_column<double>("dblcol_1")[15] == 15.0); count = result2.get(); assert(count == 3); assert(df.get_column<double>("dblcol_5")[0] == 20.0); assert(df.get_column<double>("dblcol_5")[20] == 110.0); assert(df.get_column<double>("dblcol_5")[1] == 10.1); assert(df.get_column<double>("dblcol_5")[2] == 300.0); assert(df.get_column<double>("dblcol_5")[3] == 18.5); assert(df.get_column<double>("dblcol_5")[10] == 210.0); assert(df.get_column<double>("dblcol_5")[11] == 11.1); assert(df.get_column<double>("dblcol_5")[17] == 1.2); assert(df.get_column<double>("dblcol_5")[19] == 23.2); } // ----------------------------------------------------------------------------- static void test_replace_2() { std::cout << "\nTesting replace(2) ..." << std::endl; std::vector<double> d1 = { 1.0, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<double> d2 = { 1.0, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<double> d3 = { 1.1, 10.09, 8.2, 18.03, 19.4, 15.9, 20.8, 17.1, 19.9, 3.3, 2.2, 10.8, 7.4, 5.3, 9.1, 14.9, 14.8, 13.2, 12.6, 6.1, 4.4 }; std::vector<double> d4 = { 0.1, 9.09, 7.2, 17.03, 18.4, 14.9, 19.8, 16.1, 18.9, 2.3, 1.2, 9.8, 6.4, 4.3, 8.1, 13.9, 13.8, 12.2, 11.6, 5.1, 3.4 }; std::vector<double> d5 = { 20.0, 10.1, -30.2, 18.5, 1.1, 16.2, 30.8, -1.56, 20.1, 25.5, 30.89, 11.1, 7.4, 5.3, 19, 15.1, 1.3, 1.2, 12.6, 23.2, 40.1 }; MyDataFrame df; df.load_data(MyDataFrame::gen_datetime_index( "01/01/2018", "01/22/2018", time_frequency::daily), std::make_pair("dblcol_1", d1), std::make_pair("dblcol_2", d2), std::make_pair("dblcol_3", d3), std::make_pair("dblcol_4", d4), std::make_pair("dblcol_5", d5)); assert(df.get_column<double>("dblcol_1")[0] == 1.0); assert(df.get_column<double>("dblcol_1")[19] == 6.0); assert(df.get_column<double>("dblcol_1")[20] == 4.0); assert(df.get_column<double>("dblcol_1")[2] == 8.0); assert(df.get_column<double>("dblcol_1")[14] == 9.0); ReplaceFunctor functor; auto result = df.replace_async<double, ReplaceFunctor>("dblcol_1", functor); result.get(); assert(functor.count == 3); assert(df.get_column<double>("dblcol_1")[0] == 1.0); assert(df.get_column<double>("dblcol_1")[19] == 6.0); assert(df.get_column<double>("dblcol_1")[20] == 4000.0); assert(df.get_column<double>("dblcol_1")[2] == 8000.0); assert(df.get_column<double>("dblcol_1")[14] == 9000.0); auto seq_vec = MyDataFrame::gen_sequence_index(1, 200, 4); assert(seq_vec.size() == 50); assert(seq_vec[0] == 1); assert(seq_vec[2] == 9); assert(seq_vec[3] == 13); assert(seq_vec[49] == 197); assert(seq_vec[48] == 193); }