Signature | Description | Parameters |
---|---|---|
template<typename T, typename ... Ts> StdDataFrame<T> get_reindexed(const char *col_to_be_index, const char *old_index_name = nullptr) const; |
It creates and returns a new DataFrame which has the col_to_be_index column as the index. If old_index_name is not null, it will be loaded as a regular column in the result under the name old_index_name. NOTE: If the new index column is shorter than other columns, every column will be cut to that length. NOTE: Columns will not be padded by nan |
T: Type of the "new index" column Ts: List all the types of all data columns. A type should be specified in the list only once. col_to_be_index: Name of the column you want as the new index. This name will not be a column in the result anymore old_index_name: Name of the current index, if converted into a regular column in the result. If this is null, the current index will not be loaded into the result as a column. |
template<typename T, typename ... Ts> StdDataFrame<T> get_reindexed_view(const char *col_to_be_index, const char *old_index_name = nullptr); |
This is similar to get_reindexed(), but it returns a view. Please read above for specs. NOTE: There are certain operations that you cannot do with a view. For example, you cannot add/delete columns, etc. NOTE: Views could not be const, becuase you can change original data through views. |
T: Type of the "new index" column Ts: List all the types of all data columns. A type should be specified in the list only once. col_to_be_index: Name of the column you want as the new index. This name will not be a column in the result anymore old_index_name: Name of the current index, if converted into a regular column in the result. If this is null, the current index will not be loaded into the result as a column. |
static void test_get_reindexed() { std::cout << "\nTesting get_reindexed( ) ..." << 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, 2.0, 1.0, 12.0, 11.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 9.0, 10.0}; std::vector<double> dblvec2 = { 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.55, 107.34, 1.8, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0}; std::vector<int> intvec = { 1, 2, 3, 4, 5, 8, 6, 7, 11, 14, 9 }; std::vector<std::string> strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo" }; df.load_data(std::move(idxvec), std::make_pair("dbl_col", dblvec), std::make_pair("dbl_col_2", dblvec2), std::make_pair("str_col", strvec)); df.load_column("int_col", std::move(intvec), nan_policy::dont_pad_with_nans); auto result1 = df.get_reindexed<double, int, double, std::string>("dbl_col", "OLD_IDX"); assert(result1.get_index().size() == 15); assert(result1.get_column<double>("dbl_col_2").size() == 15); assert(result1.get_column<unsigned long>("OLD_IDX").size() == 15); assert(result1.get_column<std::string>("str_col").size() == 15); assert(result1.get_column<int>("int_col").size() == 11); assert(result1.get_index()[0] == 0); assert(result1.get_index()[14] == 10.0); assert(result1.get_column<int>("int_col")[3] == 4); assert(result1.get_column<int>("int_col")[9] == 14); assert(result1.get_column<std::string>("str_col")[5] == "ff"); assert(result1.get_column<double>("dbl_col_2")[10] == 112.0); auto result2 = df.get_reindexed<int, int, double, std::string>("int_col", "OLD_IDX"); assert(result2.get_index().size() == 11); assert(result2.get_column<double>("dbl_col_2").size() == 11); assert(result2.get_column<double>("dbl_col").size() == 11); assert(result2.get_column<unsigned long>("OLD_IDX").size() == 11); assert(result2.get_column<std::string>("str_col").size() == 11); assert(result2.get_column<double>("dbl_col_2")[10] == 112.0); assert(result2.get_column<double>("dbl_col")[3] == 2.0); assert(result2.get_column<std::string>("str_col")[5] == "ff"); assert(result2.get_index()[0] == 1); assert(result2.get_index()[10] == 9); } // ----------------------------------------------------------------------------- static void test_get_reindexed_view() { std::cout << "\nTesting get_reindexed_view( ) ..." << 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, 2.0, 1.0, 12.0, 11.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 9.0, 10.0}; std::vector<double> dblvec2 = { 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.55, 107.34, 1.8, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0}; std::vector<int> intvec = { 1, 2, 3, 4, 5, 8, 6, 7, 11, 14, 9 }; std::vector<std::string> strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo" }; df.load_data(std::move(idxvec), std::make_pair("dbl_col", dblvec), std::make_pair("dbl_col_2", dblvec2), std::make_pair("str_col", strvec)); df.load_column("int_col", std::move(intvec), nan_policy::dont_pad_with_nans); auto result1 = df.get_reindexed_view<double, int, double, std::string>("dbl_col", "OLD_IDX"); assert(result1.get_index().size() == 15); assert(result1.get_column<double>("dbl_col_2").size() == 15); assert(result1.get_column<unsigned long>("OLD_IDX").size() == 15); assert(result1.get_column<std::string>("str_col").size() == 15); assert(result1.get_column<int>("int_col").size() == 11); assert(result1.get_index()[0] == 0); assert(result1.get_index()[14] == 10.0); assert(result1.get_column<int>("int_col")[3] == 4); assert(result1.get_column<int>("int_col")[9] == 14); assert(result1.get_column<std::string>("str_col")[5] == "ff"); assert(result1.get_column<double>("dbl_col_2")[10] == 112.0); auto result2 = df.get_reindexed_view<int, int, double, std::string>("int_col", "OLD_IDX"); assert(result2.get_index().size() == 11); assert(result2.get_column<double>("dbl_col_2").size() == 11); assert(result2.get_column<double>("dbl_col").size() == 11); assert(result2.get_column<unsigned long>("OLD_IDX").size() == 11); assert(result2.get_column<std::string>("str_col").size() == 11); assert(result2.get_column<double>("dbl_col_2")[10] == 112.0); assert(result2.get_column<double>("dbl_col")[3] == 2.0); assert(result2.get_column<std::string>("str_col")[5] == "ff"); assert(result2.get_index()[0] == 1); assert(result2.get_index()[10] == 9); result2.get_column<double>("dbl_col")[3] = 1002.45; assert(result2.get_column<double>("dbl_col")[3] == 1002.45); assert(df.get_column<double>("dbl_col")[3] == result2.get_column<double>("dbl_col")[3]); }