示例#1
0
def test_head():
    df = rc.DataFrame({1: [0, 1, 2], 2: [3, 4, 5]}, columns=[1, 2], sort=False)

    assert_frame_equal(df.head(0), rc.DataFrame(columns=[1, 2], sort=False))
    assert_frame_equal(df.head(1), rc.DataFrame({1: [0], 2: [3]}, columns=[1, 2], sort=False))
    assert_frame_equal(df.head(2), rc.DataFrame({1: [0, 1], 2: [3, 4]}, columns=[1, 2], sort=False))
    assert_frame_equal(df.head(3), rc.DataFrame({1: [0, 1, 2], 2: [3, 4, 5]}, columns=[1, 2], sort=False))
    assert_frame_equal(df.head(999), rc.DataFrame({1: [0, 1, 2], 2: [3, 4, 5]}, columns=[1, 2], sort=False))
示例#2
0
def test_tail():
    df = rc.DataFrame({1: [0, 1, 2], 2: [3, 4, 5]}, columns=[1, 2], sort=False)

    assert_frame_equal(df.tail(0), rc.DataFrame(columns=[1, 2], sort=False))
    assert_frame_equal(df.tail(1), rc.DataFrame({1: [2], 2: [5]}, columns=[1, 2], index=[2], sort=False))
    assert_frame_equal(df.tail(2), rc.DataFrame({1: [1, 2], 2: [4, 5]}, columns=[1, 2], index=[1, 2], sort=False))
    assert_frame_equal(df.tail(3), rc.DataFrame({1: [0, 1, 2], 2: [3, 4, 5]}, columns=[1, 2], sort=False))
    assert_frame_equal(df.tail(999), rc.DataFrame({1: [0, 1, 2], 2: [3, 4, 5]}, columns=[1, 2], sort=False))
示例#3
0
def test_set_locations():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8])

    df.set_locations([0, 2], 'a', [-1, -3])
    assert_frame_equal(df, rc.DataFrame({'a': [-1, 2, -3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8]))

    df.set_locations([1, 3], 'a', -10)
    assert_frame_equal(df, rc.DataFrame({'a': [-1, -10, -3, -10], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8]))
示例#4
0
def test_delete_all_rows():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=['a', 'b', 'c'], columns=['b', 'a'])

    link_col = df.get_entire_column('b', as_list=True)
    link_index = df.index

    df.delete_all_rows()
    assert_frame_equal(df, rc.DataFrame(columns=['b', 'a'], sort=False))

    assert link_col == []
    assert link_index == []
示例#5
0
def test_get_location():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8])

    # forward indexing, all columns
    assert_frame_equal(df.get_location(2), rc.DataFrame({'a': [3], 'b': [7]}, index=[6]))
    assert df.get_location(2, as_dict=True) == {'index': 6, 'a': 3, 'b': 7}
    assert df.get_location(2, as_dict=True, index=False) == {'a': 3, 'b': 7}

    # reverse indexing, all columns
    assert_frame_equal(df.get_location(-1), rc.DataFrame({'a': [4], 'b': [8]}, index=[8]))
    assert df.get_location(-1, as_dict=True) == {'index': 8, 'a': 4, 'b': 8}
    assert df.get_location(-1, as_dict=True, index=False) == {'a': 4, 'b': 8}

    # forward indexing, one column
    assert_frame_equal(df.get_location(0, ['a']), rc.DataFrame({'a': [1]}, index=[2]))
    assert df.get_location(0, ['a'], as_dict=True) == {'index': 2, 'a': 1}
    assert df.get_location(0, ['a'], as_dict=True, index=False) == {'a': 1}

    # reverse indexing, all columns
    assert_frame_equal(df.get_location(-2, ['b']), rc.DataFrame({'b': [7]}, index=[6]))
    assert df.get_location(-2, ['b'], as_dict=True) == {'index': 6, 'b': 7}
    assert df.get_location(-2, ['b'], as_dict=True, index=False) == {'b': 7}

    # single value for column and not list returns just the value
    assert df.get_location(1, 'b') == 6
示例#6
0
def test_get_slicer():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9], 'd': [10, 11, 12]}, columns=['a', 'b', 'c', 'd'],
                      sort=False)

    # df[1:2] -- get slice from index 1 to 2, all columns
    assert_frame_equal(df[1:2],
                       rc.DataFrame({'a': [2, 3], 'b': [5, 6], 'c': [8, 9], 'd': [11, 12]},
                                    columns=['a', 'b', 'c', 'd'], index=[1, 2], sort=False))

    # df[0:1, ['c', 'd']] -- get slice from index 0 to 1, columns ['c', 'd']
    assert_frame_equal(df[0:1, ['c', 'd']], rc.DataFrame({'c': [7, 8], 'd': [10, 11]},
                                                         columns=['c', 'd'], index=[0, 1], sort=False))

    assert_frame_equal(df[0:1, ['d', 'c']], rc.DataFrame({'d': [10, 11], 'c': [7, 8]},
                                                         columns=['d', 'c'], index=[0, 1], sort=False))

    # df[1:1, 'c'] -- get slice 1 to 1 and column 'c'
    assert_frame_equal(df[1:1, 'c'], rc.DataFrame({'c': [8]}, index=[1], sort=False))

    # test indexes not in the range
    with pytest.raises(IndexError):
        x = df[4:5, 'c']

    with pytest.raises(IndexError):
        x = df[0:8, 'c']

    with pytest.raises(IndexError):
        x = df[2:1, 'c']
示例#7
0
def test_delete_row():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=['a', 'b', 'c'], columns=['b', 'a'])

    df.delete_rows(['a', 'c'])
    assert_frame_equal(df, rc.DataFrame({'a': [2], 'b': [5]}, columns=['b', 'a'], index=['b']))

    df.delete_rows('b')
    assert_frame_equal(df, rc.DataFrame(columns=['b', 'a'], sort=False))

    # insert back in data
    df[1, 'a'] = 9
    assert df.data == [[None], [9]]

    df[2, 'b'] = 8
    assert df.data == [[None, 8], [9, None]]

    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=['a', 'b', 'c'], columns=['b', 'a'])
    # cannot delete values not in index
    with pytest.raises(ValueError):
        df.delete_rows(['bad'])

    # length of boolean must be len of index
    with pytest.raises(ValueError):
        df.delete_rows([True, False])

    df.delete_rows([True, False, True])
    assert_frame_equal(df, rc.DataFrame({'a': [2], 'b': [5]}, columns=['b', 'a'], index=['b']))

    df.delete_rows([True])
    assert_frame_equal(df, rc.DataFrame(columns=['b', 'a'], sort=False))
