Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: pandas.api.interchange.from_dataframe now uses the Arrow PyCapsule Interface if available, only falling back to the Dataframe Interchange Protocol if that fails #60739

Merged
merged 3 commits into from
Jan 21, 2025
Merged
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
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is

(Pdb) p result.columns
Index([], dtype='object')
(Pdb) p expected.columns
RangeIndex(start=0, stop=0, step=1)

Given that this only affects dataframes with zero columns, are we OK with this difference? I am

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally RangeIndex would be better, but OK to punt



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)
Loading