Skip to content

Commit c41013e

Browse files
Backport PR pandas-dev#38539: BUG: fix array conversion from Arrow for slided array (pandas-dev#38611)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 0758b3c commit c41013e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ I/O
747747
- :meth:`DataFrame.to_html` was ignoring ``formatters`` argument for ``ExtensionDtype`` columns (:issue:`36525`)
748748
- Bumped minimum xarray version to 0.12.3 to avoid reference to the removed ``Panel`` class (:issue:`27101`)
749749
- :meth:`DataFrame.to_csv` was re-opening file-like handles that also implement ``os.PathLike`` (:issue:`38125`)
750+
- Bug in the conversion of a sliced ``pyarrow.Table`` with missing values to a DataFrame (:issue:`38525`)
750751

751752
Period
752753
^^^^^^

pandas/core/arrays/_arrow_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def pyarrow_array_to_numpy_and_mask(arr, dtype):
3030
bitmask = buflist[0]
3131
if bitmask is not None:
3232
mask = pyarrow.BooleanArray.from_buffers(
33-
pyarrow.bool_(), len(arr), [None, bitmask]
33+
pyarrow.bool_(), len(arr), [None, bitmask], offset=arr.offset
3434
)
3535
mask = np.asarray(mask)
3636
else:

pandas/tests/arrays/masked/test_arrow_compat.py

+12
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,15 @@ def test_arrow_from_arrow_uint():
5252
expected = pd.array([1, 2, 3, 4, None], dtype="UInt32")
5353

5454
tm.assert_extension_array_equal(result, expected)
55+
56+
57+
@td.skip_if_no("pyarrow", min_version="0.16.0")
58+
def test_arrow_sliced():
59+
# https://github.com/pandas-dev/pandas/issues/38525
60+
import pyarrow as pa
61+
62+
df = pd.DataFrame({"a": pd.array([0, None, 2, 3, None], dtype="Int64")})
63+
table = pa.table(df)
64+
result = table.slice(2, None).to_pandas()
65+
expected = df.iloc[2:].reset_index(drop=True)
66+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)