diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index 1abe4235c41..cb6a2e0acb4 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -4912,10 +4912,13 @@ cdef class Table(_Tabular): if isinstance(struct_array, Array): return Table.from_batches([RecordBatch.from_struct_array(struct_array)]) else: - return Table.from_batches([ - RecordBatch.from_struct_array(chunk) - for chunk in struct_array.chunks - ]) + chunks = struct_array.chunks or [struct_array.combine_chunks()] + return Table.from_batches( + [ + RecordBatch.from_struct_array(chunk) + for chunk in chunks + ] + ) def to_struct_array(self, max_chunksize=None): """ diff --git a/python/pyarrow/tests/test_table.py b/python/pyarrow/tests/test_table.py index bf6e5773ddf..cfe47e4ed05 100644 --- a/python/pyarrow/tests/test_table.py +++ b/python/pyarrow/tests/test_table.py @@ -913,6 +913,11 @@ def test_table_from_struct_array_invalid(): pa.Table.from_struct_array(pa.array(range(5))) +def test_table_from_struct_array_empty_chunked_array_invalid(): + with pytest.raises(TypeError, match="Argument 'struct_array' has incorrect type"): + pa.Table.from_struct_array(pa.chunked_array([], type=pa.int64())) + + def test_table_from_struct_array(): struct_array = pa.array( [{"ints": 1}, {"floats": 1.0}], @@ -941,6 +946,21 @@ def test_table_from_struct_array_chunked_array(): )) +def test_table_from_struct_array_for_empty_chunked_array(): + # GH-48344 + struct_type = pa.struct([("ints", pa.int32()), ("floats", pa.float32())]) + empty_chunked_struct_array = pa.chunked_array([], type=struct_type) + result = pa.Table.from_struct_array(empty_chunked_struct_array) + expected = pa.Table.from_arrays( + [ + pa.array([], type=pa.int32()), + pa.array([], type=pa.float32()), + ], ["ints", "floats"] + ) + assert result.equals(expected) + assert result.schema == expected.schema + + def test_table_to_struct_array(): table = pa.Table.from_arrays( [