Skip to content

Commit

Permalink
ARROW-13784: [Python] Table.from_arrays should raise an error when ar…
Browse files Browse the repository at this point in the history
…ray is empty but names is not

We already check that the list of arrays and list of names should have the same length in `table.pxi`.
For a special case when a list of arrays is of length 0 but length of list of names is greater than 0 the check fails.
I changed the ordering of checks in `table.pxi` and added a test to `test_table.py`.

Closes #11457 from AlenkaF/ARROW-13784

Authored-by: Alenka Frim <frim.alenka@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
AlenkaF authored and jorisvandenbossche committed Oct 20, 2021
1 parent c8f882c commit 98b0e99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions python/pyarrow/table.pxi
Expand Up @@ -572,8 +572,12 @@ cdef _schema_from_arrays(arrays, names, metadata, shared_ptr[CSchema]* schema):
c_meta = KeyValueMetadata(metadata).unwrap()

if K == 0:
schema.reset(new CSchema(c_fields, c_meta))
return arrays
if names is None or len(names) == 0:
schema.reset(new CSchema(c_fields, c_meta))
return arrays
else:
raise ValueError('Length of names ({}) does not match '
'length of arrays ({})'.format(len(names), K))

c_fields.resize(K)

Expand Down
17 changes: 17 additions & 0 deletions python/pyarrow/tests/test_table.py
Expand Up @@ -1022,6 +1022,23 @@ def test_table_remove_column_empty():
assert t3.equals(table)


def test_empty_table_with_names():
# ARROW-13784
data = []
names = ["a", "b"]
message = (
'Length of names [(]2[)] does not match length of arrays [(]0[)]')
with pytest.raises(ValueError, match=message):
pa.Table.from_arrays(data, names=names)


def test_empty_table():
table = pa.table([])

assert table.column_names == []
assert table.equals(pa.Table.from_arrays([], []))


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

0 comments on commit 98b0e99

Please sign in to comment.