Beispiel #1
0
def test_table(key):
    """Test a few ways to input tables."""
    input = _TABLE_DATA[key]
    table = Table(value=input)
    if key == "split":
        assert table.value == input
    if key not in ("tuple", "data"):
        # make sure the output is the same as the input
        assert table.to_dict(key) == input
        # can also test equality of table widgets
        assert Table(value=table.to_dict(key)) == table
        table.row_headers = ("x", "x")
        table.column_headers = ("a", "a", "b")
        with pytest.warns(UserWarning):
            table.to_dict(key)
Beispiel #2
0
def test_orient_series():
    """Test to_dict with orient = 'index' ."""
    pd = pytest.importorskip("pandas",
                             reason="Pandas required for some tables tests")
    table = Table(value=_TABLE_DATA["dict"])
    out = table.to_dict("series")
    assert all(isinstance(s, pd.Series) for s in out.values())
Beispiel #3
0
def test_orient_index():
    """Test to_dict with orient = 'index' ."""
    table = Table(value=_TABLE_DATA["dict"])
    expected = {
        "r1": {"col_1": 1, "col_2": 2, "col_3": 3},
        "r2": {"col_1": 4, "col_2": 5, "col_3": 6},
    }
    assert table.to_dict("index") == expected

    table = Table(value=_TABLE_DATA["dict"])
    table.row_headers = ("a", "a")
    with pytest.warns(UserWarning):
        table.to_dict("index")

    with pytest.raises(ValueError):
        table.to_dict("notathing")  # type: ignore
Beispiel #4
0
def test_dataview_delitem():
    """Test that table.data can be indexed like a numpy array."""
    input = _TABLE_DATA["dict"]
    table = Table(value=input)
    row_keys = table.keys("row")  # also demoing keys views
    col_keys = table.keys("column")  # also demoing keys views
    assert list(row_keys) == ["r1", "r2"]
    assert list(col_keys) == ["col_1", "col_2", "col_3"]
    del table.data[1]
    assert not table.to_dict("dict") == input
    assert list(row_keys) == ["r1"]
    assert list(col_keys) == ["col_1", "col_2", "col_3"]
    del table.data[:, 2]
    assert list(row_keys) == ["r1"]
    assert list(col_keys) == ["col_1", "col_2"]

    with pytest.raises(ValueError):
        del table.data[0, 0]  # cannot delete cells
Beispiel #5
0
    "columns": ("c1", "c2", "c3"),
}

table = Table(value=dict_of_lists)

# it behaves like a dict:
table["new_col"] = [5, 5]
assert table.pop("new_col") == [5, 5]
# keys and items have both regular (column) and "row" modes
col_item_view = table.items()  # iterate col_header/column
row_item_view = table.items("row")  # iterate row_header/row

# we can just call dict() to get back our dict of lists
assert dict(table) == dict_of_lists
# or use one of many other exports in `to_dict`
assert table.to_dict("records") == list_of_records

# change headers
table.row_headers = ("row1", "row2")
table.column_headers = ("a", "b", "c")

# setting value clears and resets the table:
table.value = np.arange(18).reshape(6, 3)
# we can get/set/delete the 2D data table using numpy-style indexing:
# get every other row
assert table.data[::2] == [[0, 1, 2], [6, 7, 8], [12, 13, 14]]
# set every other column in the 3rd row
table.data[2, ::2] = [99, 99]

# export to numpy or pandas
# table.data.to_numpy()