Beispiel #1
0
def test_cell_range_style():
    values = [[1]]
    cell = ipysheet.cell_range(values, color='red')
    assert cell.style['color'] == 'red'
    cell = ipysheet.cell_range(values, background_color='blue')
    assert cell.style['backgroundColor'] == 'blue'
    cell = ipysheet.cell_range(values, font_style='nice')
    assert cell.style['fontStyle'] == 'nice'
    cell = ipysheet.cell_range(values, font_weight='bold')
    assert cell.style['fontWeight'] == 'bold'
Beispiel #2
0
def test_cell_range():
    ipysheet.sheet(rows=3, columns=4)
    # [row][column]
    ipysheet.cell_range([[0, 1]])  # 1 row, 2 columns
    ipysheet.cell_range([[0], [2]])  # 2 rows, 1 columns
    ipysheet.cell_range([[0, 1], [2, 3]])  # 2 rows, 2 columns
    ipysheet.cell_range([[0, 1], [2, 3], [4, 5]])  # 3 rows, 2 columns
    ipysheet.cell_range([[0, 1, 9], [2, 3, 9], [4, 5, 9]])  # 3 rows, 3 columns
    ipysheet.cell_range([[0, 1, 9]], column_end=2)  # 3 rows, 3 columns
    ipysheet.cell_range([[0, 1, 9]], column_start=1)  # 1 rows, 3 columns
    with pytest.raises(ValueError):
        ipysheet.cell_range([[0, 1], [2, 3], [4, 5], [6,
                                                      7]])  # 4 rows, 2 columns
    with pytest.raises(ValueError):
        ipysheet.cell_range([[0, 1, 2, 3, 4], [2, 3, 4, 5, 6],
                             [3, 4, 5, 6, 7]])  # 3 rows, 5 columns
    with pytest.raises(ValueError):
        ipysheet.cell_range([[0, 1, 2, 3, 4], [2], [3, 4, 5, 6,
                                                    7]])  # not well shaped
    with pytest.raises(ValueError):
        ipysheet.cell_range([])  # empty rows
    with pytest.raises(ValueError):
        ipysheet.cell_range([[], []])  # empty columns

    value = [[0, 1], [2, 3], [4, 5]]
    valueT = [[0, 2, 4], [1, 3, 5]]  # it's transpose
    assert value == transpose(valueT)
    r = ipysheet.cell_range(value)  # 3 rows, 2 columns
    with pytest.raises(ValueError):
        r.value = 1
    with pytest.raises(ValueError):
        r.value = [1, 2, 3]
    with pytest.raises(ValueError):
        r.value = [[1, 2]]
    assert r.value == transpose(valueT)

    rT = ipysheet.cell_range(valueT, transpose=True)  # 3 rows, 2 columns
    with pytest.raises(ValueError):
        rT.value = 1
    with pytest.raises(ValueError):
        rT.value = [1, 2, 3]
    with pytest.raises(ValueError):
        rT.value = [[1, 2]]
    rT.value = transpose(value)
    assert rT.value == transpose(value)
Beispiel #3
0
def test_to_dataframe():
    sheet = ipysheet.sheet(rows=5, columns=4)
    ipysheet.cell(0, 0, value=True)
    ipysheet.row(1, value=[2, 34, 543, 23])
    ipysheet.column(3, value=[1.2, 1.3, 1.4, 1.5, 1.6])

    df = ipysheet.to_dataframe(sheet)
    assert np.all(df['A'].tolist() == [True, 2, None, None, None])
    assert np.all(df['B'].tolist() == [None, 34, None, None, None])
    assert np.all(df['C'].tolist() == [None, 543, None, None, None])
    assert np.all(df['D'].tolist() == [1.2, 1.3, 1.4, 1.5, 1.6])

    sheet = ipysheet.sheet(rows=4,
                           columns=4,
                           column_headers=['c0', 'c1', 'c2', 'c3'],
                           row_headers=['r0', 'r1', 'r2', 'r3'])
    ipysheet.cell_range([
        [2, 34, 543, 23],
        [1, 1, 1, 1],
        [2, 2, 222, 22],
        [2, 0, 111, 11],
    ],
                        row_start=0,
                        column_start=0,
                        transpose=True)

    df = ipysheet.to_dataframe(sheet)
    assert np.all(df['c0'].tolist() == [2, 34, 543, 23])
    assert np.all(df['c1'].tolist() == [1, 1, 1, 1])
    assert np.all(df['c2'].tolist() == [2, 2, 222, 22])
    assert np.all(df['c3'].tolist() == [2, 0, 111, 11])

    sheet = ipysheet.sheet(rows=4,
                           columns=4,
                           column_headers=['t0', 't1', 't2', 't3'])
    ipysheet.cell_range([
        [2, 34, 543, 23],
        [1, 1, 1, 1],
        [2, 2, 222, 22],
        [2, 0, 111, 11],
    ],
                        row_start=0,
                        column_start=0,
                        transpose=False)

    df = ipysheet.to_dataframe(sheet)
    assert np.all(df['t0'].tolist() == [2, 1, 2, 2])
    assert np.all(df['t1'].tolist() == [34, 1, 2, 0])
    assert np.all(df['t2'].tolist() == [543, 1, 222, 111])
    assert np.all(df['t3'].tolist() == [23, 1, 22, 11])

    sheet = ipysheet.sheet(rows=0, columns=0)

    df = ipysheet.to_dataframe(sheet)
    assert np.all(df == pd.DataFrame())

    sheet = ipysheet.sheet(rows=4, columns=1)
    ipysheet.column(0,
                    ['2019/02/28', '2019/02/27', '2019/02/26', '2019/02/25'],
                    type='date')

    df = ipysheet.to_dataframe(sheet)
    assert [_format_date(x) for x in df['A'].tolist()
            ] == ['2019/02/28', '2019/02/27', '2019/02/26', '2019/02/25']