def test_assert_frame_equal():
    df1 = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                       columns=['a', 'b'],
                       index=[1, 2, 3])
    df2 = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                       columns=['a', 'b'],
                       index=[1, 2, 3],
                       dropin=blist)
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)
示例#9
0
def test_delete_all_rows():
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      index=['a', 'b', 'c'],
                      columns=['b', 'a'])

    link_col = df.get_entire_column('b', as_list=True)
    link_index = df.index

    df.delete_all_rows()
    assert_frame_equal(df, rc.DataFrame(columns=['b', 'a'], sort=False))

    assert link_col == []
    assert link_index == []
示例#10
0
def test_delete_row_sorted():
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      index=['a', 'b', 'c'],
                      columns=['b', 'a'],
                      sort=True,
                      use_blist=False)

    df.delete_rows(['a', 'c'])
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [2],
            'b': [5]
        },
                     columns=['b', 'a'],
                     index=['b'],
                     sort=True,
                     use_blist=False))

    df.delete_rows('b')
    assert_frame_equal(
        df, rc.DataFrame(columns=['b', 'a'], sort=True, use_blist=False))

    # insert back in data
    df[1, 'a'] = 9
    assert df.data == [[None], [9]]

    df[2, 'b'] = 8
    assert df.data == [[None, 8], [9, None]]

    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      index=['a', 'b', 'c'],
                      columns=['b', 'a'])
    # cannot delete values not in index
    with pytest.raises(ValueError):
        df.delete_rows(['bad'])

    # length of boolean must be len of index
    with pytest.raises(ValueError):
        df.delete_rows([True, False])

    df.delete_rows([True, False, True])
    assert_frame_equal(
        df, rc.DataFrame({
            'a': [2],
            'b': [5]
        }, columns=['b', 'a'], index=['b']))

    df.delete_rows([True])
    assert_frame_equal(df, rc.DataFrame(columns=['b', 'a'], sort=False))
示例#11
0
def test_assert_frame_equal():
    df1 = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                       columns=['a', 'b'],
                       index=[1, 2, 3])
    assert_frame_equal(df1, df1)

    df2 = rc.DataFrame({
        'a': [1, 1, 1],
        'b': [4, 5, 6]
    },
                       columns=['a', 'b'],
                       index=[1, 2, 3])
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                       columns=['b', 'a'],
                       index=[1, 2, 3])
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                       columns=['a', 'b'],
                       index=[11, 12, 13])
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                       columns=['a', 'b'],
                       index=[1, 2, 3],
                       sort=True)
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)
示例#12
0
def test_append_row():
    actual = rc.DataFrame({'a': [1, 3], 'b': [4, 6], 'c': [7, 9]}, index=[10, 12], columns=['a', 'b', 'c'])

    # append row with new columns, ignore new columns
    actual.append_row(14, {'a': 10, 'c': 13, 'd': 99}, new_cols=False)
    expected = rc.DataFrame({'a': [1, 3, 10], 'b': [4, 6, None], 'c': [7, 9, 13]}, index=[10, 12, 14],
                            columns=['a', 'b', 'c'])
    assert_frame_equal(actual, expected)

    # append row with new columns, add new columns
    actual.append_row(16, {'a': 14, 'b': 15, 'd': 100})
    expected = rc.DataFrame({'a': [1, 3, 10, 14], 'b': [4, 6, None, 15], 'c': [7, 9, 13, None],
                             'd': [None, None, None, 100]}, index=[10, 12, 14, 16], columns=['a', 'b', 'c', 'd'])
    assert_frame_equal(actual, expected)

    # try to append existing row
    with pytest.raises(IndexError):
        actual.append_row(10, {'a': 9})
示例#13
0
def test_get_locations():
    df = rc.DataFrame({
        'a': [1, 2, 3, 4],
        'b': [5, 6, 7, 8]
    },
                      index=[2, 4, 6, 8])

    # multi row, multi columns
    assert_frame_equal(df.get_locations([0, 2]),
                       rc.DataFrame({
                           'a': [1, 3],
                           'b': [5, 7]
                       }, index=[2, 6]))

    # multiple rows, single columns
    assert_frame_equal(df.get_locations([1, 3], 'a'),
                       rc.DataFrame({'a': [2, 4]}, index=[4, 8]))
    assert df.get_locations([0, 2], 'b', as_list=True) == [5, 7]

    # single row, multiple columns
    assert_frame_equal(df.get_locations([2]),
                       rc.DataFrame({
                           'a': [3],
                           'b': [7]
                       }, index=[6]))
示例#14
0
def test_tail():
    df = rc.DataFrame({
        1: [0, 1, 2],
        2: [3, 4, 5]
    },
                      columns=[1, 2],
                      sorted=False)

    assert_frame_equal(df.tail(0), rc.DataFrame(columns=[1, 2], sorted=False))
    assert_frame_equal(
        df.tail(1),
        rc.DataFrame({
            1: [2],
            2: [5]
        }, columns=[1, 2], index=[2], sorted=False))
    assert_frame_equal(
        df.tail(2),
        rc.DataFrame({
            1: [1, 2],
            2: [4, 5]
        },
                     columns=[1, 2],
                     index=[1, 2],
                     sorted=False))
    assert_frame_equal(
        df.tail(3),
        rc.DataFrame({
            1: [0, 1, 2],
            2: [3, 4, 5]
        },
                     columns=[1, 2],
                     sorted=False))
    assert_frame_equal(
        df.tail(999),
        rc.DataFrame({
            1: [0, 1, 2],
            2: [3, 4, 5]
        },
                     columns=[1, 2],
                     sorted=False))
示例#15
0
def test_data_function():
    # Example function for testing
    def assert_approx_equal(left_data, right_data, precision=0.00001):
        for i in range(len(left_data)):
            for j in range(len(left_data[i])):
                assert abs(left_data[i][j] - right_data[i][j]) <= precision

    df1 = rc.DataFrame({
        'a': [1.0, 3.0],
        'b': [4.0, 6.0]
    },
                       columns=['a', 'b'],
                       index=[1, 3])
    df2 = rc.DataFrame({
        'a': [1.0, 3.001],
        'b': [4.0, 6.001]
    },
                       columns=['a', 'b'],
                       index=[1, 3])

    # confirm fails with standard compare
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    # passes with function and proper parameters
    assert_frame_equal(df1, df2, assert_approx_equal, {'precision': 0.01})

    # fails with function and precision parameter to low
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2, assert_approx_equal,
                           {'precision': 0.00001})
示例#16
0
def test_get_rows_sorted():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [4, 5, 6, 7], 'c': [7, 8, 9, None]}, index=[10, 11, 12, 99],
                      columns=['a', 'b', 'c'], index_name='start_10', sort=True)

    expected = rc.DataFrame({'c': [8, 9]}, index=[11, 12], index_name='start_10', sort=True)
    actual = df.get([11, 12], 'c')
    assert_frame_equal(actual, expected)

    # get as a list
    assert df.get([11, 12], 'c', as_list=True) == [8, 9]

    # test with boolean list
    actual = df.get([False, True, True, False], 'c')
    assert_frame_equal(actual, expected)

    # index out of order
    expected = rc.DataFrame({'c': [7, None]}, index=[10, 99], index_name='start_10', sort=True)
    actual = df.get([99, 10], 'c')
    assert_frame_equal(actual, expected)

    # get as a list
    assert df.get([False, True, True, False], 'c', as_list=True) == [8, 9]

    # get entire column
    assert df.get(columns='b', as_list=True) == [4, 5, 6, 7]

    # items not in index raise errors
    with pytest.raises(ValueError):
        df.get([11, 88], 'c', as_list=True)

    # not enough items in boolean list
    with pytest.raises(ValueError):
        df.get([True, True], 'c')
示例#17
0
def test_get_columns_sorted():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [4, 5, 6, 7], 'c': [7, 8, 9, None]}, index=[10, 11, 12, 99],
                      columns=['a', 'b', 'c'], index_name='start_10', sort=True)

    expected = rc.DataFrame({'a': [4], 'c': [None]}, index=[99], columns=['a', 'c'], index_name='start_10',
                            sort=True)
    actual = df.get(99, ['a', 'c'])
    assert_frame_equal(actual, expected)

    # test with boolean list
    actual = df.get(99, [True, False, True])
    assert_frame_equal(actual, expected)

    # columns out of order
    expected = rc.DataFrame({'c': [8], 'b': [5]}, index=[11], columns=['c', 'b'], index_name='start_10',
                            sort=True)
    actual = df.get(11, ['c', 'b'])
    assert_frame_equal(actual, expected)

    # test boolean list not same length as columns
    with pytest.raises(ValueError):
        df.get(99, [True, False])

    # test index out of bounds
    with pytest.raises(ValueError):
        df.get(88, ['a', 'c'])
示例#18
0
def test_sort_index():
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      columns=['a', 'b'],
                      index=[10, 8, 9],
                      sort=False,
                      dropin=blist)

    df.sort_index()
    assert isinstance(df.index, blist)
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [2, 3, 1],
            'b': [5, 6, 4]
        },
                     columns=['a', 'b'],
                     index=[8, 9, 10],
                     sort=False,
                     dropin=blist))
示例#19
0
def test_append_rows():
    actual = rc.DataFrame({'a': [1, 3], 'b': [4, 6], 'c': [7, 9]}, index=[10, 12], columns=['a', 'b', 'c'])

    # append rows with new columns, ignore new columns
    actual.append_rows([14, 15], {'a': [10, 11], 'c': [13, 14], 'd': [99, 100]}, new_cols=False)
    expected = rc.DataFrame({'a': [1, 3, 10, 11], 'b': [4, 6, None, None], 'c': [7, 9, 13, 14]},
                            index=[10, 12, 14, 15], columns=['a', 'b', 'c'])
    assert_frame_equal(actual, expected)

    # append row with new columns, add new columns
    actual.append_rows([16, 17], {'a': [14, 15], 'b': [15, 16], 'd': [100, 101]})
    expected = rc.DataFrame({'a': [1, 3, 10, 11, 14, 15], 'b': [4, 6, None, None, 15, 16],
                             'c': [7, 9, 13, 14, None, None], 'd': [None, None, None, None, 100, 101]},
                            index=[10, 12, 14, 15, 16, 17], columns=['a', 'b', 'c', 'd'])
    assert_frame_equal(actual, expected)

    # try to append existing row
    with pytest.raises(IndexError):
        actual.append_rows([10, 11], {'a': [8, 9]})

    with pytest.raises(ValueError):
        actual.append_rows([16, 17], {'a': [14, 15, 999]})
示例#20
0
def test_head():
    df = rc.DataFrame({
        1: [0, 1, 2],
        2: [3, 4, 5]
    },
                      columns=[1, 2],
                      sorted=False)

    assert_frame_equal(df.head(0), rc.DataFrame(columns=[1, 2], sorted=False))
    assert_frame_equal(
        df.head(1), rc.DataFrame({
            1: [0],
            2: [3]
        },
                                 columns=[1, 2],
                                 sorted=False))
    assert_frame_equal(
        df.head(2),
        rc.DataFrame({
            1: [0, 1],
            2: [3, 4]
        }, columns=[1, 2], sorted=False))
    assert_frame_equal(
        df.head(3),
        rc.DataFrame({
            1: [0, 1, 2],
            2: [3, 4, 5]
        },
                     columns=[1, 2],
                     sorted=False))
    assert_frame_equal(
        df.head(999),
        rc.DataFrame({
            1: [0, 1, 2],
            2: [3, 4, 5]
        },
                     columns=[1, 2],
                     sorted=False))
示例#21
0
def test_sort_index():
    # test on list
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[10, 8, 9], sorted=False)

    df.sort_index()
    assert isinstance(df.index, list)
    assert_frame_equal(df, rc.DataFrame({'a': [2, 3, 1], 'b': [5, 6, 4]}, columns=['a', 'b'], index=[8, 9, 10],
                                        sorted=False))

    # test on blist
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[10, 8, 9], sorted=False,
                      use_blist=True)

    df.sort_index()
    assert isinstance(df.index, blist)
    assert_frame_equal(df, rc.DataFrame({'a': [2, 3, 1], 'b': [5, 6, 4]}, columns=['a', 'b'], index=[8, 9, 10],
                                        sorted=False, use_blist=True))

    # fails on mixed type columns
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[10, 'a', 9])
    if PYTHON3:
        with pytest.raises(TypeError):
            df.sort_index()
