Skip to content

Commit 31e174f

Browse files
committed
DEPR: Deprecate DataFrame Interchange Protocol
1 parent 46ed6b1 commit 31e174f

File tree

5 files changed

+153
-66
lines changed

5 files changed

+153
-66
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ Other Deprecations
738738
- Deprecated backward-compatibility behavior for :meth:`DataFrame.select_dtypes` matching "str" dtype when ``np.object_`` is specified (:issue:`61916`)
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 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`)
741+
- Deprecated support for the Dataframe Interchange Protocol (:issue:`56732`)
741742
- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`)
742743

743744
.. ---------------------------------------------------------------------------

pandas/core/frame.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,14 @@ def __dataframe__(
929929
- converting to pandas: for pandas >= 2.0.3
930930
- converting from pandas: for pandas >= 3.0.0
931931
932+
.. deprecated:: 3.0.0
933+
934+
The Dataframe Interchange Protocol is deprecated.
935+
For dataframe-agnostic code, you may want to look into:
936+
937+
- `Arrow PyCapsule Interface <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html>`_
938+
- `Narwhals <https://github.com/narwhals-dev/narwhals>`_
939+
932940
Parameters
933941
----------
934942
nan_as_null : bool, default False
@@ -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)