Skip to content

Commit

Permalink
ARROW-6872: [Python] Fix empty table creation from schema with dictio…
Browse files Browse the repository at this point in the history
…nary field

This is a quick fix for ARROW-6872 to handle dictionary fields. What it does not yet fix is a nested type with dictionary inside (eg list of dictionary type), which probably needs a sequence converted for dictionary type.

Closes #6698 from jorisvandenbossche/ARROW-6872

Authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Signed-off-by: Benjamin Kietzman <bengilgit@gmail.com>
  • Loading branch information
jorisvandenbossche authored and bkietz committed Mar 24, 2020
1 parent bd3182f commit 7e6429c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions python/pyarrow/array.pxi
Expand Up @@ -2155,3 +2155,16 @@ def concat_arrays(arrays, MemoryPool memory_pool=None):
check_status(Concatenate(c_arrays, pool, &c_result))

return pyarrow_wrap_array(c_result)


def _empty_array(DataType type):
"""
Create empty array of the given type.
"""
if type.id == Type_DICTIONARY:
arr = DictionaryArray.from_arrays(
_empty_array(type.index_type), _empty_array(type.value_type),
ordered=type.ordered)
else:
arr = array([], type=type)
return arr
4 changes: 3 additions & 1 deletion python/pyarrow/tests/test_schema.py
Expand Up @@ -547,7 +547,9 @@ def test_type_schema_pickling():

def test_empty_table():
schema = pa.schema([
pa.field('oneField', pa.int64())
pa.field('f0', pa.int64()),
pa.field('f1', pa.dictionary(pa.int32(), pa.string())),
pa.field('f2', pa.list_(pa.list_(pa.int64()))),
])
table = schema.empty_table()
assert isinstance(table, pa.Table)
Expand Down
2 changes: 1 addition & 1 deletion python/pyarrow/types.pxi
Expand Up @@ -1036,7 +1036,7 @@ cdef class Schema:
arrays = []
names = []
for field in self:
arrays.append(array([], type=field.type))
arrays.append(_empty_array(field.type))
names.append(field.name)
return Table.from_arrays(
arrays=arrays,
Expand Down

0 comments on commit 7e6429c

Please sign in to comment.