Skip to content

Commit 865ae1d

Browse files
authored
DEPR: Deprecate DataFrame Interchange Protocol (#62920)
1 parent 4dc4296 commit 865ae1d

File tree

6 files changed

+164
-69
lines changed

6 files changed

+164
-69
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ Other Deprecations
739739
- Deprecated option "future.no_silent_downcasting", as it is no longer used. In a future version accessing this option will raise (:issue:`59502`)
740740
- Deprecated silent casting of non-datetime 'other' to datetime in :meth:`Series.combine_first` (:issue:`62931`)
741741
- Deprecated slicing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` using a ``datetime.date`` object, explicitly cast to :class:`Timestamp` instead (:issue:`35830`)
742+
- Deprecated support for the Dataframe Interchange Protocol (:issue:`56732`)
742743
- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`)
743744

744745
.. ---------------------------------------------------------------------------

pandas/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,14 @@ def pytest_collection_modifyitems(items, config) -> None:
135135
# Warnings from doctests that can be ignored; place reason in comment above.
136136
# Each entry specifies (path, message) - see the ignore_doctest_warning function
137137
ignored_doctest_warnings = [
138+
("api.interchange.from_dataframe", ".*Interchange Protocol is deprecated"),
138139
("is_int64_dtype", "is_int64_dtype is deprecated"),
139140
("is_interval_dtype", "is_interval_dtype is deprecated"),
140141
("is_period_dtype", "is_period_dtype is deprecated"),
141142
("is_datetime64tz_dtype", "is_datetime64tz_dtype is deprecated"),
142143
("is_categorical_dtype", "is_categorical_dtype is deprecated"),
143144
("is_sparse", "is_sparse is deprecated"),
145+
("DataFrame.__dataframe__", "Interchange Protocol is deprecated"),
144146
("DataFrameGroupBy.fillna", "DataFrameGroupBy.fillna is deprecated"),
145147
("DataFrameGroupBy.corrwith", "DataFrameGroupBy.corrwith is deprecated"),
146148
("NDFrame.replace", "Series.replace without 'value'"),

pandas/core/frame.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,14 @@ def __dataframe__(
916916
"""
917917
Return the dataframe interchange object implementing the interchange protocol.
918918
919+
.. deprecated:: 3.0.0
920+
921+
The Dataframe Interchange Protocol is deprecated.
922+
For dataframe-agnostic code, you may want to look into:
923+
924+
- `Arrow PyCapsule Interface <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html>`_
925+
- `Narwhals <https://github.com/narwhals-dev/narwhals>`_
926+
919927
.. note::
920928
921929
For new development, we highly recommend using the Arrow C Data Interface
@@ -970,7 +978,14 @@ def __dataframe__(
970978
These methods (``column_names``, ``select_columns_by_name``) should work
971979
for any dataframe library which implements the interchange protocol.
972980
"""
973-
981+
warnings.warn(
982+
"The Dataframe Interchange Protocol is deprecated.\n"
983+
"For dataframe-agnostic code, you may want to look into:\n"
984+
"- Arrow PyCapsule Interface: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html\n"
985+
"- Narwhals: https://github.com/narwhals-dev/narwhals\n",
986+
Pandas4Warning,
987+
stacklevel=find_stack_level(),
988+
)
974989
from pandas.core.interchange.dataframe import PandasDataFrameXchg
975990

976991
return PandasDataFrameXchg(self, allow_copy=allow_copy)

pandas/core/interchange/from_dataframe.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
Any,
77
overload,
88
)
9+
import warnings
910

1011
import numpy as np
1112

1213
from pandas._config import using_string_dtype
1314

1415
from pandas.compat._optional import import_optional_dependency
16+
from pandas.errors import Pandas4Warning
1517
from pandas.util._decorators import set_module
18+
from pandas.util._exceptions import find_stack_level
1619

1720
import pandas as pd
1821
from pandas.core.interchange.dataframe_protocol import (
@@ -47,6 +50,9 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
4750
From pandas 3.0 onwards, `from_dataframe` uses the PyCapsule Interface,
4851
only falling back to the interchange protocol if that fails.
4952
53+
From pandas 4.0 onwards, that fallback will no longer be available and only
54+
the PyCapsule Interface will be used.
55+
5056
.. warning::
5157
5258
Due to severe implementation issues, we recommend only considering using the
@@ -99,7 +105,14 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
99105
pa = import_optional_dependency("pyarrow", min_version="14.0.0")
100106
except ImportError:
101107
# fallback to _from_dataframe
102-
pass
108+
warnings.warn(
109+
"Conversion using Arrow PyCapsule Interface failed due to "
110+
"missing PyArrow>=14 dependency, falling back to (deprecated) "
111+
"interchange protocol. We recommend that you install "
112+
"PyArrow>=14.0.0.",
113+
UserWarning,
114+
stacklevel=find_stack_level(),
115+
)
103116
else:
104117
try:
105118
return pa.table(df).to_pandas(zero_copy_only=not allow_copy)
@@ -109,6 +122,15 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
109122
if not hasattr(df, "__dataframe__"):
110123
raise ValueError("`df` does not support __dataframe__")
111124

125+
warnings.warn(
126+
"The Dataframe Interchange Protocol is deprecated.\n"
127+
"For dataframe-agnostic code, you may want to look into:\n"
128+
"- Arrow PyCapsule Interface: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html\n"
129+
"- Narwhals: https://github.com/narwhals-dev/narwhals\n",
130+
Pandas4Warning,
131+
stacklevel=find_stack_level(),
132+
)
133+
112134
return _from_dataframe(
113135
df.__dataframe__(allow_copy=allow_copy), allow_copy=allow_copy
114136
)

0 commit comments

Comments
 (0)