Plotly Express constructor: in wide mode, lists passed to `x` and `y` params are mutated
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 18.8k
- Forks
- 2.8k
- Avg merge
- 16h 26m
- Merged PRs (30d)
- 21
Description
When either the x or y parameters to the Plotly Express constructor are passed lists of values, they are mutated such that values are converted to string.
Expected behaviour: Plotly Express should not mutate objects supplied as arguments by the user.
Minimal example (tested on Plotly 5.13.1 and master branch):
from random import randrange
import pandas as pd
import plotly.express as px
cols, rows = list(range(3)), list(range(10))
print(cols)
df = pd.DataFrame({col: [randrange(100) for _row in rows] for col in cols})
px.bar(df, y=cols, barmode="group")
print(cols)
Before the px.bar() call, cols has the value [1, 2, 3] and afterwards it is ["1", "2", "3"]. The same behaviour occurs for the x param and also when using other plotting functions than pxbar() (I tried px.histogram()).
I did some poking around, and can see that in the function build_dataframe in plotly/express/_core.py, the some of the fields in the user supplied args have copies made (which would fix this issue), but that is only applied to args found in the array_attrables list, which is ["dimensions", "custom_data", "hover_data", "path", "wide_variable"], which does not include x and y.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in plotly/express/_core.py and read build_dataframe, especially how array_attrables controls copying of user-supplied arguments. Reproduce the minimal example with wide-mode x or y lists, then verify that the original lists retain their values and are not converted to strings after the constructor call.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100