Back to Documentations

Signature Description Parameters
template<typename FROM_T, typename TO_T, typename F>
void
retype_column(const char *name, F &&convert_func) requires
std::invocable<F, const FROM_T &> &&
std::same_as<std::invoke_result_t<F, const FROM_T &>, TO_T>;
It changes the type of the named column. The change happens by calling convert_func on each element of named column.
NOTE: This will copy data.
FROM_T: Current type of the named column
TO_T: New type to be of the named column
F: Converter function type
name: Column name
convert_func: A function to change each element of named column from FROM_T to TO_T type.
static void test_retype_column()  {

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

    StlVecType<unsigned long>  idxvec = { 1UL, 2UL, 3UL, 10UL, 5UL, 7UL, 8UL, 12UL, 9UL, 12UL, 10UL, 13UL, 10UL, 15UL, 14UL };
    StlVecType<int>            intvec = { -1, 2, 3, 4, 5, 8, -6, 7, 11, 14, -9, 12, 13, 14, 15 };
    StlVecType<std::string>    strvec = { "11", "22", "33", "44", "55", "66", "-77", "88", "99", "100", "101", "102", "103", "104", "-105" };
    MyDataFrame                df;

    df.load_data(std::move(idxvec),
                 std::make_pair("str_col", strvec),
                 std::make_pair("int_col", intvec));

    df.retype_column<int, long>("int_col", [](const int &val) -> long { return (long(val)); });
    assert(df.get_index().size() == 15);
    assert(df.get_column<long>("int_col").size() == 15);
    assert(df.get_column<long>("int_col")[0] == -1L);
    assert(df.get_column<long>("int_col")[1] == 2L);
    assert(df.get_column<long>("int_col")[6] == -6L);
    assert(df.get_column<long>("int_col")[8] == 11L);
    assert(df.get_column<std::string>("str_col")[0] == "11");
    assert(df.get_column<std::string>("str_col")[6] == "-77");

    df.retype_column<std::string, int>("str_col", [](const std::string &val) -> int { return (std::stoi(val)); });
    assert(df.get_index().size() == 15);
    assert(df.get_column<long>("int_col").size() == 15);
    assert(df.get_column<int>("str_col").size() == 15);
    assert(df.get_column<long>("int_col")[6] == -6L);
    assert(df.get_column<long>("int_col")[8] == 11L);
    assert(df.get_column<int>("str_col")[0] == 11);
    assert(df.get_column<int>("str_col")[6] == -77);
}

C++ DataFrame