Signature | Description | Parameters |
---|---|---|
template<typename ... Ts> DataFrame get_data_by_idx(Index2D<IndexType> range) const; |
It returns a DataFrame (including the index and data columns) containing the data from index begin to index end. This function assumes the DataFrame is consistent and sorted by index. The behavior is undefined otherwise. |
Ts: The list of types for all columns. A type should be specified only once. range: The begin and end iterators for index specified with index values |
template<typename ... Ts> DataFrame get_data_by_idx(const std::vector<IndexType> &values) const; |
It returns a DataFrame (including the index and data columns) containing the data corresponding to the indices specified in "values" vector. This method runs in O(n), where n is the number of indices, by creating a hash table of values. IndexType must be hash able. NOTE: The returned DataFrame is in the same order as original DataFrame |
Ts: The list of types for all columns. A type should be specified only once. values: List of indices to copy data from |
template<typename ... Ts> View get_view_by_idx(Index2D<IndexType> range); |
It behaves like get_data_by_idx(range), but it returns a View. A view is a DataFrame that is a reference to the original DataFrame. So if you modify anything in the view the original DataFrame will also be modified. NOTE: There are certain operations that you cannot do with a view. For example, you cannot add/delete columns, etc. |
Ts: The list of types for all columns. A type should be specified only once. range: The begin and end iterators for index specified with index values |
template<typename ... Ts> PtrView get_view_by_idx(const std::vector<IndexType> &values); |
It behaves like get_data_by_idx(values), but it returns a PtrView. A view is a DataFrame that is a reference to the original DataFrame. So if you modify anything in the view the original DataFrame will also be modified. NOTE: There are certain operations that you cannot do with a view. For example, you cannot add/delete columns, etc. |
Ts: The list of types for all columns. A type should be specified only once. values: List of indices to copy data from |
template<typename ... Ts> ConstView get_view_by_idx(Index2D<IndexType> range) const; |
Same as above view, but it returns a const view. You can not change data in const views. But if the data is changed in the original DataFrame or through another view, it is refelcted in the const view. |
Ts: The list of types for all columns. A type should be specified only once. range: The begin and end iterators for index specified with index values |
template<typename ... Ts> ConstPtrView get_view_by_idx(const std::vector<IndexType> &values) const; |
Same as above view, but it returns a const view. You can not change data in const views. But if the data is changed in the original DataFrame or through another view, it is refelcted in the const view. |
Ts: The list of types for all columns. A type should be specified only once. values: List of indices to copy data from |
static void test_get_view_by_idx_slicing() { std::cout << "\nTesting get_view_by_idx()/slicing ..." << std::endl; StlVecType<unsigned long> idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466 }; StlVecType<double> d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; StlVecType<double> d2 = { 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 30, 31, 32, 1.89 }; StlVecType<double> d3 = { 15, 16, 17, 18, 19, 20, 21, 0.34, 1.56, 0.34, 2.3, 0.1, 0.89, 0.45 }; StlVecType<int> i1 = { 22, 23, 24, 25, 99, 100, 101, 3, 2 }; MyDataFrame df; df.load_data(std::move(idx), std::make_pair("col_1", d1), std::make_pair("col_2", d2), std::make_pair("col_3", d3), std::make_pair("col_4", i1)); typedef MyDataFrame::View MyDataFrameView; typedef MyDataFrame::ConstView MyDataFrameConstView; const MyDataFrame &const_df = df; MyDataFrame df2 = df.get_data_by_idx<double, int>(Index2D<MyDataFrame::IndexType> { 123452, 123460 }); MyDataFrameView dfv = df.get_view_by_idx<double, int>(Index2D<MyDataFrame::IndexType> { 123452, 123466 }); MyDataFrameConstView dfcv = const_df.get_view_by_idx<double, int>(Index2D<MyDataFrame::IndexType> { 123452, 123466 }); df.write<std::ostream, double, int>(std::cout); df2.write<std::ostream, double, int>(std::cout); dfv.write<std::ostream, double, int>(std::cout); dfv.get_column<double>("col_3")[0] = 88.0; assert(dfv.get_column<double>("col_3")[0] == df.get_column<double>("col_3")[2]); assert(dfv.get_column<double>("col_3")[0] == 88.0); assert(dfv.shape().first == 12); // added assert(dfcv.get_column<double>("col_3")[0] == df.get_column<double>("col_3")[2]); assert(dfcv.get_column<double>("col_3")[0] == 88.0); assert(dfcv.shape().first == 12); // added } // ----------------------------------------------------------------------------- static void test_get_data_by_idx_values() { std::cout << "\nTesting get_data_by_idx(values) ..." << std::endl; StlVecType<unsigned long> idx = { 123450, 123451, 123452, 123450, 123455, 123450, 123449 }; StlVecType<double> d1 = { 1, 2, 3, 4, 5, 6, 7 }; StlVecType<double> d2 = { 8, 9, 10, 11, 12, 13, 14 }; StlVecType<double> d3 = { 15, 16, 17, 18, 19, 20, 21 }; StlVecType<double> d4 = { 22, 23, 24, 25 }; MyDataFrame df; df.load_data(std::move(idx), std::make_pair("col_1", d1), std::make_pair("col_2", d2), std::make_pair("col_3", d3), std::make_pair("col_4", d4)); MyDataFrame df2 = df.get_data_by_idx<double>( StlVecType<MyDataFrame::IndexType> { 123452, 123455 }); MyDataFrame df3 = df.get_data_by_idx<double>( StlVecType<MyDataFrame::IndexType> { 123449, 123450 }); assert(df2.get_index().size() == 2); assert(df2.get_column<double>("col_3").size() == 2); assert(df2.get_column<double>("col_2").size() == 2); assert(df2.get_index()[0] == 123452); assert(df2.get_index()[1] == 123455); assert(df2.get_column<double>("col_3")[0] == 17.0); assert(df2.get_column<double>("col_2")[1] == 12.0); assert(std::isnan(df2.get_column<double>("col_4")[1])); assert(df3.get_index().size() == 4); assert(df3.get_column<double>("col_3").size() == 4); assert(df3.get_column<double>("col_2").size() == 4); assert(df3.get_column<double>("col_1").size() == 4); assert(df3.get_index()[0] == 123450); assert(df3.get_index()[1] == 123450); assert(df3.get_index()[2] == 123450); assert(df3.get_index()[3] == 123449); assert(df3.get_column<double>("col_1")[0] == 1.0); assert(df3.get_column<double>("col_2")[2] == 13.0); assert(df3.get_column<double>("col_4")[0] == 22.0); assert(df3.get_column<double>("col_4")[1] == 25.0); assert(std::isnan(df3.get_column<double>("col_4")[2])); assert(std::isnan(df3.get_column<double>("col_4")[3])); } // ----------------------------------------------------------------------------- static void test_get_view_by_idx_values() { std::cout << "\nTesting get_view_by_idx(values) ..." << std::endl; StlVecType<unsigned long> idx = { 123450, 123451, 123452, 123450, 123455, 123450, 123449 }; StlVecType<double> d1 = { 1, 2, 3, 4, 5, 6, 7 }; StlVecType<double> d2 = { 8, 9, 10, 11, 12, 13, 14 }; StlVecType<double> d3 = { 15, 16, 17, 18, 19, 20, 21 }; StlVecType<double> d4 = { 22, 23, 24, 25 }; MyDataFrame df; df.load_data(std::move(idx), std::make_pair("col_1", d1), std::make_pair("col_2", d2), std::make_pair("col_3", d3), std::make_pair("col_4", d4)); const MyDataFrame &const_df = df; auto dfv1 = df.get_view_by_idx<double>(StlVecType<MyDataFrame::IndexType> { 123452, 123455 }); auto const_dfv1 = const_df.get_view_by_idx<double>(StlVecType<MyDataFrame::IndexType> { 123452, 123455 }); auto dfv2 = df.get_view_by_idx<double>(StlVecType<MyDataFrame::IndexType> { 123449, 123450 }); auto const_dfv2 = const_df.get_view_by_idx<double>(StlVecType<MyDataFrame::IndexType> { 123449, 123450 }); assert(dfv1.get_index().size() == 2); assert(dfv1.get_column<double>("col_3").size() == 2); assert(dfv1.get_column<double>("col_2").size() == 2); assert(dfv1.get_index()[0] == 123452); assert(dfv1.get_index()[1] == 123455); assert(dfv1.get_column<double>("col_3")[0] == 17.0); assert(dfv1.get_column<double>("col_2")[1] == 12.0); assert(std::isnan(dfv1.get_column<double>("col_4")[1])); assert(const_dfv1.get_index().size() == 2); assert(const_dfv1.get_column<double>("col_3").size() == 2); assert(const_dfv1.get_column<double>("col_2").size() == 2); assert(const_dfv1.get_index()[0] == 123452); assert(const_dfv1.get_index()[1] == 123455); assert(const_dfv1.get_column<double>("col_3")[0] == 17.0); assert(const_dfv1.get_column<double>("col_2")[1] == 12.0); assert(std::isnan(const_dfv1.get_column<double>("col_4")[1])); assert(dfv2.get_index().size() == 4); assert(dfv2.get_column<double>("col_3").size() == 4); assert(dfv2.get_column<double>("col_2").size() == 4); assert(dfv2.get_column<double>("col_1").size() == 4); assert(dfv2.get_index()[0] == 123450); assert(dfv2.get_index()[1] == 123450); assert(dfv2.get_index()[2] == 123450); assert(dfv2.get_index()[3] == 123449); assert(dfv2.get_column<double>("col_1")[0] == 1.0); assert(dfv2.get_column<double>("col_2")[2] == 13.0); assert(dfv2.get_column<double>("col_4")[0] == 22.0); assert(dfv2.get_column<double>("col_4")[1] == 25.0); assert(std::isnan(dfv2.get_column<double>("col_4")[2])); assert(std::isnan(dfv2.get_column<double>("col_4")[3])); assert(const_dfv2.get_index().size() == 4); assert(const_dfv2.get_column<double>("col_3").size() == 4); assert(const_dfv2.get_column<double>("col_2").size() == 4); assert(const_dfv2.get_column<double>("col_1").size() == 4); assert(const_dfv2.get_index()[0] == 123450); assert(const_dfv2.get_index()[1] == 123450); assert(const_dfv2.get_index()[2] == 123450); assert(const_dfv2.get_index()[3] == 123449); assert(const_dfv2.get_column<double>("col_1")[0] == 1.0); assert(const_dfv2.get_column<double>("col_2")[2] == 13.0); assert(const_dfv2.get_column<double>("col_4")[0] == 22.0); assert(const_dfv2.get_column<double>("col_4")[1] == 25.0); assert(std::isnan(const_dfv2.get_column<double>("col_4")[2])); assert(std::isnan(const_dfv2.get_column<double>("col_4")[3])); dfv2.get_column<double>("col_1")[0] = 101.0; assert(dfv2.get_column<double>("col_1")[0] == 101.0); assert(df.get_column<double>("col_1")[0] == 101.0); }