Skip to content

Fix usage of column_names which is part of Interchange protocol #4442

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

Merged
merged 3 commits into from
Nov 28, 2023
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix issue with creating dendrogram in subplots [[#4411](https://github.com/plotly/plotly.py/pull/4411)],
- Fix issue with px.line not accepting "spline" line shape [[#2812](https://github.com/plotly/plotly.py/issues/2812)]
- Fix KeyError when using column of `pd.Categorical` dtype with unobserved categories [[#4437](https://github.com/plotly/plotly.py/pull/4437)]
- Fix dataframe interchange in case `column_names` returns an unmaterialized object: generator, iterator, etc. [[#4442]](https://github.com/plotly/plotly.py/pull/4442)

## [5.18.0] - 2023-10-25

Expand Down
6 changes: 5 additions & 1 deletion packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,11 @@ def build_dataframe(args, constructor):

df_not_pandas = args["data_frame"]
args["data_frame"] = df_not_pandas.__dataframe__()
columns = args["data_frame"].column_names()
# According interchange protocol: `def column_names(self) -> Iterable[str]:`
# so this function can return for example a generator.
# The easiest way is to convert `columns` to `pandas.Index` so that the
# type is similar to the types in other code branches.
columns = pd.Index(args["data_frame"].column_names())
needs_interchanging = True
elif hasattr(args["data_frame"], "to_pandas"):
args["data_frame"] = args["data_frame"].to_pandas()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
@pytest.fixture
def add_interchange_module_for_old_pandas():
if not hasattr(pd.api, "interchange"):
pd.api.interchange = mock.MagicMock()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is necessary so that this attribute is not saved between runs.

# to make the following import work: `import pandas.api.interchange`
with mock.patch.dict(
"sys.modules", {"pandas.api.interchange": pd.api.interchange}
):
yield
with mock.patch.object(pd.api, "interchange", mock.MagicMock(), create=True):
# to make the following import work: `import pandas.api.interchange`
with mock.patch.dict(
"sys.modules", {"pandas.api.interchange": pd.api.interchange}
):
yield
else:
yield

Expand Down Expand Up @@ -250,15 +250,24 @@ def test_build_df_with_index():
assert_frame_equal(tips.reset_index()[out["data_frame"].columns], out["data_frame"])


@pytest.mark.parametrize("column_names_as_generator", [False, True])
def test_build_df_using_interchange_protocol_mock(
add_interchange_module_for_old_pandas,
add_interchange_module_for_old_pandas, column_names_as_generator
):
class InterchangeDataFrame:
def __init__(self, columns):
self._columns = columns

def column_names(self):
return self._columns
if column_names_as_generator:

def column_names(self):
for col in self._columns:
yield col

else:

def column_names(self):
return self._columns

interchange_dataframe = InterchangeDataFrame(
["petal_width", "sepal_length", "sepal_width"]
Expand Down