def test_tabulator_function_filter(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df)

    model = table.get_root(document, comm)

    widget = TextInput(value='foo3')

    def filter_c(df, value):
        return df[df.C.str.contains(value)]

    table.add_filter(bind(filter_c, value=widget), 'C')

    expected = {
        'index':
        np.array([2]),
        'A':
        np.array([2]),
        'B':
        np.array([0]),
        'C':
        np.array(['foo3']),
        'D':
        np.array(['2009-01-05T00:00:00.000000000'],
                 dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])

    widget.value = 'foo1'

    expected = {
        'index':
        np.array([0]),
        'A':
        np.array([0]),
        'B':
        np.array([0]),
        'C':
        np.array(['foo1']),
        'D':
        np.array(['2009-01-01T00:00:00.000000000'],
                 dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])
def test_tabulator_selected_and_filtered_dataframe(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df, selection=list(range(len(df))))

    pd.testing.assert_frame_equal(table.selected_dataframe, df)

    table.add_filter('foo3', 'C')

    pd.testing.assert_frame_equal(table.selected_dataframe,
                                  df[df["C"] == "foo3"])

    table.remove_filter('foo3')

    table.selection = [0, 1, 2]

    table.add_filter('foo3', 'C')

    assert table.selection == [0]
def test_tabulator_constant_scalar_filter(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df)

    model = table.get_root(document, comm)

    table.add_filter('foo3', 'C')

    expected = {
        'index':
        np.array([2]),
        'A':
        np.array([2]),
        'B':
        np.array([0]),
        'C':
        np.array(['foo3']),
        'D':
        np.array(['2009-01-05T00:00:00.000000000'],
                 dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])
def test_tabulator_stream_dataframe_with_filter(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df)

    model = table.get_root(document, comm)

    table.add_filter(['foo2', 'foo7'], 'C')

    stream_value = pd.DataFrame({
        'A': [5, 6],
        'B': [1, 0],
        'C': ['foo6', 'foo7'],
        'D': [dt.datetime(2009, 1, 8),
              dt.datetime(2009, 1, 9)]
    })

    table.stream(stream_value)

    assert len(table.value) == 7

    expected = {
        'index':
        np.array([1, 6]),
        'A':
        np.array([1, 6]),
        'B':
        np.array([1, 0]),
        'C':
        np.array(['foo2', 'foo7']),
        'D':
        np.array(
            ['2009-01-02T00:00:00.000000000', '2009-01-09T00:00:00.000000000'],
            dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])