Skip to content

Commit 3d8e3b5

Browse files
committed
🔍 digging further into the code
1 parent 4b6661b commit 3d8e3b5

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

plotly_resampler/aggregation/aggregation_interface.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ def aggregate(self, s: pd.Series, n_out: int) -> pd.Series:
152152

153153
self._supports_dtype(s)
154154

155-
# convert the bool values to uint8 (as we will display them on a y-axis)
156-
if str(s.dtype) == "bool":
157-
s = s.astype("uint8")
158-
159155
if len(s) > n_out:
160156
# More samples that n_out -> perform data aggregation
161157
s = self._aggregate(s, n_out=n_out)

plotly_resampler/figure_resampler/figure_resampler_interface.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ def _get_figure_class(constr: type) -> type:
457457
458458
"""
459459
from ..registering import _get_plotly_constr # To avoid ImportError
460+
460461
return _get_plotly_constr(constr)
461462

462463
@staticmethod
@@ -681,8 +682,10 @@ def _parse_get_trace_props(
681682
if hf_y.dtype == "object":
682683
# But first, we try to parse to a numeric dtype (as this is the
683684
# behavior that plotly supports)
685+
# Note that a bool array of type object will remain a bool array (and
686+
# not will be transformed to an array of ints (0, 1))
684687
try:
685-
hf_y = hf_y.astype("float64")
688+
hf_y = pd.to_numeric(hf_y, errors="raise")
686689
except:
687690
hf_y = hf_y.astype("str")
688691

@@ -1102,10 +1105,7 @@ def _copy_hf_data(self, hf_data: dict, adjust_default_values: bool = False) -> d
11021105
11031106
"""
11041107
hf_data_cp = {
1105-
uid: {
1106-
k: hf_dict[k]
1107-
for k in set(hf_dict.keys())
1108-
}
1108+
uid: {k: hf_dict[k] for k in set(hf_dict.keys())}
11091109
for uid, hf_dict in hf_data.items()
11101110
}
11111111

tests/test_figure_resampler.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,3 +882,25 @@ def test_fr_object_hf_data(float_series):
882882
assert float_series_o.dtype == object
883883
assert len(fig.hf_data) == 1
884884
assert fig.hf_data[0]["y"].dtype == "float64"
885+
assert fig.data[0]["y"].dtype == "float64"
886+
887+
888+
def test_fwr_object_bool_data(bool_series):
889+
# First try with the original non-object bool series
890+
fig = FigureResampler()
891+
fig.add_trace({"name": "s0"}, hf_y=bool_series)
892+
assert len(fig.hf_data) == 1
893+
assert fig.hf_data[0]["y"].dtype == "bool"
894+
# plotly internally ocnverts this to object
895+
assert fig.data[0]["y"].dtype == "object"
896+
897+
# Now try with the object bool series
898+
bool_series_o = bool_series.astype(object)
899+
900+
fig = FigureResampler()
901+
fig.add_trace({"name": "s0"}, hf_y=bool_series_o)
902+
assert bool_series_o.dtype == object
903+
assert len(fig.hf_data) == 1
904+
assert fig.hf_data[0]["y"].dtype == "bool"
905+
# plotly internally ocnverts this to object
906+
assert fig.data[0]["y"].dtype == "object"

tests/test_figurewidget_resampler.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ def test_fwr_from_list_dict(float_series):
15961596
assert fr_fig.data[1].uid in fr_fig._hf_data
15971597

15981598
# redo the exercise with a new low-freq trace
1599-
base_fig.append({'y': float_series[:1000], 'name': "s_no_agg"})
1599+
base_fig.append({"y": float_series[:1000], "name": "s_no_agg"})
16001600
fr_fig = FigureWidgetResampler(base_fig, default_n_shown_samples=1000)
16011601
assert len(fr_fig.hf_data) == 2
16021602
assert len(fr_fig.data) == 3
@@ -1626,17 +1626,17 @@ def test_fwr_list_dict_add_trace(float_series):
16261626
assert fr_fig.data[1].uid in fr_fig._hf_data
16271627

16281628
# redo the exercise with a new low-freq trace
1629-
fr_fig.add_trace({'y': float_series[:1000], 'name': "s_no_agg"})
1629+
fr_fig.add_trace({"y": float_series[:1000], "name": "s_no_agg"})
16301630
assert len(fr_fig.hf_data) == 2
16311631
assert len(fr_fig.data) == 3
16321632

16331633
# add low-freq trace but set limit_to_view to True
1634-
fr_fig.add_trace({'y': float_series[:100], 'name': "s_agg"}, limit_to_view=True)
1634+
fr_fig.add_trace({"y": float_series[:100], "name": "s_agg"}, limit_to_view=True)
16351635
assert len(fr_fig.hf_data) == 3
16361636
assert len(fr_fig.data) == 4
16371637

16381638
# add a low-freq trace but adjust max_n_samples
1639-
lf_series = {'y': float_series[:1000], 'name': "s_agg"}
1639+
lf_series = {"y": float_series[:1000], "name": "s_agg"}
16401640
# plotly its default behavior raises a ValueError when a list or tuple is passed
16411641
# to add_trace
16421642
with pytest.raises(ValueError):
@@ -1672,18 +1672,18 @@ def test_fwr_list_dict_add_traces(float_series):
16721672

16731673
# redo the exercise with a new low-freq trace
16741674
# plotly also allows a dict or a scatter object as input
1675-
fr_fig.add_traces({'y': float_series[:1000], 'name': "s_no_agg"})
1675+
fr_fig.add_traces({"y": float_series[:1000], "name": "s_no_agg"})
16761676
assert len(fr_fig.hf_data) == 2
16771677
assert len(fr_fig.data) == 3
16781678

16791679
# add low-freq trace but set limit_to_view to True
1680-
fr_fig.add_traces([{'y': float_series[:100], 'name': "s_agg"}], limit_to_views=True)
1680+
fr_fig.add_traces([{"y": float_series[:100], "name": "s_agg"}], limit_to_views=True)
16811681
assert len(fr_fig.hf_data) == 3
16821682
assert len(fr_fig.data) == 4
16831683

16841684
# add a low-freq trace but adjust max_n_samples
16851685
# note that we use tuple as input
1686-
fr_fig.add_traces(({'y': float_series[:1000], 'name': "s_agg"}, ), max_n_samples=999)
1686+
fr_fig.add_traces(({"y": float_series[:1000], "name": "s_agg"},), max_n_samples=999)
16871687
assert len(fr_fig.hf_data) == 4
16881688
assert len(fr_fig.data) == 5
16891689

@@ -1710,7 +1710,7 @@ def test_fwr_list_scatter_add_traces(float_series):
17101710
assert fr_fig.data[1].uid in fr_fig._hf_data
17111711

17121712
# redo the exercise with a new low-freq trace
1713-
fr_fig.add_traces([go.Scattergl({'y': float_series[:1000], 'name': "s_no_agg"})])
1713+
fr_fig.add_traces([go.Scattergl({"y": float_series[:1000], "name": "s_no_agg"})])
17141714
assert len(fr_fig.hf_data) == 2
17151715
assert len(fr_fig.data) == 3
17161716

@@ -1721,10 +1721,13 @@ def test_fwr_list_scatter_add_traces(float_series):
17211721
assert len(fr_fig.data) == 4
17221722

17231723
# add a low-freq trace but adjust max_n_samples
1724-
fr_fig.add_traces(go.Scatter({'y': float_series[:1000], 'name': "s_agg"}), max_n_samples=999)
1724+
fr_fig.add_traces(
1725+
go.Scatter({"y": float_series[:1000], "name": "s_agg"}), max_n_samples=999
1726+
)
17251727
assert len(fr_fig.hf_data) == 4
17261728
assert len(fr_fig.data) == 5
17271729

1730+
17281731
def test_fwr_add_scatter():
17291732
# Checks whether the add_scatter method works as expected
17301733
# .add_scatter calls `add_traces` under the hood
@@ -1738,11 +1741,35 @@ def test_fwr_add_scatter():
17381741
assert np.all(fw_orig.data[0].y == fw_pr.hf_data[0]["y"])
17391742

17401743

1741-
def test_fwr_object_hf_data(float_series):
1744+
def test_fwr_object_hf_data(
1745+
float_series,
1746+
):
17421747
float_series_o = float_series.astype(object)
17431748

17441749
fig = FigureWidgetResampler()
17451750
fig.add_trace({"name": "s0"}, hf_y=float_series_o)
17461751
assert float_series_o.dtype == object
17471752
assert len(fig.hf_data) == 1
1748-
assert fig.hf_data[0]['y'].dtype == 'float64'
1753+
assert fig.hf_data[0]["y"].dtype == "float64"
1754+
assert fig.data[0]["y"].dtype == "float64"
1755+
1756+
1757+
def test_fwr_object_bool_data(bool_series):
1758+
# First try with the original non-object bool series
1759+
fig = FigureWidgetResampler()
1760+
fig.add_trace({"name": "s0"}, hf_y=bool_series)
1761+
assert len(fig.hf_data) == 1
1762+
assert fig.hf_data[0]["y"].dtype == "bool"
1763+
# plotly internally ocnverts this to object
1764+
assert fig.data[0]["y"].dtype == "object"
1765+
1766+
# Now try with the object bool series
1767+
bool_series_o = bool_series.astype(object)
1768+
1769+
fig = FigureWidgetResampler()
1770+
fig.add_trace({"name": "s0"}, hf_y=bool_series_o)
1771+
assert bool_series_o.dtype == object
1772+
assert len(fig.hf_data) == 1
1773+
assert fig.hf_data[0]["y"].dtype == "bool"
1774+
# plotly internally ocnverts this to object
1775+
assert fig.data[0]["y"].dtype == "object"

0 commit comments

Comments
 (0)