示例#22
0
def test_delete_columns():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, columns=['a', 'b', 'c'])

    # cannot delete bad column
    with pytest.raises(ValueError):
        df.delete_columns(['bad', 'a'])

    df.delete_columns(['a', 'c'])
    assert_frame_equal(df, rc.DataFrame({'b': [4, 5, 6]}))
    assert df.index == [0, 1, 2]

    # insert some data back in
    df[1, 'a'] = 77
    assert df.data == [[4, 5, 6], [None, 77, None]]

    df.delete_columns(['b', 'a'])
    assert_frame_equal(df, rc.DataFrame())
    assert df.columns == []
    assert df.index == []

    # insert some data back in, fresh columns and index
    df[1, 'e'] = 77
    assert df.data == [[77]]
示例#23
0
def test_json_multi_index():
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6],
        'c': [7, 8, 9]
    },
                      index=[('a', 4), ('b', 5), ('c', 6)])

    str = df.to_json()
    actual = rc.from_json(str)
    assert_frame_equal(df, actual)

    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6],
        'c': [7, 8, 9]
    },
                      index=[('a', 4), ('b', 5), ('c', 6)],
                      index_name=('first', 'second'))

    str = df.to_json()
    actual = rc.from_json(str)
    assert_frame_equal(df, actual)
示例#24
0
def test_append_row():
    actual = rc.DataFrame({
        'a': [1, 3],
        'b': [4, 6],
        'c': [7, 9]
    },
                          index=[10, 12],
                          columns=['a', 'b', 'c'])

    # append row with new columns, ignore new columns
    actual.append_row(14, {'a': 10, 'c': 13, 'd': 99}, new_cols=False)
    expected = rc.DataFrame(
        {
            'a': [1, 3, 10],
            'b': [4, 6, None],
            'c': [7, 9, 13]
        },
        index=[10, 12, 14],
        columns=['a', 'b', 'c'])
    assert_frame_equal(actual, expected)

    # append row with new columns, add new columns
    actual.append_row(16, {'a': 14, 'b': 15, 'd': 100})
    expected = rc.DataFrame(
        {
            'a': [1, 3, 10, 14],
            'b': [4, 6, None, 15],
            'c': [7, 9, 13, None],
            'd': [None, None, None, 100]
        },
        index=[10, 12, 14, 16],
        columns=['a', 'b', 'c', 'd'])
    assert_frame_equal(actual, expected)

    # try to append existing row
    with pytest.raises(IndexError):
        actual.append_row(10, {'a': 9})
示例#25
0
def test_json():
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6],
        'c': [7, 8, 9]
    },
                      sort=False,
                      dropin=blist)

    string = df.to_json()
    actual = rc.DataFrame.from_json(string, blist)
    assert_frame_equal(df, actual)

    # fails with no dropin supplied
    with pytest.raises(AttributeError) as e:
        rc.DataFrame.from_json(string)
        assert e == "AttributeError: the JSON has a dropin : <class 'blist.blist'> : " \
                    "but the dropin parameter was not supplied"

    # fails with the wrong dropin supplied
    with pytest.raises(AttributeError) as e:
        rc.DataFrame.from_json(string, list)
        assert e == "AttributeError: the supplied dropin parameter: <class 'list'> : does not match the value" \
                    " in the JSON: <class 'blist.blist'>"
示例#26
0
def test_set_locations():
    df = rc.DataFrame({
        'a': [1, 2, 3, 4],
        'b': [5, 6, 7, 8]
    },
                      index=[2, 4, 6, 8])

    df.set_locations([0, 2], 'a', [-1, -3])
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [-1, 2, -3, 4],
            'b': [5, 6, 7, 8]
        },
                     index=[2, 4, 6, 8]))

    df.set_locations([1, 3], 'a', -10)
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [-1, -10, -3, -10],
            'b': [5, 6, 7, 8]
        },
                     index=[2, 4, 6, 8]))
示例#27
0
def test_get_locations():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8])

    # multi row, multi columns
    assert_frame_equal(df.get_locations([0, 2]), rc.DataFrame({'a': [1, 3], 'b': [5, 7]}, index=[2, 6]))

    # multiple rows, single columns
    assert_frame_equal(df.get_locations([1, 3], 'a'), rc.DataFrame({'a': [2, 4]}, index=[4, 8]))
    assert df.get_locations([0, 2], 'b', as_list=True) == [5, 7]

    # single row, multiple columns
    assert_frame_equal(df.get_locations([2]), rc.DataFrame({'a': [3], 'b': [7]}, index=[6]))
示例#28
0
def test_get_location():
    df = rc.DataFrame({
        'a': [1, 2, 3, 4],
        'b': [5, 6, 7, 8]
    },
                      index=[2, 4, 6, 8])

    # forward indexing, all columns
    assert_frame_equal(df.get_location(2),
                       rc.DataFrame({
                           'a': [3],
                           'b': [7]
                       }, index=[6]))
    assert df.get_location(2, as_dict=True) == {'index': 6, 'a': 3, 'b': 7}
    assert df.get_location(2, as_dict=True, index=False) == {'a': 3, 'b': 7}

    # reverse indexing, all columns
    assert_frame_equal(df.get_location(-1),
                       rc.DataFrame({
                           'a': [4],
                           'b': [8]
                       }, index=[8]))
    assert df.get_location(-1, as_dict=True) == {'index': 8, 'a': 4, 'b': 8}
    assert df.get_location(-1, as_dict=True, index=False) == {'a': 4, 'b': 8}

    # forward indexing, one column
    assert_frame_equal(df.get_location(0, ['a']),
                       rc.DataFrame({'a': [1]}, index=[2]))
    assert df.get_location(0, ['a'], as_dict=True) == {'index': 2, 'a': 1}
    assert df.get_location(0, ['a'], as_dict=True, index=False) == {'a': 1}

    # reverse indexing, all columns
    assert_frame_equal(df.get_location(-2, ['b']),
                       rc.DataFrame({'b': [7]}, index=[6]))
    assert df.get_location(-2, ['b'], as_dict=True) == {'index': 6, 'b': 7}
    assert df.get_location(-2, ['b'], as_dict=True, index=False) == {'b': 7}

    # single value for column and not list returns just the value
    assert df.get_location(1, 'b') == 6
