Skip to content

numpy.ndarray serialization behavior #5110

@mun-akkio

Description

@mun-akkio

Upgrading to plotly 6.0.1 from 5.24.1, seeing the behavior difference below

import pandas as pd
import plotly
import plotly.express as px
import json

df = pd.DataFrame({"date": ["Apr 2023", "Sep 2023", "Jan 2023", "Dec 2023", "Apr 2023", "Jul 2023", "Jan 2023", "Oct 2023"], "value": [4, 9, 1, 12, 4, 7, 1, 10], "group": ["A", "A", "A", "A", "B", "B", "B", "B"]})
df_grouped = df.groupby(["group", "date"])["value"].sum().reset_index()
fig = px.line(df_grouped, x="date", y="value", color="group")
json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) 

> '{"data":... "xaxis": "x", "y": {"dtype": "i1", "bdata": "BAwBCQ=="}, "yaxis": "y", "type": "scatter" ...}

fig.to_json()

> '{"data":...,"xaxis":"x","y":{"dtype":"i1","bdata":"BAwBCQ==" ...}

In 5.24.1

json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) 

> {"data": ..., "xaxis": "x", "y": [4, 12, 1, 9], ...}

fig.to_json()
> '{"data":...,"xaxis":"x","y":[4,12,1,9], ...}

Note that fig.data[0].y is serialized into {"dtype": "i1", "bdata": "BAwBCQ=="} rather than a list. Is that expected behavior with the new version?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions