Skip to content

Commit

Permalink
ENH: Support contains/not contains in the pandas backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Nov 2, 2017
1 parent b530707 commit 514a8a8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ibis/expr/types.py
Expand Up @@ -1582,8 +1582,8 @@ class ValueList(ValueOp):
"""

def __init__(self, args):
self.values = [as_value_expr(x) for x in args]
ValueOp.__init__(self, self.values)
self.values = list(map(as_value_expr, args))
super(ValueList, self).__init__(self.values)

def root_tables(self):
return distinct_roots(*self.values)
Expand Down
15 changes: 15 additions & 0 deletions ibis/pandas/execution/generic.py
Expand Up @@ -763,3 +763,18 @@ def execute_array_collect_group_by(op, data, **kwargs):
@execute_node.register(ops.SelfReference, pd.DataFrame)
def execute_node_self_reference_dataframe(op, data, **kwargs):
return data


@execute_node.register(ir.ValueList)
def execute_node_value_list(op, **kwargs):
return [execute(arg, **kwargs) for arg in op.values]


@execute_node.register(ops.Contains, pd.Series, list)
def execute_node_contains_series_list(op, data, elements, **kwargs):
return data.isin(elements)


@execute_node.register(ops.NotContains, pd.Series, list)
def execute_node_not_contains_series_list(op, data, elements, **kwargs):
return ~data.isin(elements)
36 changes: 36 additions & 0 deletions ibis/pandas/execution/tests/test_operations.py
Expand Up @@ -634,3 +634,39 @@ def test_scalar_parameter(t, df, raw_value):
result = expr.execute(params={value: raw_value})
expected = df.float64_with_zeros == raw_value
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
'elements',
[
[1],
(1,),
pytest.mark.xfail({1}, raises=TypeError, reason='Not yet implemented'),
pytest.mark.xfail(
frozenset({1}), raises=TypeError, reason='Not yet implemented'
),
]
)
def test_isin(t, df, elements):
expr = t.plain_float64.isin(elements)
expected = df.plain_float64.isin(elements)
result = expr.execute()
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
'elements',
[
[1],
(1,),
pytest.mark.xfail({1}, raises=TypeError, reason='Not yet implemented'),
pytest.mark.xfail(
frozenset({1}), raises=TypeError, reason='Not yet implemented'
),
]
)
def test_notin(t, df, elements):
expr = t.plain_float64.notin(elements)
expected = ~df.plain_float64.isin(elements)
result = expr.execute()
tm.assert_series_equal(result, expected)

0 comments on commit 514a8a8

Please sign in to comment.