From 0527926e6c9d70b5e554f98c95dbbc9e60334e3f Mon Sep 17 00:00:00 2001 From: Kee Chong Tan Date: Sat, 21 Apr 2018 14:37:23 +0100 Subject: [PATCH 1/2] Improve Table column access --- python/pyarrow/table.pxi | 25 ++++++++++++++++++++++++- python/pyarrow/tests/test_table.py | 17 +++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index cbf2a69f78c..9660e36cd2d 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -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. diff --git a/python/pyarrow/tests/test_table.py b/python/pyarrow/tests/test_table.py index 5303cb2190c..492cf0089e6 100644 --- a/python/pyarrow/tests/test_table.py +++ b/python/pyarrow/tests/test_table.py @@ -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)), From c65033c2b46d6d35f044734c7a07c8522cbc9552 Mon Sep 17 00:00:00 2001 From: Kee Chong Tan Date: Sat, 21 Apr 2018 15:08:32 +0100 Subject: [PATCH 2/2] Remove whitespace --- python/pyarrow/table.pxi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index 9660e36cd2d..cbbfe7da8b4 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -1174,7 +1174,7 @@ cdef class Table: 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.