Skip to content

Commit 7c03e13

Browse files
committed
fix: use PyCapsule Interface instead of Dataframe Interchange Protocol
1 parent b4e5f8d commit 7c03e13

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dev = [
4242
"mypy",
4343
"pandas-stubs",
4444
"pre-commit",
45+
"pyarrow",
4546
"flit",
4647
]
4748
docs = [

seaborn/_core/data.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from collections.abc import Mapping, Sized
77
from typing import cast
8-
import warnings
98

109
import pandas as pd
1110
from pandas import DataFrame
@@ -269,9 +268,9 @@ def _assign_variables(
269268

270269
def handle_data_source(data: object) -> pd.DataFrame | Mapping | None:
271270
"""Convert the data source object to a common union representation."""
272-
if isinstance(data, pd.DataFrame) or hasattr(data, "__dataframe__"):
271+
if isinstance(data, pd.DataFrame) or hasattr(data, "__arrow_c_stream__"):
273272
# Check for pd.DataFrame inheritance could be removed once
274-
# minimal pandas version supports dataframe interchange (1.5.0).
273+
# minimal pandas version supports PyCapsule Interface (2.2).
275274
data = convert_dataframe_to_pandas(data)
276275
elif data is not None and not isinstance(data, Mapping):
277276
err = f"Data source must be a DataFrame or Mapping, not {type(data)!r}."
@@ -285,35 +284,32 @@ def convert_dataframe_to_pandas(data: object) -> pd.DataFrame:
285284
if isinstance(data, pd.DataFrame):
286285
return data
287286

288-
if not hasattr(pd.api, "interchange"):
289-
msg = (
290-
"Support for non-pandas DataFrame objects requires a version of pandas "
291-
"that implements the DataFrame interchange protocol. Please upgrade "
292-
"your pandas version or coerce your data to pandas before passing "
293-
"it to seaborn."
294-
)
295-
raise TypeError(msg)
296-
297-
if _version_predates(pd, "2.0.2"):
298-
msg = (
299-
"DataFrame interchange with pandas<2.0.2 has some known issues. "
300-
f"You are using pandas {pd.__version__}. "
301-
"Continuing, but it is recommended to carefully inspect the results and to "
302-
"consider upgrading."
303-
)
304-
warnings.warn(msg, stacklevel=2)
305-
306-
try:
307-
# This is going to convert all columns in the input dataframe, even though
308-
# we may only need one or two of them. It would be more efficient to select
309-
# the columns that are going to be used in the plot prior to interchange.
310-
# Solving that in general is a hard problem, especially with the objects
311-
# interface where variables passed in Plot() may only be referenced later
312-
# in Plot.add(). But noting here in case this seems to be a bottleneck.
313-
return pd.api.interchange.from_dataframe(data)
314-
except Exception as err:
315-
msg = (
316-
"Encountered an exception when converting data source "
317-
"to a pandas DataFrame. See traceback above for details."
318-
)
319-
raise RuntimeError(msg) from err
287+
if hasattr(data, '__arrow_c_stream__'):
288+
try:
289+
import pyarrow
290+
except ImportError as err:
291+
msg = "PyArrow is required for non-pandas Dataframe support."
292+
raise RuntimeError(msg) from err
293+
if _version_predates(pyarrow, '14.0.0'):
294+
msg = "PyArrow>=14.0.0 is required for non-pandas Dataframe support."
295+
raise RuntimeError(msg)
296+
try:
297+
# This is going to convert all columns in the input dataframe, even though
298+
# we may only need one or two of them. It would be more efficient to select
299+
# the columns that are going to be used in the plot prior to interchange.
300+
# Solving that in general is a hard problem, especially with the objects
301+
# interface where variables passed in Plot() may only be referenced later
302+
# in Plot.add(). But noting here in case this seems to be a bottleneck.
303+
return pyarrow.table(data).to_pandas()
304+
except Exception as err:
305+
msg = (
306+
"Encountered an exception when converting data source "
307+
"to a pandas DataFrame. See traceback above for details."
308+
)
309+
raise RuntimeError(msg) from err
310+
311+
msg = (
312+
"Expected object which implements '__arrow_c_stream__' from the "
313+
f"PyCapsule Interface, got: {type(data)}"
314+
)
315+
raise TypeError(msg)

tests/_core/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def test_data_interchange(self, mock_long_df, long_df):
425425
)
426426
def test_data_interchange_failure(self, mock_long_df):
427427

428-
mock_long_df._data = None # Break __dataframe__()
428+
mock_long_df.__arrow_c_stream__ = lambda _x: 1 / 0 # Break __arrow_c_stream__()
429429
with pytest.raises(RuntimeError, match="Encountered an exception"):
430430
PlotData(mock_long_df, {"x": "x"})
431431

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ class MockInterchangeableDataFrame:
188188
def __init__(self, data):
189189
self._data = data
190190

191-
def __dataframe__(self, *args, **kwargs):
192-
return self._data.__dataframe__(*args, **kwargs)
191+
def __arrow_c_stream__(self, *args, **kwargs):
192+
return self._data.__arrow_c_stream__()
193193

194194

195195
@pytest.fixture

0 commit comments

Comments
 (0)