1 2
from __future__ import absolute_import, division, unicode_literals
2

3 2
import pytest
4

5 2
try:
6 2
    import pandas as pd
7 2
    from pandas.util.testing import makeTimeDataFrame
8 0
except ImportError:
9 0
    pytestmark = pytest.mark.skip('pandas not available')
10

11 2
from bokeh.models.widgets.tables import (
12
    NumberFormatter, IntEditor, NumberEditor, StringFormatter,
13
    SelectEditor, DateFormatter, DataCube, CellEditor,
14
    SumAggregator, AvgAggregator, MinAggregator
15
)
16

17 2
from panel.widgets import DataFrame
18

19

20 2
def test_dataframe_widget(dataframe, document, comm):
21

22 2
    table = DataFrame(dataframe)
23

24 2
    model = table.get_root(document, comm)
25

26 2
    index_col, int_col, float_col, str_col = model.columns
27

28 2
    assert index_col.title == 'index'
29 2
    assert isinstance(index_col.formatter, NumberFormatter)
30 2
    assert isinstance(index_col.editor, CellEditor)
31

32 2
    assert int_col.title == 'int'
33 2
    assert isinstance(int_col.formatter, NumberFormatter)
34 2
    assert isinstance(int_col.editor, IntEditor)
35

36 2
    assert float_col.title == 'float'
37 2
    assert isinstance(float_col.formatter, NumberFormatter)
38 2
    assert isinstance(float_col.editor, NumberEditor)
39

40 2
    assert str_col.title == 'str'
41 2
    assert isinstance(float_col.formatter, StringFormatter)
42 2
    assert isinstance(float_col.editor, NumberEditor)
43

44

45 2
def test_dataframe_widget_no_show_index(dataframe, document, comm):
46 2
    table = DataFrame(dataframe, show_index=False)
47

48 2
    model = table.get_root(document, comm)
49

50 2
    assert len(model.columns) == 3
51 2
    int_col, float_col, str_col = model.columns
52 2
    assert int_col.title == 'int'
53 2
    assert float_col.title == 'float'
54 2
    assert str_col.title == 'str'
55

56 2
    table.show_index = True
57

58 2
    assert len(model.columns) == 4
59 2
    index_col, int_col, float_col, str_col = model.columns
60 2
    assert index_col.title == 'index'
61 2
    assert int_col.title == 'int'
62 2
    assert float_col.title == 'float'
63 2
    assert str_col.title == 'str'
64

65

66 2
def test_dataframe_widget_datetimes(document, comm):
67

68 2
    table = DataFrame(makeTimeDataFrame())
69

70 2
    model = table.get_root(document, comm)
71

72 2
    dt_col, _, _, _, _ = model.columns
73

74 2
    assert dt_col.title == 'index'
75 2
    assert isinstance(dt_col.formatter, DateFormatter)
76 2
    assert isinstance(dt_col.editor, CellEditor)
77

78

79 2
def test_dataframe_editors(dataframe, document, comm):
80 2
    editor = SelectEditor(options=['A', 'B', 'C'])
81 2
    table = DataFrame(dataframe, editors={'str': editor})
82 2
    model = table.get_root(document, comm)
83

84 2
    assert model.columns[-1].editor is editor
85

86
    
87 2
def test_dataframe_formatter(dataframe, document, comm):
88 2
    formatter = NumberFormatter(format='0.0000')
89 2
    table = DataFrame(dataframe, formatters={'float': formatter})
90 2
    model = table.get_root(document, comm)
91 2
    assert model.columns[2].formatter is formatter
92

93

94 2
def test_dataframe_triggers(dataframe):
95 2
    events = []
96

97 2
    def increment(event, events=events):
98 2
        events.append(event)
99
    
100 2
    table = DataFrame(dataframe)
101 2
    table.param.watch(increment, 'value')
102 2
    table._process_events({'data': {'str': ['C', 'B', 'A']}})
103 2
    assert len(events) == 1
104
    
105
    
106 2
def test_dataframe_does_not_trigger(dataframe):
107 2
    events = []
108

109 2
    def increment(event, events=events):
110 0
        events.append(event)
111
    
112 2
    table = DataFrame(dataframe)
113 2
    table.param.watch(increment, 'value')
114 2
    table._process_events({'data': {'str': ['A', 'B', 'C']}})
115 2
    assert len(events) == 0
116

117

118 2
def test_dataframe_selected_dataframe(dataframe):
119 2
    table = DataFrame(dataframe, selection=[0, 2])
120 2
    pd.testing.assert_frame_equal(table.selected_dataframe, dataframe.iloc[[0, 2]])
121

122

123 2
def test_dataframe_process_selection_event(dataframe):
124 2
    table = DataFrame(dataframe, selection=[0, 2])
125 2
    table._process_events({'indices': [0, 2]})
126 2
    pd.testing.assert_frame_equal(table.selected_dataframe, dataframe.iloc[[0, 2]])
127

128

129 2
def test_dataframe_process_data_event(dataframe):
130 2
    df = dataframe.copy()
131

132 2
    table = DataFrame(dataframe, selection=[0, 2])
133 2
    table._process_events({'data': {'int': [5, 7, 9]}})
134 2
    df['int'] = [5, 7, 9]
135 2
    pd.testing.assert_frame_equal(table.value, df)
136

137 2
    table._process_events({'data': {'int': {1: 3, 2: 4, 0: 1}}})
138 2
    df['int'] = [1, 3, 4]
139 2
    pd.testing.assert_frame_equal(table.value, df)
140

141

142 2
def test_dataframe_duplicate_column_name(document, comm):
143 2
    df = pd.DataFrame([[1, 1], [2, 2]], columns=['col', 'col'])
144 2
    with pytest.raises(ValueError):
145 2
        table = DataFrame(df)
146

147 2
    df = pd.DataFrame([[1, 1], [2, 2]], columns=['a', 'b'])
148 2
    table = DataFrame(df)
149 2
    with pytest.raises(ValueError):
150 2
        table.value = table.value.rename(columns={'a': 'b'})
151
    
152 2
    df = pd.DataFrame([[1, 1], [2, 2]], columns=['a', 'b'])
153 2
    table = DataFrame(df)
154 2
    table.get_root(document, comm)
155 2
    with pytest.raises(ValueError):
156 2
        table.value = table.value.rename(columns={'a': 'b'})
157

158

159 2
def test_hierarchical_index(document, comm):
160 2
    df = pd.DataFrame([
161
        ('Germany', 2020, 9, 2.4, 'A'),
162
        ('Germany', 2021, 3, 7.3, 'C'),
163
        ('Germany', 2022, 6, 3.1, 'B'),
164
        ('UK', 2020, 5, 8.0, 'A'),
165
        ('UK', 2021, 1, 3.9, 'B'),
166
        ('UK', 2022, 9, 2.2, 'A')
167
    ], columns=['Country', 'Year', 'Int', 'Float', 'Str']).set_index(['Country', 'Year'])
168

169 2
    table = DataFrame(value=df, hierarchical=True,
170
                      aggregators={'Year': {'Int': 'sum', 'Float': 'mean'}})
171

172 2
    model = table.get_root(document, comm)
173 2
    assert isinstance(model, DataCube)
174 2
    assert len(model.grouping) == 1
175 2
    grouping = model.grouping[0]
176 2
    assert len(grouping.aggregators) == 2
177 2
    agg1, agg2 = grouping.aggregators
178 2
    assert agg1.field_ == 'Int'
179 2
    assert isinstance(agg1, SumAggregator)
180 2
    assert agg2.field_ == 'Float'
181 2
    assert isinstance(agg2, AvgAggregator)
182

183 2
    table.aggregators = {'Year': 'min'}
184

185 2
    agg1, agg2 = grouping.aggregators
186 2
    print(grouping)
187 2
    assert agg1.field_ == 'Int'
188 2
    assert isinstance(agg1, MinAggregator)
189 2
    assert agg2.field_ == 'Float'
190 2
    assert isinstance(agg2, MinAggregator)
191

192

193 2
def test_none_table(document, comm):
194 2
    table = DataFrame(value=None)
195 2
    assert table.indexes == []
196

197 2
    model = table.get_root(document, comm)
198

199 2
    assert model.source.data == {}

Read our documentation on viewing source code .

Loading