Skip to content

Commit

Permalink
ENH: pandas.api.interchange.from_dataframe now uses the Arrow PyCap…
Browse files Browse the repository at this point in the history
…sule Interface if available, only falling back to the Dataframe Interchange Protocol if that fails (#60739)

* add test for list dtype

* catch arrowinvalid and keep raising runtimeerror

* use rst hyperlink
  • Loading branch information
MarcoGorelli authored Jan 21, 2025
1 parent 27baf48 commit bbd6526
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Other enhancements
^^^^^^^^^^^^^^^^^^
- :class:`pandas.api.typing.FrozenList` is available for typing the outputs of :attr:`MultiIndex.names`, :attr:`MultiIndex.codes` and :attr:`MultiIndex.levels` (:issue:`58237`)
- :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`)
- :meth:`pandas.api.interchange.from_dataframe` now uses the `PyCapsule Interface <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html>`_ if available, only falling back to the Dataframe Interchange Protocol if that fails (:issue:`60739`)
- :class:`pandas.api.typing.NoDefault` is available for typing ``no_default``
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
- :func:`pandas.merge` now validates the ``how`` parameter input (merge type) (:issue:`59435`)
Expand Down
16 changes: 15 additions & 1 deletion pandas/core/interchange/from_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
.. note::
For new development, we highly recommend using the Arrow C Data Interface
alongside the Arrow PyCapsule Interface instead of the interchange protocol
alongside the Arrow PyCapsule Interface instead of the interchange protocol.
From pandas 3.0 onwards, `from_dataframe` uses the PyCapsule Interface,
only falling back to the interchange protocol if that fails.
.. warning::
Expand Down Expand Up @@ -90,6 +92,18 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
if isinstance(df, pd.DataFrame):
return df

if hasattr(df, "__arrow_c_stream__"):
try:
pa = import_optional_dependency("pyarrow", min_version="14.0.0")
except ImportError:
# fallback to _from_dataframe
pass
else:
try:
return pa.table(df).to_pandas(zero_copy_only=not allow_copy)
except pa.ArrowInvalid as e:
raise RuntimeError(e) from e

if not hasattr(df, "__dataframe__"):
raise ValueError("`df` does not support __dataframe__")

Expand Down
14 changes: 11 additions & 3 deletions pandas/tests/interchange/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def test_empty_pyarrow(data):
expected = pd.DataFrame(data)
arrow_df = pa_from_dataframe(expected)
result = from_dataframe(arrow_df)
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected, check_column_type=False)


def test_multi_chunk_pyarrow() -> None:
Expand All @@ -288,8 +288,7 @@ def test_multi_chunk_pyarrow() -> None:
table = pa.table([n_legs], names=names)
with pytest.raises(
RuntimeError,
match="To join chunks a copy is required which is "
"forbidden by allow_copy=False",
match="Cannot do zero copy conversion into multi-column DataFrame block",
):
pd.api.interchange.from_dataframe(table, allow_copy=False)

Expand Down Expand Up @@ -641,3 +640,12 @@ def test_buffer_dtype_categorical(
col = dfi.get_column_by_name("data")
assert col.dtype == expected_dtype
assert col.get_buffers()["data"][1] == expected_buffer_dtype


def test_from_dataframe_list_dtype():
pa = pytest.importorskip("pyarrow", "14.0.0")
data = {"a": [[1, 2], [4, 5, 6]]}
tbl = pa.table(data)
result = from_dataframe(tbl)
expected = pd.DataFrame(data)
tm.assert_frame_equal(result, expected)

0 comments on commit bbd6526

Please sign in to comment.