示例#29
0
def test_set_location():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8])

    df.set_location(2, {'a': -3})
    assert_frame_equal(df, rc.DataFrame({'a': [1, 2, -3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8]))

    df.set_location(3, {'a': -10, 'b': -88})
    assert_frame_equal(df, rc.DataFrame({'a': [1, 2, -3, -10], 'b': [5, 6, 7, -88]}, index=[2, 4, 6, 8]))

    df.set_location(0, {'b': -55}, missing_to_none=True)
    assert_frame_equal(df, rc.DataFrame({'a': [None, 2, -3, -10], 'b': [-55, 6, 7, -88]}, index=[2, 4, 6, 8]))

    # location out of bounds
    with pytest.raises(IndexError):
        df.set_location(99, {'a': 10})
示例#30
0
def test_get_matrix_sorted():
    df = rc.DataFrame({'a': [2, 1, 3], 'b': [5, 4, 6], 'c': [8, 7, 9], 'd': [11, 10, 12]}, index=['y', 'x', 'z'],
                      columns=['a', 'b', 'c', 'd'], index_name='letters', sort=True)

    expected = rc.DataFrame({'b': [4, 6], 'd': [10, 12]}, index=['x', 'z'], columns=['b', 'd'], index_name='letters',
                            sort=True)
    actual = df.get(['x', 'z'], ['b', 'd'])
    assert_frame_equal(actual, expected)

    # test with booleans
    actual = df.get([True, False, True], [False, True, False, True])
    assert_frame_equal(actual, expected)

    # columns out of order
    expected = rc.DataFrame({'d': [10, 12], 'c': [7, 9]}, index=['x', 'z'], columns=['d', 'c'], index_name='letters',
                            sort=True)
    actual = df.get(['x', 'z'], ['d', 'c'])
    assert_frame_equal(actual, expected)

    # get everything
    everything = df.get()
    assert_frame_equal(everything, df)

    # boolean list does not match index length
    with pytest.raises(ValueError):
        df.get([True, False], [False, True, False, True])

    # boolean list does not match columns length
    with pytest.raises(ValueError):
        df.get([True, False, True], [False, True])

    # missing index
    with pytest.raises(ValueError):
        df.get_matrix(['BAD', 'x'], ['a', 'b'])

    # missing column
    with pytest.raises(ValueError):
        df.get_matrix(['x', 'y'], ['a', 'b', 'BAD'])
示例#31
0
def test_json():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, index=[4, 5, 6], columns=['b', 'a', 'c'])

    str = df.to_json()
    actual = rc.DataFrame.from_json(str)
    assert_frame_equal(df, actual)

    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, use_blist=True, sort=False)

    str = df.to_json()
    actual = rc.DataFrame.from_json(str)
    assert_frame_equal(df, actual)

    # empty DataFrame
    df = rc.DataFrame({'a': [], 'b': [], 'c': []})
    str = df.to_json()
    actual = rc.DataFrame.from_json(str)
    assert_frame_equal(df, actual)

    df = rc.DataFrame()
    str = df.to_json()
    actual = rc.DataFrame.from_json(str)
    assert_frame_equal(df, actual)
示例#32
0
def test_append():
    # duplicate indexes
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[0, 1, 2])
    df2 = rc.DataFrame({'a': [11, 12, 13], 'b': [14, 15, 16]}, columns=['a', 'b'], index=[2, 3, 4])
    with pytest.raises(ValueError):
        df.append(df2)

    # all same columns
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[0, 1, 2])
    df2 = rc.DataFrame({'a': [11, 12, 13], 'b': [14, 15, 16]}, columns=['a', 'b'], index=[3, 4, 5])
    df.append(df2)
    assert_frame_equal(df, rc.DataFrame({'a': [1, 2, 3, 11, 12, 13], 'b': [4, 5, 6, 14, 15, 16]},
                                        columns=['a', 'b'], index=[0, 1, 2, 3, 4, 5]))

    # all new columns
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[0, 1, 2])
    df2 = rc.DataFrame({'x': [11, 12, 13], 'y': [14, 15, 16]}, columns=['x', 'y'], index=[3, 4, 5])
    df.append(df2)
    assert_frame_equal(df, rc.DataFrame({'a': [1, 2, 3, None, None, None], 'b': [4, 5, 6, None, None, None],
                                         'x': [None, None, None, 11, 12, 13], 'y': [None, None, None, 14, 15, 16]},
                                        columns=['a', 'b', 'x', 'y'], index=[0, 1, 2, 3, 4, 5]))

    # some same, some new columns
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[0, 1, 2])
    df2 = rc.DataFrame({'b': [11, 12, 13], 'y': [14, 15, 16]}, columns=['b', 'y'], index=[3, 4, 5])
    df.append(df2)
    assert_frame_equal(df, rc.DataFrame({'a': [1, 2, 3, None, None, None], 'b': [4, 5, 6, 11, 12, 13],
                                         'y': [None, None, None, 14, 15, 16]},
                                        columns=['a', 'b', 'y'], index=[0, 1, 2, 3, 4, 5]))

    # append empty DataFrame
    df2 = rc.DataFrame(columns=['a', 'b', 'y'])
    df.append(df2)
    assert_frame_equal(df, rc.DataFrame({'a': [1, 2, 3, None, None, None], 'b': [4, 5, 6, 11, 12, 13],
                                         'y': [None, None, None, 14, 15, 16]},
                                        columns=['a', 'b', 'y'], index=[0, 1, 2, 3, 4, 5]))
示例#33
0
def test_sort_column_w_key():
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd']}, columns=['a', 'b'], index=[8, 9, 10, 11])

    # No key, reverse
    df.sort_columns('a', key=None, reverse=True)
    assert_frame_equal(df, rc.DataFrame({'a': [4, 3, 2, 1], 'b': ['d', 'c', 'b', 'a']}, columns=['a', 'b'],
                                        index=[11, 10, 9, 8]))

    # a key function that turns evens into a odds into a negative number
    def even_to_neg(i): return i * -1 if i % 2 == 0 else i
    df.sort_columns('a', key=even_to_neg)
    assert_frame_equal(df, rc.DataFrame({'a': [4, 2, 1, 3], 'b': ['d', 'b', 'a', 'c']}, columns=['a', 'b'],
                                        index=[11, 9, 8, 10]))

    # with key and reverse
    df.sort_columns('a', key=even_to_neg, reverse=True)
    assert_frame_equal(df, rc.DataFrame({'a': [3, 1, 2, 4], 'b': ['c', 'a', 'b', 'd']}, columns=['a', 'b'],
                                        index=[10, 8, 9, 11]))
