Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -4664,24 +4664,24 @@ cdef class Table(_PandasConvertible):

return pyarrow_wrap_table(c_table)

def drop(self, columns):
def drop_columns(self, columns):
"""
Drop one or more columns and return a new table.

Parameters
----------
columns : list of str
List of field names referencing existing columns.
columns : str or list[str]
Field name(s) referencing existing column(s).

Raises
------
KeyError
If any of the passed columns name are not existing.
If any of the passed column names do not exist.

Returns
-------
Table
New table without the columns.
New table without the column(s).

Examples
--------
Expand All @@ -4693,19 +4693,22 @@ cdef class Table(_PandasConvertible):

Drop one column:

>>> table.drop(["animals"])
>>> table.drop_columns("animals")
pyarrow.Table
n_legs: int64
----
n_legs: [[2,4,5,100]]

Drop more columns:
Drop one or more columns:

>>> table.drop(["n_legs", "animals"])
>>> table.drop_columns(["n_legs", "animals"])
pyarrow.Table
...
----
"""
if isinstance(columns, str):
columns = [columns]

indices = []
for col in columns:
idx = self.schema.get_field_index(col)
Expand All @@ -4722,6 +4725,10 @@ cdef class Table(_PandasConvertible):

return table

def drop(self, columns):
"""Alias of Table.drop_columns, but kept for backwards compatibility."""
return self.drop_columns(columns)

def group_by(self, keys):
"""Declare a grouping over the columns of the table.

Expand Down
2 changes: 1 addition & 1 deletion python/pyarrow/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3906,7 +3906,7 @@ def test_write_dataset_with_scanner(tempdir):
load_back = ds.dataset(tempdir2, format='ipc', partitioning=["b"])
load_back_table = load_back.to_table()
assert dict(load_back_table.to_pydict()
) == table.drop(["a"]).to_pydict()
) == table.drop_columns("a").to_pydict()


@pytest.mark.parquet
Expand Down
29 changes: 26 additions & 3 deletions python/pyarrow/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,17 +1050,40 @@ def test_table_set_column():
assert t2.equals(expected)


def test_table_drop():
def test_table_drop_columns():
""" drop one or more columns given labels"""
a = pa.array(range(5))
b = pa.array([-10, -5, 0, 5, 10])
c = pa.array(range(5, 10))

table = pa.Table.from_arrays([a, b, c], names=('a', 'b', 'c'))
t2 = table.drop_columns(['a', 'b'])
t3 = table.drop_columns('a')

exp_t2 = pa.Table.from_arrays([c], names=('c',))
assert exp_t2.equals(t2)
exp_t3 = pa.Table.from_arrays([b, c], names=('b', 'c',))
assert exp_t3.equals(t3)

# -- raise KeyError if column not in Table
with pytest.raises(KeyError, match="Column 'd' not found"):
table.drop_columns(['d'])


def test_table_drop():
""" verify the alias of drop_columns is working"""
a = pa.array(range(5))
b = pa.array([-10, -5, 0, 5, 10])
c = pa.array(range(5, 10))

table = pa.Table.from_arrays([a, b, c], names=('a', 'b', 'c'))
t2 = table.drop(['a', 'b'])
t3 = table.drop('a')

exp = pa.Table.from_arrays([c], names=('c',))
assert exp.equals(t2)
exp_t2 = pa.Table.from_arrays([c], names=('c',))
assert exp_t2.equals(t2)
exp_t3 = pa.Table.from_arrays([b, c], names=('b', 'c',))
assert exp_t3.equals(t3)

# -- raise KeyError if column not in Table
with pytest.raises(KeyError, match="Column 'd' not found"):
Expand Down