Example #1
0
def test_with_index():
    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)),
                      columns=list('ABCD'))
    df = df.set_index(['A'])
    n = FrametoArraySerializer()
    a = n.serialize(df)
    assert_frame_equal(df, n.deserialize(a))
Example #2
0
def test_multiple_data_input_with_no_index_and_invalid_column(column):
    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)),
                      columns=list('ABCD'))
    n = FrametoArraySerializer()
    a = n.serialize(df[['B']])
    b = n.serialize(df[['D']])
    assert_frame_equal(df[column], n.deserialize([a, b], columns=column))
def test_with_nans():
    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)),
                      columns=list('ABCD'))
    df['A'] = np.NaN
    n = FrametoArraySerializer()
    a = n.serialize(df)
    assert_frame_equal_(df, n.deserialize(a))
Example #4
0
def test_invalid_column_subset_with_index():
    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)),
                      columns=list('ABCD'))
    df = df.set_index(['A'])
    n = FrametoArraySerializer()
    a = n.serialize(df)
    expected = pd.DataFrame({'N': np.nan}, index=df.index)
    assert_frame_equal(expected, n.deserialize(a, columns=['N']))
Example #5
0
def test_multi_column_fail():
    df = pd.DataFrame(data={'A': [1, 2, 3], 'B': [2, 3, 4], 'C': [3, 4, 5]})
    df = df.set_index(['A'])
    n = FrametoArraySerializer()
    a = n.serialize(df)

    with pytest.raises(Exception) as e:
        n.deserialize(a, columns=['A', 'B'])
    assert ('Duplicate' in str(e.value))
Example #6
0
def test_multiple_data_input_different_columns(columns, index):
    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)),
                      columns=list('ABCD'))
    if index:
        df = df.set_index(['A'])
    n = FrametoArraySerializer()
    a = n.serialize(df[['B']])
    b = n.serialize(df[['D']])
    expected = df[['B']].append(df[['D']], ignore_index=not index)
    assert_frame_equal(expected, n.deserialize([a, b], columns=columns))
Example #7
0
def test_empty_columns():
    df = pd.DataFrame(data={'A': [], 'B': [], 'C': []})
    n = FrametoArraySerializer()
    a = n.serialize(df)
    assert_frame_equal(df, n.deserialize(a))
Example #8
0
def test_empty_dataframe():
    df = pd.DataFrame()
    n = FrametoArraySerializer()
    a = n.serialize(df)
    assert_frame_equal(df, n.deserialize(a))