示例#34
0
def test_sort_column():
    df = rc.DataFrame({
        'a': [2, 1, 3],
        'b': ['a', 'c', 'b']
    },
                      columns=['a', 'b'],
                      index=[10, 8, 9])

    # cannot sort multiple columns
    with pytest.raises(TypeError):
        df.sort_columns(['a', 'b'])

    df.sort_columns('a')
    assert isinstance(df.index, list)
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [1, 2, 3],
            'b': ['c', 'a', 'b']
        },
                     columns=['a', 'b'],
                     index=[8, 10, 9]))

    df.sort_columns('b')
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [2, 3, 1],
            'b': ['a', 'b', 'c']
        },
                     columns=['a', 'b'],
                     index=[10, 9, 8]))

    df.sort_columns('b', reverse=True)
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [1, 3, 2],
            'b': ['c', 'b', 'a']
        },
                     columns=['a', 'b'],
                     index=[8, 9, 10]))
示例#35
0
def test_data_function():
    # Example function for testing
    def assert_approx_equal(left_data, right_data, precision=0.00001):
        for i in range(len(left_data)):
            for j in range(len(left_data[i])):
                assert abs(left_data[i][j]- right_data[i][j]) <= precision

    df1 = rc.DataFrame({'a': [1.0, 3.0], 'b': [4.0, 6.0]}, columns=['a', 'b'], index=[1, 3])
    df2 = rc.DataFrame({'a': [1.0, 3.001], 'b': [4.0, 6.001]}, columns=['a', 'b'], index=[1, 3])

    # confirm fails with standard compare
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    # passes with function and proper parameters
    assert_frame_equal(df1, df2, assert_approx_equal, {'precision': 0.01})

    # fails with function and precision parameter to low
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2, assert_approx_equal, {'precision': 0.00001})
示例#36
0
def test_set_location():
    df = rc.DataFrame({
        'a': [1, 2, 3, 4],
        'b': [5, 6, 7, 8]
    },
                      index=[2, 4, 6, 8])

    df.set_location(2, {'a': -3})
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [1, 2, -3, 4],
            'b': [5, 6, 7, 8]
        },
                     index=[2, 4, 6, 8]))

    df.set_location(3, {'a': -10, 'b': -88})
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [1, 2, -3, -10],
            'b': [5, 6, 7, -88]
        },
                     index=[2, 4, 6, 8]))

    df.set_location(0, {'b': -55}, missing_to_none=True)
    assert_frame_equal(
        df,
        rc.DataFrame({
            'a': [None, 2, -3, -10],
            'b': [-55, 6, 7, -88]
        },
                     index=[2, 4, 6, 8]))

    # location out of bounds
    with pytest.raises(IndexError):
        df.set_location(99, {'a': 10})
示例#37
0
def test_assert_frame_equal():
    df1 = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[1, 2, 3])
    assert_frame_equal(df1, df1)

    df2 = rc.DataFrame({'a': [1, 1, 1], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[1, 2, 3])
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['b', 'a'], index=[1, 2, 3])
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[11, 12, 13])
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[1, 2, 3], use_blist=True)
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)

    df2 = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[1, 2, 3], sort=True)
    with pytest.raises(AssertionError):
        assert_frame_equal(df1, df2)
示例#38
0
def test_get_slicer_sorted():
    df = rc.DataFrame(
        {
            'a': [1, 2, 3],
            'b': [4, 5, 6],
            'c': [7, 8, 9],
            'd': [10, 11, 12]
        },
        columns=['a', 'b', 'c', 'd'],
        sort=True)

    # df[1:2] -- get slice from index 1 to 2, all columns
    assert_frame_equal(
        df[1:2],
        rc.DataFrame({
            'a': [2, 3],
            'b': [5, 6],
            'c': [8, 9],
            'd': [11, 12]
        },
                     columns=['a', 'b', 'c', 'd'],
                     index=[1, 2],
                     sort=True))

    # df[0:1, ['c', 'd']] -- get slice from index 0 to 1, columns ['c', 'd']
    assert_frame_equal(
        df[0:1, ['c', 'd']],
        rc.DataFrame({
            'c': [7, 8],
            'd': [10, 11]
        },
                     columns=['c', 'd'],
                     index=[0, 1],
                     sort=True))

    assert_frame_equal(
        df[0:1, ['d', 'c']],
        rc.DataFrame({
            'd': [10, 11],
            'c': [7, 8]
        },
                     columns=['d', 'c'],
                     index=[0, 1],
                     sort=True))

    # df[1:1, 'c'] -- get slice 1 to 1 and column 'c'
    assert_frame_equal(df[1:1, 'c'],
                       rc.DataFrame({'c': [8]}, index=[1], sort=True))

    # test indexes not in the range
    assert_frame_equal(df[4:5],
                       rc.DataFrame(columns=['a', 'b', 'c', 'd'], sort=True))
    assert_frame_equal(df[2:1],
                       rc.DataFrame(columns=['a', 'b', 'c', 'd'], sort=True))
    assert_frame_equal(df[0:8], df)
    assert_frame_equal(df[1.5:3.5], df.get_slice(1.5, 3.5))
