Skip to content

Commit 2d23b43

Browse files
committed
masked constants test
1 parent 6ba5371 commit 2d23b43

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@
77
import json
88
import pandas as pd
99
from pandas.util.testing import assert_series_equal
10+
import matplotlib.pyplot as plt
1011

1112
from plotly import utils
1213
from plotly.grid_objs import Column
1314
from plotly.graph_objs import Scatter, Scatter3d, Figure, Data
15+
from plotly.matplotlylib import Exporter, PlotlyRenderer
16+
from plotly.plotly import plot
1417

1518
## JSON encoding
1619
numeric_list = [1, 2, 3]
1720
np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])
1821
mixed_list = [1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
1922
dt(2014, 1, 5, 1, 1, 1, 1)]
23+
dt_list = [dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
24+
dt(2014, 1, 5, 1, 1, 1, 1)]
2025

2126
df = pd.DataFrame(columns=['col 1'],
2227
data=[1, 2, 3, dt(2014, 1, 5), pd.NaT, np.NaN, np.Inf])
@@ -98,3 +103,36 @@ def test_pandas_json_encoding():
98103

99104
j6 = json.dumps(ts.index, cls=utils._plotlyJSONEncoder)
100105
assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')
106+
107+
108+
def test_numpy_masked_json_encoding():
109+
l = [1, 2, np.ma.core.masked]
110+
j1 = json.dumps(l, cls=utils._plotlyJSONEncoder)
111+
assert(j1 == '[1, 2, NaN]')
112+
assert(set(l) == set([1, 2, np.ma.core.masked]))
113+
114+
115+
def test_masked_constants_example():
116+
# example from: https://gist.github.com/tschaume/d123d56bf586276adb98
117+
data = {
118+
'esN': [0, 1, 2, 3],
119+
'ewe_is0': [-398.11901997, -398.11902774,
120+
-398.11897111, -398.11882215],
121+
'ewe_is1': [-398.11793027, -398.11792966, -398.11786308, None],
122+
'ewe_is2': [-398.11397008, -398.11396421, None, None]
123+
}
124+
df = pd.DataFrame.from_dict(data)
125+
126+
plotopts = {'x': 'esN', 'marker': 'o'}
127+
fig, ax = plt.subplots(1, 1)
128+
df.plot(ax=ax, **plotopts)
129+
130+
renderer = PlotlyRenderer()
131+
Exporter(renderer).run(fig)
132+
133+
json.dumps(renderer.plotly_fig, cls=utils._plotlyJSONEncoder)
134+
135+
jy = json.dumps(renderer.plotly_fig['data'][1]['y'],
136+
cls=utils._plotlyJSONEncoder)
137+
assert(jy == '[-398.11793026999999, -398.11792966000002, '
138+
'-398.11786308000001, NaN]')

0 commit comments

Comments
 (0)