diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index 68eb5cbdcac..028797e45b8 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -315,7 +315,7 @@ cdef int _schema_from_arrays( fields.resize(K) - if len(arrays) == 0: + if not K: raise ValueError('Must pass at least one array') if isinstance(arrays[0], Column): @@ -328,6 +328,9 @@ cdef int _schema_from_arrays( if names is None: raise ValueError('Must pass names when constructing ' 'from Array objects') + if len(names) != K: + raise ValueError("Length of names ({}) does not match " + "length of arrays ({})".format(len(names), K)) for i in range(K): val = arrays[i] if isinstance(val, (Array, ChunkedArray)): diff --git a/python/pyarrow/tests/test_parquet.py b/python/pyarrow/tests/test_parquet.py index 9b5a4bcc43c..09e427fb37b 100644 --- a/python/pyarrow/tests/test_parquet.py +++ b/python/pyarrow/tests/test_parquet.py @@ -58,7 +58,7 @@ def test_single_pylist_column_roundtrip(tmpdir): filename = tmpdir.join('single_{}_column.parquet' .format(dtype.__name__)) data = [pa.array(list(map(dtype, range(5))))] - table = pa.Table.from_arrays(data, names=('a', 'b')) + table = pa.Table.from_arrays(data, names=['a']) _write_table(table, filename.strpath) table_read = _read_table(filename.strpath) for col_written, col_read in zip(table.itercolumns(), diff --git a/python/pyarrow/tests/test_table.py b/python/pyarrow/tests/test_table.py index 4d5cb364c69..3a523e73c80 100644 --- a/python/pyarrow/tests/test_table.py +++ b/python/pyarrow/tests/test_table.py @@ -82,6 +82,18 @@ def test_recordbatch_basics(): batch[2] +def test_recordbatch_from_arrays_invalid_names(): + data = [ + pa.array(range(5)), + pa.array([-10, -5, 0, 5, 10]) + ] + with pytest.raises(ValueError): + pa.RecordBatch.from_arrays(data, names=['a', 'b', 'c']) + + with pytest.raises(ValueError): + pa.RecordBatch.from_arrays(data, names=['a']) + + def test_recordbatch_empty_metadata(): data = [ pa.array(range(5)), @@ -200,6 +212,18 @@ def test_table_basics(): assert chunk is not None +def test_table_from_arrays_invalid_names(): + data = [ + pa.array(range(5)), + pa.array([-10, -5, 0, 5, 10]) + ] + with pytest.raises(ValueError): + pa.Table.from_arrays(data, names=['a', 'b', 'c']) + + with pytest.raises(ValueError): + pa.Table.from_arrays(data, names=['a']) + + def test_table_add_column(): data = [ pa.array(range(5)),