Skip to content

Commit

Permalink
Merge pull request #4300 from plotly/toPandas-support
Browse files Browse the repository at this point in the history
Add support for dataframes that have `toPandas` method
  • Loading branch information
LiamConnors committed Aug 9, 2023
2 parents e670c4b + 2a409f1 commit 00813b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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
15 changes: 13 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,14 @@ def build_dataframe(args, constructor):
# def __dataframe__(self, ...):
# if not some_condition:
# self.to_pandas(...)
if not hasattr(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()
elif hasattr(df_not_pandas, "to_pandas"):
args["data_frame"] = df_not_pandas.to_pandas()
else:
raise exc
args["data_frame"] = df_not_pandas.to_pandas()

df_input = args["data_frame"]

Expand Down

0 comments on commit 00813b7

Please sign in to comment.