Skip to content

Add support for dataframes that have toPandas method #4300

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 7 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Updated
- Updated Plotly.js from version 2.24.1 to version 2.24.2. See the [plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#2242----2023-06-09) for more information. These changes are reflected in the auto-generated `plotly.graph_objects` module.
- `px` methods now accept data-frame-like objects that support a [dataframe interchange protocol](https://data-apis.org/dataframe-protocol/latest/index.html), such as polars, vaex, modin etc. This protocol has priority on `to_pandas` call, but will only be used if pandas>=2.0.2 is installed in the environment.
- `px` methods now accept data-frame-like objects that support a `toPandas()` method, such as Spark DataFrames, or a `to_pandas_df()` method, such as Vaex DataFrames.

## [5.15.0] - 2023-06-08

Expand Down
19 changes: 17 additions & 2 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,12 @@ def build_dataframe(args, constructor):
elif hasattr(args["data_frame"], "to_pandas"):
args["data_frame"] = args["data_frame"].to_pandas()
columns = args["data_frame"].columns
elif hasattr(args["data_frame"], "toPandas"):
args["data_frame"] = args["data_frame"].toPandas()
columns = args["data_frame"].columns
elif hasattr(args["data_frame"], "to_pandas_df"):
args["data_frame"] = args["data_frame"].to_pandas_df()
columns = args["data_frame"].columns
else:
args["data_frame"] = pd.DataFrame(args["data_frame"])
columns = args["data_frame"].columns
Expand Down Expand Up @@ -1425,9 +1431,18 @@ def build_dataframe(args, constructor):
# def __dataframe__(self, ...):
# if not some_condition:
# self.to_pandas(...)
if not hasattr(df_not_pandas, "to_pandas"):
if not (
hasattr(df_not_pandas, "to_pandas")
or hasattr(df_not_pandas, "toPandas")
or hasattr(df_not_pandas, "to_pandas_df")
):
raise exc
args["data_frame"] = df_not_pandas.to_pandas()
if hasattr(df_not_pandas, "toPandas"):
args["data_frame"] = df_not_pandas.toPandas()
elif hasattr(df_not_pandas, "to_pandas_df"):
args["data_frame"] = df_not_pandas.to_pandas_df()
else:
args["data_frame"] = df_not_pandas.to_pandas()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a big deal, but it would be a bit cleaner to invert this:

if hasattr(df_not_pandas, "toPandas"):
    args["data_frame"] = df_not_pandas.toPandas()
elif hasattr(df_not_pandas, "to_pandas_df"):
    args["data_frame"] = df_not_pandas.to_pandas_df()
elif hasattr(df_not_pandas, "to_pandas"):
    args["data_frame"] = df_not_pandas.to_pandas()
else:
    raise exc


df_input = args["data_frame"]

Expand Down