|
6 | 6 | import numpy as np
|
7 | 7 | import json
|
8 | 8 | import pandas as pd
|
| 9 | +from pandas.util.testing import assert_series_equal |
9 | 10 |
|
10 | 11 | from plotly import utils
|
11 | 12 | from plotly.grid_objs import Column
|
@@ -57,24 +58,43 @@ def test_figure_json_encoding():
|
57 | 58 | '"z": [1, "A", "2014-01-05", '
|
58 | 59 | '"2014-01-05 01:01:01", "2014-01-05 01:01:01.000001"]}')
|
59 | 60 | assert(js2 == '{"type": "scatter", "x": [1, 2, 3]}')
|
| 61 | + |
| 62 | + # Test JSON encoding works |
60 | 63 | json.dumps(data, cls=utils._plotlyJSONEncoder, sort_keys=True)
|
61 | 64 | json.dumps(figure, cls=utils._plotlyJSONEncoder, sort_keys=True)
|
62 | 65 |
|
| 66 | + # Test data wasn't mutated |
| 67 | + assert(bool(np.asarray(np_list == |
| 68 | + np.array([1, 2, 3, np.NaN, |
| 69 | + np.NAN, np.Inf, dt(2014, 1, 5)])).all())) |
| 70 | + assert(set(data[0]['z']) == |
| 71 | + set([1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1), |
| 72 | + dt(2014, 1, 5, 1, 1, 1, 1)])) |
| 73 | + |
63 | 74 |
|
64 | 75 | def test_pandas_json_encoding():
|
65 | 76 | j1 = json.dumps(df['col 1'], cls=utils._plotlyJSONEncoder)
|
66 | 77 | assert(j1 == '[1, 2, 3, "2014-01-05", null, NaN, Infinity]')
|
| 78 | + |
| 79 | + # Test that data wasn't mutated |
| 80 | + assert_series_equal(df['col 1'], |
| 81 | + pd.Series([1, 2, 3, dt(2014, 1, 5), |
| 82 | + pd.NaT, np.NaN, np.Inf])) |
| 83 | + |
67 | 84 | j2 = json.dumps(df.index, cls=utils._plotlyJSONEncoder)
|
68 | 85 | assert(j2 == '[0, 1, 2, 3, 4, 5, 6]')
|
69 | 86 |
|
70 |
| - j3 = json.dumps([pd.NaT], cls=utils._plotlyJSONEncoder) |
| 87 | + nat = [pd.NaT] |
| 88 | + j3 = json.dumps(nat, cls=utils._plotlyJSONEncoder) |
71 | 89 | assert(j3 == '[null]')
|
| 90 | + assert(nat[0] is pd.NaT) |
72 | 91 |
|
73 | 92 | j4 = json.dumps(rng, cls=utils._plotlyJSONEncoder)
|
74 | 93 | assert(j4 == '["2011-01-01", "2011-01-01 01:00:00"]')
|
75 | 94 |
|
76 | 95 | j5 = json.dumps(ts, cls=utils._plotlyJSONEncoder)
|
77 | 96 | assert(j5 == '[1.5, 2.5]')
|
| 97 | + assert_series_equal(ts, pd.Series([1.5, 2.5], index=rng)) |
78 | 98 |
|
79 | 99 | j6 = json.dumps(ts.index, cls=utils._plotlyJSONEncoder)
|
80 | 100 | assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')
|
0 commit comments