示例#39
0
def test_get_square_brackets_sorted():
    df = rc.DataFrame(
        {
            'a': [1, 2, 3],
            'b': [4, 5, 6],
            'c': [7, 8, 9],
            'd': [10, 11, 12]
        },
        columns=['a', 'b', 'c', 'd'],
        sort=True)

    # df['b'] -- get column
    assert_frame_equal(df['b'], rc.DataFrame({'b': [4, 5, 6]}, sort=True))

    # df[['a', 'b', c']] -- get columns
    assert_frame_equal(
        df[['a', 'b', 'c']],
        rc.DataFrame({
            'a': [1, 2, 3],
            'b': [4, 5, 6],
            'c': [7, 8, 9]
        },
                     columns=['a', 'b', 'c'],
                     sort=True))

    assert_frame_equal(
        df[['c', 'a']],
        rc.DataFrame({
            'c': [7, 8, 9],
            'a': [1, 2, 3]
        },
                     columns=['c', 'a'],
                     sort=True))

    # df[1, 'd'] -- get cell at index = 5, column = 'b'
    assert df[1, 'd'] == 11

    # df[[0, 2]] -- get indexes = [0, 2] all columns
    assert_frame_equal(
        df[[0, 2], df.columns],
        rc.DataFrame({
            'a': [1, 3],
            'b': [4, 6],
            'c': [7, 9],
            'd': [10, 12]
        },
                     columns=['a', 'b', 'c', 'd'],
                     index=[0, 2],
                     sort=True))

    assert_frame_equal(
        df[[2, 1], df.columns],
        rc.DataFrame({
            'a': [2, 3],
            'b': [5, 6],
            'c': [8, 9],
            'd': [11, 12]
        },
                     columns=['a', 'b', 'c', 'd'],
                     index=[1, 2],
                     sort=True))

    # df[[0, 2], 'c'] -- get indexes = [4, 5], column = 'b'
    assert_frame_equal(df[[0, 2], 'c'],
                       rc.DataFrame({'c': [7, 9]}, index=[0, 2], sort=True))

    assert_frame_equal(df[[2, 0], 'c'],
                       rc.DataFrame({'c': [9, 7]}, index=[2, 0], sort=True))

    # df[[1, 2], ['a', 'd']] -- get indexes = [4, 5], columns = ['a', 'b']
    assert_frame_equal(
        df[[1, 2], ['a', 'd']],
        rc.DataFrame({
            'a': [2, 3],
            'd': [11, 12]
        },
                     columns=['a', 'd'],
                     index=[1, 2],
                     sort=True))

    assert_frame_equal(
        df[[2, 0], ['d', 'a']],
        rc.DataFrame({
            'd': [10, 12],
            'a': [1, 3]
        },
                     columns=['d', 'a'],
                     index=[0, 2],
                     sort=True))
示例#40
0
def test_get_slice():
    # fails for non-sort DataFrame
    df = rc.DataFrame({
        'a': [1, 2, 3, 4],
        'b': [5, 6, 7, 8]
    },
                      index=[2, 4, 6, 8])
    with pytest.raises(RuntimeError):
        df.get_slice(2, 4)

    # empty DataFrame
    df = rc.DataFrame(sort=True)
    assert_frame_equal(df.get_slice(3, 3), rc.DataFrame(sort=True))

    df = rc.DataFrame(sort=True, columns=['a', 'b'])
    assert_frame_equal(df.get_slice(3, 3),
                       rc.DataFrame(sort=True, columns=['a', 'b']))

    # full DataFrame
    df = rc.DataFrame({
        'a': [1, 2, 3, 4],
        'b': [5, 6, 7, 8]
    },
                      columns=['a', 'b'],
                      index=[2, 4, 6, 8],
                      sort=True)

    assert_frame_equal(df.get_slice(2, 8), df)
    assert_frame_equal(df.get_slice(1, 8), df)
    assert_frame_equal(df.get_slice(2, 10), df)
    assert_frame_equal(df.get_slice(1, 10), df)

    assert_frame_equal(df.get_slice(4, 4, ['b']),
                       rc.DataFrame({'b': [6]}, index=[4], sort=True))
    assert_frame_equal(df.get_slice(3, 4, ['b']),
                       rc.DataFrame({'b': [6]}, index=[4], sort=True))
    assert_frame_equal(df.get_slice(4, 5, ['b']),
                       rc.DataFrame({'b': [6]}, index=[4], sort=True))
    assert_frame_equal(df.get_slice(3, 5, ['b']),
                       rc.DataFrame({'b': [6]}, index=[4], sort=True))

    assert_frame_equal(df.get_slice(4, 6, ['a']),
                       rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))
    assert_frame_equal(df.get_slice(3, 6, ['a']),
                       rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))
    assert_frame_equal(df.get_slice(4, 7, ['a']),
                       rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))
    assert_frame_equal(df.get_slice(3, 7, ['a']),
                       rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))

    assert_frame_equal(df.get_slice(None, 5, ['a']),
                       rc.DataFrame({'a': [1, 2]}, index=[2, 4], sort=True))
    assert_frame_equal(df.get_slice(5, None, [True, False]),
                       rc.DataFrame({'a': [3, 4]}, index=[6, 8], sort=True))

    # boolean column list not the right size
    with pytest.raises(ValueError):
        df.get_slice(5, None, [True])

    assert_frame_equal(
        df.get_slice(3, 3),
        rc.DataFrame({
            'a': [],
            'b': []
        }, columns=['a', 'b'], sort=True))
    assert_frame_equal(
        df.get_slice(0, 0),
        rc.DataFrame({
            'a': [],
            'b': []
        }, columns=['a', 'b'], sort=True))
    assert_frame_equal(
        df.get_slice(10, 10),
        rc.DataFrame({
            'a': [],
            'b': []
        }, columns=['a', 'b'], sort=True))
示例#41
0
def test_get_square_brackets_sorted():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9], 'd': [10, 11, 12]}, columns=['a', 'b', 'c', 'd'],
                      sort=True)

    # df['b'] -- get column
    assert_frame_equal(df['b'], rc.DataFrame({'b': [4, 5, 6]}, sort=True))

    # df[['a', 'b', c']] -- get columns
    assert_frame_equal(df[['a', 'b', 'c']], rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]},
                                                         columns=['a', 'b', 'c'], sort=True))

    assert_frame_equal(df[['c', 'a']], rc.DataFrame({'c': [7, 8, 9], 'a': [1, 2, 3]}, columns=['c', 'a'], sort=True))

    # df[1, 'd'] -- get cell at index = 5, column = 'b'
    assert df[1, 'd'] == 11

    # df[[0, 2]] -- get indexes = [0, 2] all columns
    assert_frame_equal(df[[0, 2], df.columns],
                       rc.DataFrame({'a': [1, 3], 'b': [4, 6], 'c': [7, 9], 'd': [10, 12]},
                                    columns=['a', 'b', 'c', 'd'], index=[0, 2], sort=True))

    assert_frame_equal(df[[2, 1], df.columns],
                       rc.DataFrame({'a': [2, 3], 'b': [5, 6], 'c': [8, 9], 'd': [11, 12]},
                                    columns=['a', 'b', 'c', 'd'], index=[1, 2], sort=True))

    # df[[0, 2], 'c'] -- get indexes = [4, 5], column = 'b'
    assert_frame_equal(df[[0, 2], 'c'], rc.DataFrame({'c': [7, 9]}, index=[0, 2], sort=True))

    assert_frame_equal(df[[2, 0], 'c'], rc.DataFrame({'c': [9, 7]}, index=[2, 0], sort=True))

    # df[[1, 2], ['a', 'd']] -- get indexes = [4, 5], columns = ['a', 'b']
    assert_frame_equal(df[[1, 2], ['a', 'd']], rc.DataFrame({'a': [2, 3], 'd': [11, 12]}, columns=['a', 'd'],
                                                            index=[1, 2], sort=True))

    assert_frame_equal(df[[2, 0], ['d', 'a']], rc.DataFrame({'d': [10, 12], 'a': [1, 3]}, columns=['d', 'a'],
                                                            index=[0, 2], sort=True))
