Signature | Description | Parameters |
---|---|---|
#include <DataFrame/DataFrameStatsVisitors.h> template<typename T, typename I = unsigned long> struct CategoryVisitor; |
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 categorizes the given column values with unsigned long starting with 0. The result is a vector of integers with the same length as the column. Each unique value in the column has a unique integer associated with it. This could be used to create statistical category columns. The constructor takes an unsigned value to represent NaN values. The default is unsigned long value of -1. explicit CategoryVisitor(std::size_t nan_val = std::size_t(-1));The result is a vector of std::size_t |
T: Column data type. I: Index type. |
static void test_CategoryVisitor() { std::cout << "\nTesting CategoryVisitor{ } ..." << std::endl; MyDataFrame df; std::vector<unsigned long> idxvec = { 1UL, 2UL, 3UL, 10UL, 5UL, 7UL, 8UL, 12UL, 9UL, 12UL, 10UL, 13UL, 10UL, 15UL, 14UL }; std::vector<double> dblvec = { 0.0, 15.0, 14.0, 15.0, 1.0, 12.0, 11.0, 8.0, 15.0, 6.0, sqrt(-1), 4.0, 14.0, 14.0, 20.0}; std::vector<int> intvec = { 1, 2, 3, 4, 5, 8, 6, 7, 11, 14, 9 }; std::vector<std::string> strvec = { "zz", "bb", "zz", "ww", "ee", "ff", "gg", "zz", "ii", "jj", "kk", "ll", "mm", "ee", "" }; df.load_data(std::move(idxvec), std::make_pair("dbl_col", dblvec), std::make_pair("str_col", strvec)); df.load_column("int_col", std::move(intvec), nan_policy::dont_pad_with_nans); CategoryVisitor<double> cat; auto result = df.single_act_visit<double>("dbl_col", cat).get_result(); assert(result.size() == 15); assert(result[0] == 0); assert(result[1] == 1); assert(result[2] == 2); assert(result[3] == 1); assert(result[4] == 3); assert(result[8] == 1); assert(result[13] == 2); assert(result[12] == 2); assert(result[11] == 8); assert(result[10] == static_cast<unsigned long>(-1)); CategoryVisitor<std::string> cat2; auto result2 = df.single_act_visit<std::string>("str_col", cat2).get_result(); assert(result2.size() == 15); assert(result2[0] == 0); assert(result2[1] == 1); assert(result2[2] == 0); assert(result2[13] == 3); }