13
13
14
14
## JSON encoding
15
15
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 )
19
25
20
26
21
27
def test_column_json_encoding ():
@@ -28,21 +34,47 @@ def test_column_json_encoding():
28
34
columns , cls = utils ._plotlyJSONEncoder , sort_keys = True
29
35
)
30
36
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 )
33
43
34
44
35
45
def test_figure_json_encoding ():
46
+ df = pd .DataFrame (columns = ['col 1' ], data = [1 , 2 , 3 ])
36
47
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' ])
38
49
data = Data ([s1 , s2 ])
39
50
figure = Figure (data = data )
40
51
41
52
js1 = json .dumps (s1 , cls = utils ._plotlyJSONEncoder , sort_keys = True )
42
53
js2 = json .dumps (s2 , cls = utils ._plotlyJSONEncoder , sort_keys = True )
43
54
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"]}' )
46
59
assert (js2 == '{"type": "scatter", "x": [1, 2, 3]}' )
47
60
json .dumps (data , cls = utils ._plotlyJSONEncoder , sort_keys = True )
48
61
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