Skip to content

Commit d1dd468

Browse files
authored
BUG: respect allow_copy=False in interchange.from_dataframe (#54322)
respect allow_copy=False in interchange.from_dataframe
1 parent 41ab6af commit d1dd468

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ Other
685685
- Bug in :class:`DataFrame` and :class:`Series` raising for data of complex dtype when ``NaN`` values are present (:issue:`53627`)
686686
- Bug in :class:`DatetimeIndex` where ``repr`` of index passed with time does not print time is midnight and non-day based freq(:issue:`53470`)
687687
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
688+
- Bug in :func:`api.interchange.from_dataframe` was not respecting ``allow_copy`` argument (:issue:`54322`)
688689
- Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`)
689690
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
690691
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)

pandas/core/interchange/from_dataframe.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
6767
if not hasattr(df, "__dataframe__"):
6868
raise ValueError("`df` does not support __dataframe__")
6969

70-
return _from_dataframe(df.__dataframe__(allow_copy=allow_copy))
70+
return _from_dataframe(
71+
df.__dataframe__(allow_copy=allow_copy), allow_copy=allow_copy
72+
)
7173

7274

7375
def _from_dataframe(df: DataFrameXchg, allow_copy: bool = True):
@@ -451,10 +453,9 @@ def buffer_to_ndarray(
451453
data_pointer = ctypes.cast(
452454
buffer.ptr + (offset * bit_width // 8), ctypes.POINTER(ctypes_type)
453455
)
454-
return np.ctypeslib.as_array(
455-
data_pointer,
456-
shape=(length,),
457-
)
456+
if length > 0:
457+
return np.ctypeslib.as_array(data_pointer, shape=(length,))
458+
return np.array([], dtype=ctypes_type)
458459

459460

460461
def set_nulls(

pandas/tests/interchange/test_impl.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ def test_empty_pyarrow(data):
286286
tm.assert_frame_equal(result, expected)
287287

288288

289+
def test_multi_chunk_pyarrow() -> None:
290+
pa = pytest.importorskip("pyarrow", "11.0.0")
291+
n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
292+
names = ["n_legs"]
293+
table = pa.table([n_legs], names=names)
294+
with pytest.raises(
295+
RuntimeError,
296+
match="To join chunks a copy is required which is "
297+
"forbidden by allow_copy=False",
298+
):
299+
pd.api.interchange.from_dataframe(table, allow_copy=False)
300+
301+
289302
@pytest.mark.parametrize("tz", ["UTC", "US/Pacific"])
290303
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
291304
def test_datetimetzdtype(tz, unit):

0 commit comments

Comments
 (0)