Back to Documentations

Signature Description Parameters
template<typename ... Ts>
void
remove_data_by_loc(Index2D<long> range,
                   inclusiveness incld = inclusiveness::begin);
It removes the data rows from location begin to location end within range.
This function supports Python-like negative indexing. That is why the range type is long.
This function first calls make_consistent() that may add nan values to data columns.
Ts: The list of types for all columns. A type should be specified only once.
range: The begin and end iterators for data
incld: How to include beginning and end of the range
static void test_remove_data_by_loc()  {

    std::cout << "\nTesting remove_data_by_loc() ..." << 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 };
    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));

    {
        df.write<std::ostream, double, int>(std::cout);
        std::cout << "After removing by ibdex { 3, -3 }" << std::endl;

        auto    df2 = df;

        df2.remove_data_by_loc<double, int>({ 3, -3 });
        df2.write<std::ostream, double, int>(std::cout);
    }

    {
        auto    df2 = df;

        df2.remove_data_by_loc<double, int>({ 3, 10 });
        assert(df2.get_index().size() == 7);
        assert(df2.get_index()[0] == 123450);
        assert(df2.get_index()[3] == 123460);
        assert(df2.get_index()[6] == 123466);
        assert(df2.get_column<double>("col_3").size() == 7);
        assert(df2.get_column<double>("col_3")[0] == 15.0);
        assert(df2.get_column<double>("col_3")[6] == 0.45);
        assert(df2.get_column<int>("col_4").size() == 7);
        assert(df2.get_column<int>("col_4")[0] == 22);
        assert(df2.get_column<int>("col_4")[6] == 0);
    }

    {
        auto    df2 = df;

        df2.remove_data_by_loc<double, int>({ 3, 10 }, inclusiveness::end);
        assert(df2.get_index().size() == 7);
        assert(df2.get_index()[0] == 123450);
        assert(df2.get_index()[3] == 123453);
        assert(df2.get_index()[6] == 123466);
        assert(df2.get_column<double>("col_3").size() == 7);
        assert(df2.get_column<double>("col_3")[0] == 15.0);
        assert(df2.get_column<double>("col_3")[6] == 0.45);
        assert(df2.get_column<int>("col_4").size() == 7);
        assert(df2.get_column<int>("col_4")[0] == 22);
        assert(df2.get_column<int>("col_4")[6] == 0);
    }

    {
        auto    df2 = df;

        df2.remove_data_by_loc<double, int>({ 3, 10 }, inclusiveness::both);
        assert(df2.get_index().size() == 6);
        assert(df2.get_index()[0] == 123450);
        assert(df2.get_index()[3] == 123461);
        assert(df2.get_index()[5] == 123466);
        assert(df2.get_column<double>("col_3").size() == 6);
        assert(df2.get_column<double>("col_3")[0] == 15.0);
        assert(df2.get_column<double>("col_3")[5] == 0.45);
        assert(df2.get_column<int>("col_4").size() == 6);
        assert(df2.get_column<int>("col_4")[0] == 22);
        assert(df2.get_column<int>("col_4")[5] == 0);
    }

    {
        auto    df2 = df;

        df2.remove_data_by_loc<double, int>({ 3, 10 }, inclusiveness::neither);
        assert(df2.get_index().size() == 8);
        assert(df2.get_index()[0] == 123450);
        assert(df2.get_index()[3] == 123453);
        assert(df2.get_index()[7] == 123466);
        assert(df2.get_column<double>("col_3").size() == 8);
        assert(df2.get_column<double>("col_3")[0] == 15.0);
        assert(df2.get_column<double>("col_3")[7] == 0.45);
        assert(df2.get_column<int>("col_4").size() == 8);
        assert(df2.get_column<int>("col_4")[0] == 22);
        assert(df2.get_column<int>("col_4")[7] == 0);
    }
}

C++ DataFrame