Skip to content

Commit 31d55dc

Browse files
committed
Merge pull request #149 from plotly/fix-pandas-serialization
Fix pandas serialization
2 parents b9ebee5 + b1c2880 commit 31d55dc

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __str__(self):
3535
def __repr__(self):
3636
return 'Column("{}", {})'.format(self.data, self.name)
3737

38-
def to_json(self):
38+
def to_plotly_json(self):
3939
return {'name': self.name, 'data': self.data}
4040

4141

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
from datetime import datetime as dt
66
import numpy as np
77
import json
8+
import pandas as pd
89

910
from plotly import utils
1011
from plotly.grid_objs import Column
11-
from plotly.graph_objs import Scatter3d, Figure, Data
12+
from plotly.graph_objs import Scatter, Scatter3d, Figure, Data
1213

1314
## JSON encoding
1415
numeric_list = [1, 2, 3]
1516
np_list = np.array([1, 2, 3])
1617
mixed_list = [1, 'A', dt(2014, 1, 5)]
18+
pd = pd.DataFrame(columns=['col 1'], data=[1, 2, 3])
1719

1820

1921
def test_column_json_encoding():
@@ -31,10 +33,16 @@ def test_column_json_encoding():
3133

3234

3335
def test_figure_json_encoding():
34-
s = Scatter3d(x=numeric_list, y=np_list, z=mixed_list)
35-
data = Data([s])
36+
s1 = Scatter3d(x=numeric_list, y=np_list, z=mixed_list)
37+
s2 = Scatter(x=pd['col 1'])
38+
data = Data([s1, s2])
3639
figure = Figure(data=data)
3740

38-
json.dumps(s, cls=utils._plotlyJSONEncoder)
39-
json.dumps(data, cls=utils._plotlyJSONEncoder)
40-
json.dumps(figure, cls=utils._plotlyJSONEncoder)
41+
js1 = json.dumps(s1, cls=utils._plotlyJSONEncoder, sort_keys=True)
42+
js2 = json.dumps(s2, cls=utils._plotlyJSONEncoder, sort_keys=True)
43+
44+
assert(js1 == '{"type": "scatter3d", "x": [1, 2, 3], '+
45+
'"y": [1, 2, 3], "z": [1, "A", "2014-01-05"]}')
46+
assert(js2 == '{"type": "scatter", "x": [1, 2, 3]}')
47+
json.dumps(data, cls=utils._plotlyJSONEncoder, sort_keys=True)
48+
json.dumps(figure, cls=utils._plotlyJSONEncoder, sort_keys=True)

plotly/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def sageJSONEncoder(self, obj):
128128

129129
def builtinJSONEncoder(self, obj):
130130
try:
131-
return obj.to_json()
131+
return obj.to_plotly_json()
132132
except AttributeError:
133133
return None
134134

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.4.3'
1+
__version__ = '1.4.4'

0 commit comments

Comments
 (0)