@@ -257,6 +257,61 @@ def test_pandas_json_encoding(self):
257
257
j6 = _json .dumps (ts .index , cls = utils .PlotlyJSONEncoder )
258
258
assert j6 == '["2011-01-01T00:00:00", "2011-01-01T01:00:00"]'
259
259
260
+ def test_encode_customdata_datetime_series (self ):
261
+ df = pd .DataFrame (dict (t = pd .to_datetime (["2010-01-01" , "2010-01-02" ])))
262
+
263
+ # 1D customdata
264
+ fig = Figure (
265
+ Scatter (x = df ["t" ], customdata = df ["t" ]), layout = dict (template = "none" )
266
+ )
267
+ fig_json = _json .dumps (
268
+ fig , cls = utils .PlotlyJSONEncoder , separators = ("," , ":" ), sort_keys = True
269
+ )
270
+ self .assertTrue (
271
+ fig_json .startswith (
272
+ '{"data":[{"customdata":["2010-01-01T00:00:00","2010-01-02T00:00:00"]'
273
+ )
274
+ )
275
+
276
+ def test_encode_customdata_datetime_homogenous_dataframe (self ):
277
+ df = pd .DataFrame (dict (
278
+ t1 = pd .to_datetime (["2010-01-01" , "2010-01-02" ]),
279
+ t2 = pd .to_datetime (["2011-01-01" , "2011-01-02" ]),
280
+ ))
281
+ # 2D customdata
282
+ fig = Figure (
283
+ Scatter (x = df ["t1" ], customdata = df [["t1" , "t2" ]]), layout = dict (template = "none" )
284
+ )
285
+ fig_json = _json .dumps (
286
+ fig , cls = utils .PlotlyJSONEncoder , separators = ("," , ":" ), sort_keys = True
287
+ )
288
+ self .assertTrue (
289
+ fig_json .startswith (
290
+ '{"data":[{"customdata":'
291
+ '[["2010-01-01T00:00:00","2011-01-01T00:00:00"],'
292
+ '["2010-01-02T00:00:00","2011-01-02T00:00:00"]'
293
+ )
294
+ )
295
+
296
+ def test_encode_customdata_datetime_inhomogenous_dataframe (self ):
297
+ df = pd .DataFrame (dict (
298
+ t = pd .to_datetime (["2010-01-01" , "2010-01-02" ]),
299
+ v = np .arange (2 ),
300
+ ))
301
+ # 2D customdata
302
+ fig = Figure (
303
+ Scatter (x = df ["t" ], customdata = df [["t" , "v" ]]), layout = dict (template = "none" )
304
+ )
305
+ fig_json = _json .dumps (
306
+ fig , cls = utils .PlotlyJSONEncoder , separators = ("," , ":" ), sort_keys = True
307
+ )
308
+ self .assertTrue (
309
+ fig_json .startswith (
310
+ '{"data":[{"customdata":'
311
+ '[["2010-01-01T00:00:00",0],["2010-01-02T00:00:00",1]]'
312
+ )
313
+ )
314
+
260
315
def test_numpy_masked_json_encoding (self ):
261
316
l = [1 , 2 , np .ma .core .masked ]
262
317
j1 = _json .dumps (l , cls = utils .PlotlyJSONEncoder )
0 commit comments