Skip to content

Commit 43ac339

Browse files
authored
Merge pull request plotly#4922 from plotly/fix-4919
fix: Skip base64 conversion for empty arrays (fixes plotly#4919)
2 parents 8f50e2b + f7675c3 commit 43ac339

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

packages/python/plotly/_plotly_utils/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ def to_typed_array_spec(v):
4343
"""
4444
v = copy_to_readonly_numpy_array(v)
4545

46+
# Skip b64 encoding if numpy is not installed,
47+
# or if v is not a numpy array, or if v is empty
4648
np = get_module("numpy", should_load=False)
47-
if not np or not isinstance(v, np.ndarray):
49+
if not np or not isinstance(v, np.ndarray) or v.size == 0:
4850
return v
4951

5052
dtype = str(v.dtype)

packages/python/plotly/plotly/tests/test_io/test_to_from_json.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import plotly.io as pio
33
import pytest
44
import plotly
5+
import numpy as np
56
import json
67
import os
78
import tempfile
@@ -259,3 +260,16 @@ def test_write_json_from_file_string(fig1, pretty, remove_uids):
259260
# Check contents that were written
260261
expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
261262
assert result == expected
263+
264+
265+
def test_to_dict_empty_np_array_int64():
266+
fig = go.Figure(
267+
[
268+
go.Bar(
269+
x=np.array([], dtype="str"),
270+
y=np.array([], dtype="int64"),
271+
)
272+
]
273+
)
274+
# to_dict() should not raise an exception
275+
fig.to_dict()

packages/python/plotly/plotly/tests/test_optional/test_px/test_px.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,18 @@ def test_render_mode(backend):
360360
)
361361
assert fig.data[0].type == "histogram2dcontour"
362362
assert fig.data[1].type == "scatter"
363+
364+
365+
def test_empty_df_int64(backend):
366+
# Load px data, then filter it such that the dataframe is empty
367+
df = px.data.tips(return_type=backend)
368+
df = nw.from_native(px.data.tips(return_type=backend))
369+
df_empty = df.filter(nw.col("day") == "banana").to_native()
370+
371+
fig = px.scatter(
372+
df_empty,
373+
x="total_bill",
374+
y="size", # size is an int64 column
375+
)
376+
# to_dict() should not raise an exception
377+
fig.to_dict()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy