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

fall back to to_pandas if interchange protocol fails #3534

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions seaborn/_core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,18 @@
# in Plot.add(). But noting here in case this seems to be a bottleneck.
return pd.api.interchange.from_dataframe(data)
except Exception as err:
msg = (
"Encountered an exception when converting data source "
"to a pandas DataFrame. See traceback above for details."
)
raise RuntimeError(msg) from err
if hasattr(data, 'to_pandas'):
try:
return data.to_pandas() # type: ignore[attr-defined]
mwaskom marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
msg = (

Check warning on line 319 in seaborn/_core/data.py

View check run for this annotation

Codecov / codecov/patch

seaborn/_core/data.py#L318-L319

Added lines #L318 - L319 were not covered by tests
"Encountered an exception when converting data source "
"to a pandas DataFrame. See traceback above for details."
)
raise RuntimeError(msg) from err

Check warning on line 323 in seaborn/_core/data.py

View check run for this annotation

Codecov / codecov/patch

seaborn/_core/data.py#L323

Added line #L323 was not covered by tests
mwaskom marked this conversation as resolved.
Show resolved Hide resolved
else:
msg = (
"Encountered an exception when converting data source "
"to a pandas DataFrame. See traceback above for details."
)
raise RuntimeError(msg) from err
13 changes: 13 additions & 0 deletions tests/_core/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,19 @@ def test_data_interchange_failure(self, mock_long_df):
with pytest.raises(RuntimeError, match="Encountered an exception"):
PlotData(mock_long_df, {"x": "x"})

@pytest.mark.skipif(
condition=not hasattr(pd.api, "interchange"),
reason="Tests behavior assuming support for dataframe interchange"
)
def test_data_interchange_failure_with_fallback(self, mock_long_df, long_df):

mock_long_df._data = None # Break __dataframe__()
mock_long_df.to_pandas = lambda: long_df
variables = {"x": "x", "y": "z", "color": "a"}
p = PlotData(mock_long_df, variables)
for var, col in variables.items():
assert_vector_equal(p.frame[var], long_df[col])

@pytest.mark.skipif(
condition=hasattr(pd.api, "interchange"),
reason="Tests graceful failure without support for dataframe interchange"
Expand Down
Loading