Skip to content
Closed
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
25 changes: 24 additions & 1 deletion python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,30 @@ cdef class Table:
self._check_nullptr()
return pyarrow_wrap_schema(self.table.schema())

def column(self, int i):
def column(self, i):
"""
Select a column by its column name, or numeric index.

Parameters
----------
i : int or string

Returns
-------
pyarrow.Column
"""
if isinstance(i, six.string_types):
field_index = self.schema.get_field_index(i)
if field_index < 0:
raise KeyError("Column {} does not exist in table".format(i))
else:
return self._column(field_index)
elif isinstance(i, six.integer_types):
return self._column(i)
else:
raise TypeError("Index must either be string or integer")

def _column(self, int i):
"""
Select a column by its numeric index.

Expand Down
17 changes: 17 additions & 0 deletions python/pyarrow/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ def test_table_from_arrays_invalid_names():
pa.Table.from_arrays(data, names=['a'])


def test_table_select_column():
data = [
pa.array(range(5)),
pa.array([-10, -5, 0, 5, 10]),
pa.array(range(5, 10))
]
table = pa.Table.from_arrays(data, names=('a', 'b', 'c'))

assert table.column('a').equals(table.column(0))

with pytest.raises(KeyError):
table.column('d')

with pytest.raises(TypeError):
table.column(None)


def test_table_add_column():
data = [
pa.array(range(5)),
Expand Down