Signature Description Parameters

template<typename V>
std::size_t
load_result_as_column(V &visitor,
                      const char *new_col_name,
                      nan_policy padding = nan_policy::pad_with_nans);
        
This is a convenient method to load the result of a visitor (i.e. algorithm) as a column in DataFrame
For this method to work:
  1. The visitor must have a result() method
  2. The result must be a vector
  3. The visitor must define result_type type
  4. Caller must to run the visitor before this call, so the result is already populated
NOTE: This call moves the result vector to the DataFrame. After the call the the visitor's result vector will be empty
V: Visitor type
visitor: A reference to a visitor instance
new_col_name: Name of the new column to be added
padding: If true, it pads the data column with nan if it is shorter than the index column.

template<typename T, typename V>
std::size_t
load_result_as_column(const char *col_name,
                      V &&visitor,
                      const char *new_col_name,
                      nan_policy padding = nan_policy::pad_with_nans);
        
This is a shortcut to running an algorithm and loading its vectored result as a column in one shot. It runs and loads the result() of a visitor into the named column.
For this method to work:
  1. The visitor must have a result() method
  2. The result must be a vector
  3. The visitor must define result_type type
T: Column type to be passed to the visitor
V: Visitor type
col_name: Name of the column to be passed to the visitor
visitor: A reference to a visitor instance
new_col_name: Name of the new column to be added
padding: If true, it pads the data column with nan if it is shorter than the index column.

template<typename T1, typename T2, typename V>
std::size_t
load_result_as_column(const char *col_name1,
                      const char *col_name2,
                      V &&visitor,
                      const char *new_col_name,
                      nan_policy padding = nan_policy::pad_with_nans);
        
Same as above but supporting two columns. T1: First column type to be passed to the visitor
T2: Second column type to be passed to the visitor
V: Visitor type
col_name1: Name of the first column to be passed to the visitor
col_name2: Name of the second column to be passed to the visitor
visitor: A reference to a visitor instance
new_col_name: Name of the new column to be added
padding: If true, it pads the data column with nan if it is shorter than the index column.

template<typename T1, typename T2, typename T3, typename V>
std::size_t
load_result_as_column(const char *col_name1,
                      const char *col_name2,
                      const char *col_name3,
                      V &&visitor,
                      const char *new_col_name,
                      nan_policy padding = nan_policy::pad_with_nans);
        
Same as above but supporting three columns. T1: First column type to be passed to the visitor
T2: Second column type to be passed to the visitor
T3: Third column type to be passed to the visitor
V: Visitor type
col_name1: Name of the first column to be passed to the visitor
col_name2: Name of the second column to be passed to the visitor
col_name3: Name of the third column to be passed to the visitor
visitor: A reference to a visitor instance
new_col_name: Name of the new column to be added
padding: If true, it pads the data column with nan if it is shorter than the index column.

template<typename T1, typename T2, typename T3, typename T4, typename V>
std::size_t
load_result_as_column(const char *col_name1,
                      const char *col_name2,
                      const char *col_name3,
                      const char *col_name4,
                      V &&visitor,
                      const char *new_col_name,
                      nan_policy padding = nan_policy::pad_with_nans);
        
Same as above but supporting four columns. T1: First column type to be passed to the visitor
T2: Second column type to be passed to the visitor
T3: Third column type to be passed to the visitor
T4: Fourth column type to be passed to the visitor
V: Visitor type
col_name1: Name of the first column to be passed to the visitor
col_name2: Name of the second column to be passed to the visitor
col_name3: Name of the third column to be passed to the visitor
col_name4: Name of the fourth column to be passed to the visitor
visitor: A reference to a visitor instance
new_col_name: Name of the new column to be added
padding: If true, it pads the data column with nan if it is shorter than the index column.
static void test_load_result_as_column()  {

    std::cout << "\nTesting load_result_as_column( ) ..." << std::endl;

    StrDataFrame    df;

    try  {
        df.read("data/SHORT_IBM.csv", io_format::csv2);

        // CoppockCurveVisitor
        //
        coppc_v<double, std::string>  copp;

        df.single_act_visit<double>("IBM_Close", copp);
        df.load_result_as_column(copp, "IBM_close_curve");

        const auto &curve_col = df.get_column<double>("IBM_close_curve");

        assert(curve_col.size() == 1721);
        assert(std::isnan(curve_col[0]));
        assert(std::abs(curve_col[14] - -0.051884971603) < 0.0000001);
        assert(std::abs(curve_col[18] - -0.100660882748) < 0.0000001);
        assert(std::abs(curve_col[25] - -0.124090378548) < 0.0000001);
        assert(std::abs(curve_col[1720] - -0.219247796696) < 0.0000001);
        assert(std::abs(curve_col[1712] - 0.0630742594051) < 0.0000001);
        assert(std::abs(curve_col[1707] - 0.0766481878384) < 0.0000001);
        assert(copp.get_result().size() == 0); // Data was moved
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
    }
}

// -----------------------------------------------------------------------------

static void test_load_result_as_column2()  {

    std::cout << "\nTesting load_result_as_column2( ) ..." << std::endl;

    using chop_t = chop_v<double, std::string>;
    using coppc_t = coppc_v<double, std::string>;

    StrDataFrame    df;

    try  {
        df.read("data/SHORT_IBM.csv", io_format::csv2);

        // Choppiness Index indicator
        //
        df.load_result_as_column<double, double, double>("IBM_Close", "IBM_High", "IBM_Low", chop_t(), "IBM_choppy");

        // CoppockCurveVisitor
        //
        df.load_result_as_column<double>("IBM_Close", coppc_t(), "IBM_close_curve");

        const auto &choppy_col = df.get_column<double>("IBM_choppy");

        assert(choppy_col.size() == 1721);
        assert(std::isnan(choppy_col[0]));
        assert(std::isnan(choppy_col[12]));
        assert(std::abs(choppy_col[20] - 39.3577) < 0.0001);
        assert(std::abs(choppy_col[25] - 31.2701) < 0.0001);
        assert(std::abs(choppy_col[30] - 40.8049) < 0.0001);
        assert(std::abs(choppy_col[1720] - 27.7729) < 0.0001);
        assert(std::abs(choppy_col[1712] - 37.9124) < 0.0001);
        assert(std::abs(choppy_col[1707] - 34.344) < 0.0001);

        const auto &curve_col = df.get_column<double>("IBM_close_curve");

        assert(curve_col.size() == 1721);
        assert(std::isnan(curve_col[0]));
        assert(std::abs(curve_col[14] - -0.051884971603) < 0.0000001);
        assert(std::abs(curve_col[18] - -0.100660882748) < 0.0000001);
        assert(std::abs(curve_col[25] - -0.124090378548) < 0.0000001);
        assert(std::abs(curve_col[1720] - -0.219247796696) < 0.0000001);
        assert(std::abs(curve_col[1712] - 0.0630742594051) < 0.0000001);
        assert(std::abs(curve_col[1707] - 0.0766481878384) < 0.0000001);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
    }
}
C++ DataFrame