Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,12 @@ cdef class RecordBatch(_Tabular):
return pyarrow_wrap_batch(CRecordBatch.Make((<Schema> schema).sp_schema,
n_rows, c_arrays))
else:
# A pandas column can yield a ChunkedArray, e.g. for an
# arrow-backed dtype after pd.concat (GH-41936)
arrays = [
arr.combine_chunks() if isinstance(arr, ChunkedArray) else arr
for arr in arrays
]
return cls.from_arrays(arrays, schema=schema)

@staticmethod
Expand Down
13 changes: 13 additions & 0 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3791,6 +3791,19 @@ def test_recordbatch_from_to_pandas():
tm.assert_frame_equal(data, result)


def test_recordbatch_from_pandas_concatenated_arrow_backed():
# GH-41936: pd.concat of arrow-backed columns produces chunked
# arrays, which RecordBatch.from_pandas needs to combine
df = pd.DataFrame({'a': [1, 2]}).astype({'a': 'int32[pyarrow]'})
concat_df = pd.concat([df, df])

batch = pa.RecordBatch.from_pandas(concat_df, preserve_index=False)
assert batch.column('a').to_pylist() == [1, 2, 1, 2]
assert batch.schema.field('a').type == pa.int32()
tm.assert_frame_equal(concat_df.reset_index(drop=True),
batch.to_pandas())


def test_recordbatchlist_to_pandas():
data1 = pd.DataFrame({
'c1': np.array([1, 1, 2], dtype='uint32'),
Expand Down