Skip to content

Commit 88815a1

Browse files
authored
GH-46355: [Python] Fix table.to_struct_array with an empty table (#46357)
### Rationale for this change Currently `pyarrow.Table.to_struct_array` fails if table is empty, this PR fixes that. See issue #46355 for the reproducible example. ### What changes are included in this PR? Check if table is empty and create an empty chunked array using table schema for the array type ### Are these changes tested? I added a test for that, it failed before I implemented the fix ### Are there any user-facing changes? No * GitHub Issue: #46355 Authored-by: Konstantin Malanchev <[email protected]> Signed-off-by: Raúl Cumplido <[email protected]>
1 parent e2a5b4e commit 88815a1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

python/pyarrow/table.pxi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4960,10 +4960,11 @@ cdef class Table(_Tabular):
49604960
ChunkedArray
49614961
"""
49624962
self._assert_cpu()
4963-
return chunked_array([
4963+
chunks = [
49644964
batch.to_struct_array()
49654965
for batch in self.to_batches(max_chunksize=max_chunksize)
4966-
])
4966+
]
4967+
return chunked_array(chunks, type=struct(self.schema))
49674968

49684969
@staticmethod
49694970
def from_batches(batches, Schema schema=None):

python/pyarrow/tests/test_table.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,22 @@ def test_table_to_struct_array_with_max_chunksize():
970970
))
971971

972972

973+
def test_table_to_struct_array_for_empty_table():
974+
table = pa.Table.from_arrays(
975+
[
976+
pa.array([], type=pa.int32()),
977+
pa.array([], type=pa.float32()),
978+
], ["ints", "floats"]
979+
)
980+
result = table.to_struct_array()
981+
assert result.equals(
982+
pa.chunked_array(
983+
[],
984+
type=pa.struct({"ints": pa.int32(), "floats": pa.float32()}),
985+
),
986+
)
987+
988+
973989
def check_tensors(tensor, expected_tensor, type, size):
974990
assert tensor.equals(expected_tensor)
975991
assert tensor.size == size

0 commit comments

Comments
 (0)