Back to Documentations

Signature Description Parameters
#include <DataFrame/DataFrameStatsVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct KthValueVisitor;

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using kthv_v = KthValueVisitor<T, I, A>;
This is a single action visitor, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This functor class finds the Kth element in the given column in linear time.
    explicit
    KthValueVisitor (std::size_t ke, bool skipnan = true);
        
T: Column data type
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
#include <DataFrame/DataFrameStatsVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct NKthValueVisitor;

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using nkthv_v = NKthValueVisitor<T, I, A>;
This is a single action visitor, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This is the same as above KthValueVisitor but you can give it a vector of Kth values. If you have multiple Kth values, this visitor saves you memory and complexity of running multiple KthValueVisitor’s.
    explicit
    NKthValueVisitor (std::vector &&ke, bool skipnan = true);
        
T: Column data type
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_median()  {

    std::cout << "\nTesting Median ..." << std::endl;

    StlVecType<unsigned long>   idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466, 123467, 123468, 123469, 123470, 123471, 123472, 123473 };
    StlVecType<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 };
    StlVecType<double>          d2 = { 1.0, 10, 8, 18, 19, 16, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 };
    StlVecType<int>             i1 = { 1, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7, 5, 9, 15, 14, 13, 12, 6, 4 };
    StlVecType<int>             i2 = { 1, 10, 8, 18, 19, 16, 17, 20, 3, 2, 11, 7, 5, 9, 15, 14, 13, 12, 6, 4 };
    MyDataFrame                 df;

    df.load_data(std::move(idx),
                 std::make_pair("dblcol_1", d1),
                 std::make_pair("intcol_1", i1));
    df.load_column<double>("dblcol_2", std::move(d2), nan_policy::dont_pad_with_nans);
    df.load_column<int>("intcol_2", std::move(i2), nan_policy::dont_pad_with_nans);

    MedianVisitor<double>   med_visit;
    double                  result = df.single_act_visit<double>("dblcol_1", med_visit, true).get_result();

    assert(result == 11.0);

    result = df.single_act_visit<double>("dblcol_2", med_visit).get_result();
    assert(result == 10.50);

    MedianVisitor<int>  med_visit2;
    int                 result2 = df.single_act_visit<int>("intcol_1", med_visit2).get_result();

    assert(result2 == 11);

    result2 = df.single_act_visit<int>("intcol_2", med_visit2).get_result();
    assert(result2 == 10);

    using TestDF = StdDataFrame<std::string>;

    std::vector<std::string> syms = { "AAPL", "IBM", "TSLA", "MSFT", "CSCO" };
    std::vector<double>      c1 = { 1.0, 2.0, 3.0, 4.0 };
    std::vector<double>      c2 = { 0.01, 0.02, 0.03 };
    std::vector<double>      c3 = { 0.0, std::numeric_limits<double>::quiet_NaN(), 0.1 };
    TestDF                   testDF ;

    testDF.load_data(std::move(syms),
                     std::make_pair("c1",  c1),
                     std::make_pair("c2",  c2),
                     std::make_pair("c3",  c3));

    MedianVisitor<double, std::string>  md { true };  // skip nan

    assert((testDF.single_act_visit<double>("c1", md).get_result() == 2.5));
    assert((testDF.single_act_visit<double>("c2", md).get_result() == 0.02));
    assert((testDF.single_act_visit<double>("c3", md).get_result() == 0.05));

    NKthValueVisitor<double>    nkth_v { { 1, 5, 3, 2, 7 } };

    df.single_act_visit<double>("dblcol_1", nkth_v);

    const auto  &kth_result { nkth_v.get_result() };

    assert(kth_result.size() == 5);
    assert(kth_result[0] == 1.0);
    assert(kth_result[1] == 5.0);
    assert(kth_result[2] == 3.0);
    assert(kth_result[3] == 2.0);
    assert(kth_result[4] == 7.0);
}

C++ DataFrame