Skip to content

Commit

Permalink
feat(api): make drop variadic
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Sep 19, 2022
1 parent 96e1580 commit 1d69702
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/dask/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_read_with_undiscoverable_type(client):

def test_drop(table):
table = table.mutate(c=table.a)
expr = table.drop(['a'])
expr = table.drop('a')
result = expr.execute()
expected = table[['b', 'c']].execute()
tm.assert_frame_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/pandas/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_list_tables(client):

def test_drop(table):
table = table.mutate(c=table.a)
expr = table.drop(['a'])
expr = table.drop('a')
result = expr.execute()
expected = table[['b', 'c']].execute()
tm.assert_frame_equal(result, expected)
Expand Down
8 changes: 2 additions & 6 deletions ibis/backends/tests/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def check_eq(left, right, how, **kwargs):
@pytest.mark.notimpl(["datafusion"])
def test_mutating_join(backend, batting, awards_players, how):
left = batting[batting.yearID == 2015]
right = awards_players[awards_players.lgID == 'NL'].drop(
['yearID', 'lgID']
)
right = awards_players[awards_players.lgID == 'NL'].drop('yearID', 'lgID')

left_df = left.execute()
right_df = right.execute()
Expand Down Expand Up @@ -135,9 +133,7 @@ def test_mutating_join(backend, batting, awards_players, how):
)
def test_filtering_join(backend, batting, awards_players, how):
left = batting[batting.yearID == 2015]
right = awards_players[awards_players.lgID == 'NL'].drop(
['yearID', 'lgID']
)
right = awards_players[awards_players.lgID == 'NL'].drop('yearID', 'lgID')

left_df = left.execute()
right_df = right.execute()
Expand Down
13 changes: 10 additions & 3 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import itertools
import operator
import sys
import warnings
from functools import cached_property
from typing import IO, TYPE_CHECKING, Any, Iterable, Literal, Mapping, Sequence

Expand Down Expand Up @@ -685,7 +686,7 @@ def relabel(self, substitutions: Mapping[str, str]) -> Table:

return self.select(exprs)

def drop(self, fields: str | Sequence[str]) -> Table:
def drop(self, *fields: str) -> Table:
"""Remove fields from a table.
Parameters
Expand All @@ -696,13 +697,19 @@ def drop(self, fields: str | Sequence[str]) -> Table:
Returns
-------
Table
Expression without `fields`
A table with all columns in `fields` removed.
"""
if not fields:
# no-op if nothing to be dropped
return self

fields = util.promote_list(fields)
if len(fields) == 1 and not isinstance(fields[0], str):
fields = util.promote_list(fields[0])
warnings.warn(
"Passing a sequence of fields to `drop` is deprecated and "
"will be removed in version 5.0, use `drop(*fields)` instead",
FutureWarning,
)

schema = self.schema()
field_set = frozenset(fields)
Expand Down
19 changes: 19 additions & 0 deletions ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,25 @@ def test_join_suffixes(how):
assert expr.columns == ["id_left", "first_name", "id_right", "last_name"]


def test_drop():
t = ibis.table(dict.fromkeys("abcd", "int"))

assert t.drop() is t

res = t.drop("a")
assert res.equals(t.select("b", "c", "d"))

res = t.drop("a", "b")
assert res.equals(t.select("c", "d"))

with pytest.raises(KeyError, match="Fields not in table"):
t.drop("missing")

with pytest.warns(FutureWarning, match="a sequence of fields"):
res = t.drop(["a", "b"])
assert res.equals(t.select("c", "d"))


def test_python_table_ambiguous():
with pytest.raises(NotImplementedError):
ibis.memtable(
Expand Down
15 changes: 4 additions & 11 deletions ibis/tests/sql/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_table_drop_with_filter():
[('a', 'int64'), ('b', 'string'), ('c', 'timestamp')], name='t'
).relabel({'c': 'C'})
left = left.filter(left.C == datetime.datetime(2018, 1, 1))
left = left.drop(['C'])
left = left.drop('C')
left = left.mutate(the_date=datetime.datetime(2018, 1, 1))

right = ibis.table([('b', 'string')], name='s')
Expand Down Expand Up @@ -311,17 +311,10 @@ def test_table_drop_consistency():
)

expected = t.projection(["a", "c"])
result_1 = t.drop(["b"])
result_2 = t.drop("b")
result = t.drop("b")

assert expected.schema() == result_1.schema()
assert expected.schema() == result_2.schema()

assert expected.schema() != t.schema()

assert "b" not in expected.columns
assert "a" in result_1.columns
assert "c" in result_2.columns
assert expected.schema() == result.schema()
assert set(result.columns) == {"a", "c"}


def test_subquery_where_location():
Expand Down

0 comments on commit 1d69702

Please sign in to comment.