示例#42
0
def test_get_slicer_sorted():
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9], 'd': [10, 11, 12]}, columns=['a', 'b', 'c', 'd'],
                      sort=True)

    # df[1:2] -- get slice from index 1 to 2, all columns
    assert_frame_equal(df[1:2],
                       rc.DataFrame({'a': [2, 3], 'b': [5, 6], 'c': [8, 9], 'd': [11, 12]},
                                    columns=['a', 'b', 'c', 'd'], index=[1, 2], sort=True))

    # df[0:1, ['c', 'd']] -- get slice from index 0 to 1, columns ['c', 'd']
    assert_frame_equal(df[0:1, ['c', 'd']], rc.DataFrame({'c': [7, 8], 'd': [10, 11]},
                                                         columns=['c', 'd'], index=[0, 1], sort=True))

    assert_frame_equal(df[0:1, ['d', 'c']], rc.DataFrame({'d': [10, 11], 'c': [7, 8]},
                                                         columns=['d', 'c'], index=[0, 1], sort=True))

    # df[1:1, 'c'] -- get slice 1 to 1 and column 'c'
    assert_frame_equal(df[1:1, 'c'], rc.DataFrame({'c': [8]}, index=[1], sort=True))

    # test indexes not in the range
    assert_frame_equal(df[4:5], rc.DataFrame(columns=['a', 'b', 'c', 'd'], sort=True))
    assert_frame_equal(df[2:1], rc.DataFrame(columns=['a', 'b', 'c', 'd'], sort=True))
    assert_frame_equal(df[0:8], df)
    assert_frame_equal(df[1.5:3.5], df.get_slice(1.5, 3.5))
示例#43
0
def test_reset_index():
    # no index defined
    df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'])
    df.reset_index()
    expected = rc.DataFrame(
        {
            'a': [1, 2, 3],
            'b': [4, 5, 6],
            'index_0': [0, 1, 2]
        },
        columns=['a', 'b', 'index_0'])
    assert_frame_equal(df, expected)

    # with index and index name defined
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      columns=['a', 'b'],
                      index=['x', 'y', 'z'],
                      index_name='jelo')
    df.reset_index()
    expected = rc.DataFrame(
        {
            'a': [1, 2, 3],
            'b': [4, 5, 6],
            'jelo': ['x', 'y', 'z']
        },
        columns=['a', 'b', 'jelo'],
        sorted=False)
    assert_frame_equal(df, expected)

    # with a tuple multi-index
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      columns=['a', 'b'],
                      index=[('a', 10, 'x'), ('b', 11, 'y'), ('c', 12, 'z')],
                      index_name=('melo', 'helo', 'gelo'))
    df.reset_index()
    expected = rc.DataFrame(
        {
            'a': [1, 2, 3],
            'b': [4, 5, 6],
            'melo': ['a', 'b', 'c'],
            'helo': [10, 11, 12],
            'gelo': ['x', 'y', 'z']
        },
        columns=['a', 'b', 'melo', 'helo', 'gelo'],
        sorted=False)
    assert_frame_equal(df, expected)

    # drop
    df = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6]
    },
                      columns=['a', 'b'],
                      index=['x', 'y', 'z'],
                      index_name='jelo')
    df.reset_index(drop=True)
    expected = rc.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6],
    },
                            columns=['a', 'b'],
                            sorted=False)
    assert_frame_equal(df, expected)
示例#44
0
def test_get_slice():
    # fails for non-sort DataFrame
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]}, index=[2, 4, 6, 8])
    with pytest.raises(RuntimeError):
        df.get_slice(2, 4)

    # empty DataFrame
    df = rc.DataFrame(sort=True)
    assert_frame_equal(df.get_slice(3, 3), rc.DataFrame(sort=True))

    df = rc.DataFrame(sort=True, columns=['a', 'b'])
    assert_frame_equal(df.get_slice(3, 3), rc.DataFrame(sort=True, columns=['a', 'b']))

    # full DataFrame
    df = rc.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]}, columns=['a', 'b'], index=[2, 4, 6, 8], sort=True)

    assert_frame_equal(df.get_slice(2, 8), df)
    assert_frame_equal(df.get_slice(1, 8), df)
    assert_frame_equal(df.get_slice(2, 10), df)
    assert_frame_equal(df.get_slice(1, 10), df)

    assert_frame_equal(df.get_slice(4, 4, ['b']), rc.DataFrame({'b': [6]}, index=[4], sort=True))
    assert_frame_equal(df.get_slice(3, 4, ['b']), rc.DataFrame({'b': [6]}, index=[4], sort=True))
    assert_frame_equal(df.get_slice(4, 5, ['b']), rc.DataFrame({'b': [6]}, index=[4], sort=True))
    assert_frame_equal(df.get_slice(3, 5, ['b']), rc.DataFrame({'b': [6]}, index=[4], sort=True))

    assert_frame_equal(df.get_slice(4, 6, ['a']), rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))
    assert_frame_equal(df.get_slice(3, 6, ['a']), rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))
    assert_frame_equal(df.get_slice(4, 7, ['a']), rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))
    assert_frame_equal(df.get_slice(3, 7, ['a']), rc.DataFrame({'a': [2, 3]}, index=[4, 6], sort=True))

    assert_frame_equal(df.get_slice(None, 5, ['a']), rc.DataFrame({'a': [1, 2]}, index=[2, 4], sort=True))
    assert_frame_equal(df.get_slice(5, None, [True, False]), rc.DataFrame({'a': [3, 4]}, index=[6, 8], sort=True))

    # boolean column list not the right size
    with pytest.raises(ValueError):
        df.get_slice(5, None, [True])

    assert_frame_equal(df.get_slice(3, 3), rc.DataFrame({'a': [], 'b': []}, columns=['a', 'b'], sort=True))
    assert_frame_equal(df.get_slice(0, 0), rc.DataFrame({'a': [], 'b': []}, columns=['a', 'b'], sort=True))
    assert_frame_equal(df.get_slice(10, 10), rc.DataFrame({'a': [], 'b': []}, columns=['a', 'b'], sort=True))