3
3
from nose .plugins .attrib import attr
4
4
5
5
from datetime import datetime as dt
6
+ import datetime
6
7
import numpy as np
7
8
import json
8
9
import pandas as pd
9
10
from pandas .util .testing import assert_series_equal
11
+ import matplotlib .pyplot as plt
10
12
11
13
from plotly import utils
12
14
from plotly .grid_objs import Column
13
15
from plotly .graph_objs import Scatter , Scatter3d , Figure , Data
16
+ from plotly .matplotlylib import Exporter , PlotlyRenderer
17
+ from plotly .plotly import plot
14
18
15
19
## JSON encoding
16
20
numeric_list = [1 , 2 , 3 ]
17
21
np_list = np .array ([1 , 2 , 3 , np .NaN , np .NAN , np .Inf , dt (2014 , 1 , 5 )])
18
22
mixed_list = [1 , 'A' , dt (2014 , 1 , 5 ), dt (2014 , 1 , 5 , 1 , 1 , 1 ),
19
23
dt (2014 , 1 , 5 , 1 , 1 , 1 , 1 )]
24
+ dt_list = [dt (2014 , 1 , 5 ), dt (2014 , 1 , 5 , 1 , 1 , 1 ),
25
+ dt (2014 , 1 , 5 , 1 , 1 , 1 , 1 )]
20
26
21
27
df = pd .DataFrame (columns = ['col 1' ],
22
28
data = [1 , 2 , 3 , dt (2014 , 1 , 5 ), pd .NaT , np .NaN , np .Inf ])
@@ -72,6 +78,17 @@ def test_figure_json_encoding():
72
78
dt (2014 , 1 , 5 , 1 , 1 , 1 , 1 )]))
73
79
74
80
81
+ def test_datetime_json_encoding ():
82
+ j1 = json .dumps (dt_list , cls = utils ._plotlyJSONEncoder )
83
+ assert (j1 == '["2014-01-05", '
84
+ '"2014-01-05 01:01:01", '
85
+ '"2014-01-05 01:01:01.000001"]' )
86
+ j2 = json .dumps ({"x" : dt_list }, cls = utils ._plotlyJSONEncoder )
87
+ assert (j2 == '{"x": ["2014-01-05", '
88
+ '"2014-01-05 01:01:01", '
89
+ '"2014-01-05 01:01:01.000001"]}' )
90
+
91
+
75
92
def test_pandas_json_encoding ():
76
93
j1 = json .dumps (df ['col 1' ], cls = utils ._plotlyJSONEncoder )
77
94
assert (j1 == '[1, 2, 3, "2014-01-05", null, NaN, Infinity]' )
@@ -98,3 +115,50 @@ def test_pandas_json_encoding():
98
115
99
116
j6 = json .dumps (ts .index , cls = utils ._plotlyJSONEncoder )
100
117
assert (j6 == '["2011-01-01", "2011-01-01 01:00:00"]' )
118
+
119
+
120
+ def test_numpy_masked_json_encoding ():
121
+ l = [1 , 2 , np .ma .core .masked ]
122
+ j1 = json .dumps (l , cls = utils ._plotlyJSONEncoder )
123
+ assert (j1 == '[1, 2, NaN]' )
124
+ assert (set (l ) == set ([1 , 2 , np .ma .core .masked ]))
125
+
126
+
127
+ def test_masked_constants_example ():
128
+ # example from: https://gist.github.com/tschaume/d123d56bf586276adb98
129
+ data = {
130
+ 'esN' : [0 , 1 , 2 , 3 ],
131
+ 'ewe_is0' : [- 398.11901997 , - 398.11902774 ,
132
+ - 398.11897111 , - 398.11882215 ],
133
+ 'ewe_is1' : [- 398.11793027 , - 398.11792966 , - 398.11786308 , None ],
134
+ 'ewe_is2' : [- 398.11397008 , - 398.11396421 , None , None ]
135
+ }
136
+ df = pd .DataFrame .from_dict (data )
137
+
138
+ plotopts = {'x' : 'esN' , 'marker' : 'o' }
139
+ fig , ax = plt .subplots (1 , 1 )
140
+ df .plot (ax = ax , ** plotopts )
141
+
142
+ renderer = PlotlyRenderer ()
143
+ Exporter (renderer ).run (fig )
144
+
145
+ json .dumps (renderer .plotly_fig , cls = utils ._plotlyJSONEncoder )
146
+
147
+ jy = json .dumps (renderer .plotly_fig ['data' ][1 ]['y' ],
148
+ cls = utils ._plotlyJSONEncoder )
149
+ assert (jy == '[-398.11793026999999, -398.11792966000002, '
150
+ '-398.11786308000001, NaN]' )
151
+
152
+
153
+ def test_numpy_dates ():
154
+ a = np .arange (np .datetime64 ('2011-07-11' ), np .datetime64 ('2011-07-18' ))
155
+ j1 = json .dumps (a , cls = utils ._plotlyJSONEncoder )
156
+ assert (j1 == '["2011-07-11", "2011-07-12", "2011-07-13", '
157
+ '"2011-07-14", "2011-07-15", "2011-07-16", '
158
+ '"2011-07-17"]' )
159
+
160
+
161
+ def test_datetime_dot_date ():
162
+ a = [datetime .date (2014 , 1 , 1 ), datetime .date (2014 , 1 , 2 )]
163
+ j1 = json .dumps (a , cls = utils ._plotlyJSONEncoder )
164
+ assert (j1 == '["2014-01-01", "2014-01-02"]' )
0 commit comments