Skip to content

fix: Skip base64 conversion for empty arrays (fixes #4919) #4922

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 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion packages/python/plotly/_plotly_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def to_typed_array_spec(v):
"""
v = copy_to_readonly_numpy_array(v)

# Skip b64 encoding if numpy is not installed,
# or if v is not a numpy array, or if v is empty
np = get_module("numpy", should_load=False)
if not np or not isinstance(v, np.ndarray):
if not np or not isinstance(v, np.ndarray) or v.size == 0:
return v

dtype = str(v.dtype)
Expand Down
14 changes: 14 additions & 0 deletions packages/python/plotly/plotly/tests/test_io/test_to_from_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import plotly.io as pio
import pytest
import plotly
import numpy as np
import json
import os
import tempfile
Expand Down Expand Up @@ -259,3 +260,16 @@ def test_write_json_from_file_string(fig1, pretty, remove_uids):
# Check contents that were written
expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
assert result == expected


def test_to_dict_empty_np_array_int64():
fig = go.Figure(
[
go.Bar(
x=np.array([], dtype="str"),
y=np.array([], dtype="int64"),
)
]
)
# to_dict() should not raise an exception
fig.to_dict()
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,18 @@ def test_render_mode(backend):
)
assert fig.data[0].type == "histogram2dcontour"
assert fig.data[1].type == "scatter"


def test_empty_df_int64(backend):
# Load px data, then filter it such that the dataframe is empty
df = px.data.tips(return_type=backend)
df = nw.from_native(px.data.tips(return_type=backend))
df_empty = df.filter(nw.col("day") == "banana").to_native()

fig = px.scatter(
df_empty,
x="total_bill",
y="size", # size is an int64 column
)
# to_dict() should not raise an exception
fig.to_dict()