Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bff27b8

Browse files
committed
Test json encoding for NaN, pd.NaT, pd.Index
1 parent 6d9e8ca commit bff27b8

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313

1414
## JSON encoding
1515
numeric_list = [1, 2, 3]
16-
np_list = np.array([1, 2, 3])
17-
mixed_list = [1, 'A', dt(2014, 1, 5)]
18-
pd = pd.DataFrame(columns=['col 1'], data=[1, 2, 3])
16+
np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])
17+
mixed_list = [1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
18+
dt(2014, 1, 5, 1, 1, 1, 1)]
19+
20+
df = pd.DataFrame(columns=['col 1'],
21+
data=[1, 2, 3, dt(2014, 1, 5), pd.NaT, np.NaN, np.Inf])
22+
23+
rng = pd.date_range('1/1/2011', periods=2, freq='H')
24+
ts = pd.Series([1.5, 2.5], index=rng)
1925

2026

2127
def test_column_json_encoding():
@@ -28,21 +34,47 @@ def test_column_json_encoding():
2834
columns, cls=utils._plotlyJSONEncoder, sort_keys=True
2935
)
3036
assert('[{"data": [1, 2, 3], "name": "col 1"}, '
31-
'{"data": [1, "A", "2014-01-05"], "name": "col 2"}, '
32-
'{"data": [1, 2, 3], "name": "col 3"}]' == json_columns)
37+
'{"data": [1, "A", "2014-01-05", '
38+
'"2014-01-05 01:01:01", '
39+
'"2014-01-05 01:01:01.000001"], '
40+
'"name": "col 2"}, '
41+
'{"data": [1, 2, 3, NaN, NaN, Infinity, '
42+
'"2014-01-05"], "name": "col 3"}]' == json_columns)
3343

3444

3545
def test_figure_json_encoding():
46+
df = pd.DataFrame(columns=['col 1'], data=[1, 2, 3])
3647
s1 = Scatter3d(x=numeric_list, y=np_list, z=mixed_list)
37-
s2 = Scatter(x=pd['col 1'])
48+
s2 = Scatter(x=df['col 1'])
3849
data = Data([s1, s2])
3950
figure = Figure(data=data)
4051

4152
js1 = json.dumps(s1, cls=utils._plotlyJSONEncoder, sort_keys=True)
4253
js2 = json.dumps(s2, cls=utils._plotlyJSONEncoder, sort_keys=True)
4354

44-
assert(js1 == '{"type": "scatter3d", "x": [1, 2, 3], '+
45-
'"y": [1, 2, 3], "z": [1, "A", "2014-01-05"]}')
55+
assert(js1 == '{"type": "scatter3d", "x": [1, 2, 3], '
56+
'"y": [1, 2, 3, NaN, NaN, Infinity, "2014-01-05"], '
57+
'"z": [1, "A", "2014-01-05", '
58+
'"2014-01-05 01:01:01", "2014-01-05 01:01:01.000001"]}')
4659
assert(js2 == '{"type": "scatter", "x": [1, 2, 3]}')
4760
json.dumps(data, cls=utils._plotlyJSONEncoder, sort_keys=True)
4861
json.dumps(figure, cls=utils._plotlyJSONEncoder, sort_keys=True)
62+
63+
64+
def test_pandas_json_encoding():
65+
j1 = json.dumps(df['col 1'], cls=utils._plotlyJSONEncoder)
66+
assert(j1 == '[1, 2, 3, "2014-01-05", null, NaN, Infinity]')
67+
j2 = json.dumps(df.index, cls=utils._plotlyJSONEncoder)
68+
assert(j2 == '[0, 1, 2, 3, 4, 5, 6]')
69+
70+
j3 = json.dumps([pd.NaT], cls=utils._plotlyJSONEncoder)
71+
assert(j3 == '[null]')
72+
73+
j4 = json.dumps(rng, cls=utils._plotlyJSONEncoder)
74+
assert(j4 == '["2011-01-01", "2011-01-01 01:00:00"]')
75+
76+
j5 = json.dumps(ts, cls=utils._plotlyJSONEncoder)
77+
assert(j5 == '[1.5, 2.5]')
78+
79+
j6 = json.dumps(ts.index, cls=utils._plotlyJSONEncoder)
80+
assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')

0 commit comments

Comments
 (0)