From 3d3881eaa1d9b16e2315117a91f552e50889eb8f Mon Sep 17 00:00:00 2001 From: Sameer Kumar Date: Sat, 28 Nov 2026 01:25:14 +0530 Subject: [PATCH 1/2] GH-41936: [Python] Combine chunked arrays in RecordBatch.from_pandas pd.concat of DataFrames with arrow-backed dtypes produces chunked arrays inside the pandas columns, so pa.array() returns a ChunkedArray for those columns and RecordBatch.from_pandas failed with "TypeError: Cannot convert pyarrow.lib.ChunkedArray to pyarrow.lib.Array". Table.from_pandas is unaffected because Table.from_arrays accepts chunked arrays. Combine the chunked arrays into single arrays with ChunkedArray.combine_chunks() before calling RecordBatch.from_arrays. Table.from_pandas keeps its chunked columns unchanged. Added a regression test in test_pandas.py. --- python/pyarrow/table.pxi | 6 ++++++ python/pyarrow/tests/test_pandas.py | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index 30fff040fc37..a433fddff188 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -3425,6 +3425,12 @@ cdef class RecordBatch(_Tabular): return pyarrow_wrap_batch(CRecordBatch.Make(( 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 diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py index 1f09a9247e6b..d1bdb1a2240d 100644 --- a/python/pyarrow/tests/test_pandas.py +++ b/python/pyarrow/tests/test_pandas.py @@ -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'), From 688d6cc6cff47dbd7ce56f13c0902946a3074e30 Mon Sep 17 00:00:00 2001 From: Sameer Kumar Date: Sat, 28 Nov 2026 01:29:02 +0530 Subject: [PATCH 2/2] GH-38789: [Python][Docs] Correct ChunkedArray.to_numpy docstring The docstring claimed that a NumPy copy is always returned, but for a single chunk without nulls a zero-copy read-only view of the Arrow buffers is returned even with zero_copy_only=False, while copies are made for multiple chunks or in the presence of nulls. This explains the OWNDATA/WRITEABLE flags reported in the issue: a view is not writable (Arrow data is immutable), and a copy does not own its data either because the underlying buffer is owned by the memory pool allocation wrapped as the NumPy base. Document the actual view-or-copy behavior, the flag semantics, and how to obtain an owned writable array. --- python/pyarrow/table.pxi | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index a433fddff188..05c5981da3e7 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -468,13 +468,26 @@ cdef class ChunkedArray(_PandasConvertible): def to_numpy(self, zero_copy_only=False): """ - Return a NumPy copy of this array (experimental). + Return a NumPy view or copy of this array (experimental). + + A zero-copy view of the underlying Arrow buffers is returned when + possible, i.e. for a single chunk without nulls of a primitive type. + Such a view is not writable, as the Arrow data is immutable. In all + other cases (multiple chunks or in the presence of nulls), a new + writable NumPy array is returned that does not share memory with the + Arrow data. + + The returned array never owns its data itself (the OWNDATA flag is + False in both cases), so use the WRITEABLE flag or ``.copy()`` to + obtain an array that is safe to modify. Parameters ---------- zero_copy_only : bool, default False Introduced for signature consistence with pyarrow.Array.to_numpy. - This must be False here since NumPy arrays' buffer must be contiguous. + This must be False here since a ChunkedArray consisting of + multiple chunks cannot be viewed as a single contiguous NumPy + array. Returns -------