Skip to content

Commit 3f8a650

Browse files
authored
Merge pull request #5727 from cpruijsen/fix/issue-4117
Fix Plotly Express mutating lists passed to x or y in wide mode
2 parents cb82f63 + a92e7dc commit 3f8a650

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1414
- Fix `mpl_to_plotly` showing unwanted legends with placeholder or internal label entries (`_nolegend_`, `_childN`) by defaulting `layout.showlegend=False` unless the matplotlib figure explicitly defines a legend [[#5699](https://github.com/plotly/plotly.py/pull/5699)], with thanks to @robertoffmoura for the contribution!
1515
- Fix `mpl_to_plotly` crashing on figures with fully transparent or alpha-carrying colors, such as boxplot outlier markers (`facecolor="none"`), rgba line colors, and `color="none"` text and axis labels; all matplotlib colors are now exported through a single helper that maps `"none"` to transparent rgba and keeps or overrides alpha as matplotlib does [[#5700](https://github.com/plotly/plotly.py/pull/5700)], with thanks to @robertoffmoura for the contribution!
1616
- Export tick label colors to the plotly tickfont in `mpl_to_plotly` [[#5716](https://github.com/plotly/plotly.py/pull/5716)], with thanks to @robertoffmoura for the contribution!
17+
- Fix Plotly Express mutating lists passed to the `x` or `y` arguments in wide mode [[#5727](https://github.com/plotly/plotly.py/pull/5727)], with thanks to @cpruijsen for the contribution!
1718
- Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5766](https://github.com/plotly/plotly.py/pull/5766)], with thanks to @Irahan2 for the contribution!
1819
- Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution!
1920

‎plotly/express/_core.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,8 +1687,9 @@ def build_dataframe(args, constructor):
16871687
args["wide_variable"] = args["y"] if wide_y else args["x"]
16881688
if df_provided and is_pd_like and args["wide_variable"] is columns:
16891689
var_name = columns.name
1690-
if is_pd_like and isinstance(args["wide_variable"], native_namespace.Index):
1691-
args["wide_variable"] = list(args["wide_variable"])
1690+
# copy into a new list so that the object passed by the user for
1691+
# x or y is not mutated when wide_variable's entries are replaced
1692+
args["wide_variable"] = list(args["wide_variable"])
16921693
if var_name in [None, "value", "index"] or (
16931694
df_provided and var_name in columns
16941695
):

‎tests/test_optional/test_px/test_px_wide.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,13 @@ def test_no_pd_perf_warning():
890890
if issubclass(warn.category, pd.errors.PerformanceWarning)
891891
]
892892
assert len(performance_warnings) == 0, "PerformanceWarning(s) raised!"
893+
894+
895+
def test_wide_mode_does_not_mutate_x_or_y():
896+
# https://github.com/plotly/plotly.py/issues/4117
897+
# integer column names, as in the issue: string names are a no-op here
898+
df = pd.DataFrame([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
899+
for arg in ["x", "y"]:
900+
cols = [0, 1]
901+
px.bar(df, **{arg: cols})
902+
assert cols == [0, 1]

0 commit comments

Comments
 (0)