28
28
# interface to/from
29
29
def to_json (path_or_buf , obj , orient = None , date_format = 'epoch' ,
30
30
double_precision = 10 , force_ascii = True , date_unit = 'ms' ,
31
- default_handler = None , lines = False , compression = None ):
31
+ default_handler = None , lines = False , compression = None ,
32
+ index = True ):
33
+
34
+ if not index and orient not in ['split' , 'table' ]:
35
+ raise ValueError ("'index=False' is only valid when 'orient' is "
36
+ "'split' or 'table'" )
32
37
33
38
path_or_buf = _stringify_path (path_or_buf )
34
39
if lines and orient != 'records' :
@@ -49,7 +54,8 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
49
54
s = writer (
50
55
obj , orient = orient , date_format = date_format ,
51
56
double_precision = double_precision , ensure_ascii = force_ascii ,
52
- date_unit = date_unit , default_handler = default_handler ).write ()
57
+ date_unit = date_unit , default_handler = default_handler ,
58
+ index = index ).write ()
53
59
54
60
if lines :
55
61
s = _convert_to_line_delimits (s )
@@ -69,7 +75,7 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
69
75
class Writer (object ):
70
76
71
77
def __init__ (self , obj , orient , date_format , double_precision ,
72
- ensure_ascii , date_unit , default_handler = None ):
78
+ ensure_ascii , date_unit , index , default_handler = None ):
73
79
self .obj = obj
74
80
75
81
if orient is None :
@@ -81,6 +87,7 @@ def __init__(self, obj, orient, date_format, double_precision,
81
87
self .ensure_ascii = ensure_ascii
82
88
self .date_unit = date_unit
83
89
self .default_handler = default_handler
90
+ self .index = index
84
91
85
92
self .is_copy = None
86
93
self ._format_axes ()
@@ -108,6 +115,19 @@ def _format_axes(self):
108
115
raise ValueError ("Series index must be unique for orient="
109
116
"'{orient}'" .format (orient = self .orient ))
110
117
118
+ def write (self ):
119
+ if not self .index and self .orient == 'split' :
120
+ self .obj = {"name" : self .obj .name , "data" : self .obj .values }
121
+ return dumps (
122
+ self .obj ,
123
+ orient = self .orient ,
124
+ double_precision = self .double_precision ,
125
+ ensure_ascii = self .ensure_ascii ,
126
+ date_unit = self .date_unit ,
127
+ iso_dates = self .date_format == 'iso' ,
128
+ default_handler = self .default_handler
129
+ )
130
+
111
131
112
132
class FrameWriter (Writer ):
113
133
_default_orient = 'columns'
@@ -123,12 +143,26 @@ def _format_axes(self):
123
143
raise ValueError ("DataFrame columns must be unique for orient="
124
144
"'{orient}'." .format (orient = self .orient ))
125
145
146
+ def write (self ):
147
+ if not self .index and self .orient == 'split' :
148
+ self .obj = self .obj .to_dict (orient = 'split' )
149
+ del self .obj ["index" ]
150
+ return dumps (
151
+ self .obj ,
152
+ orient = self .orient ,
153
+ double_precision = self .double_precision ,
154
+ ensure_ascii = self .ensure_ascii ,
155
+ date_unit = self .date_unit ,
156
+ iso_dates = self .date_format == 'iso' ,
157
+ default_handler = self .default_handler
158
+ )
159
+
126
160
127
161
class JSONTableWriter (FrameWriter ):
128
162
_default_orient = 'records'
129
163
130
164
def __init__ (self , obj , orient , date_format , double_precision ,
131
- ensure_ascii , date_unit , default_handler = None ):
165
+ ensure_ascii , date_unit , index , default_handler = None ):
132
166
"""
133
167
Adds a `schema` attribut with the Table Schema, resets
134
168
the index (can't do in caller, because the schema inference needs
@@ -137,7 +171,7 @@ def __init__(self, obj, orient, date_format, double_precision,
137
171
"""
138
172
super (JSONTableWriter , self ).__init__ (
139
173
obj , orient , date_format , double_precision , ensure_ascii ,
140
- date_unit , default_handler = default_handler )
174
+ date_unit , index , default_handler = default_handler )
141
175
142
176
if date_format != 'iso' :
143
177
msg = ("Trying to write with `orient='table'` and "
@@ -146,7 +180,7 @@ def __init__(self, obj, orient, date_format, double_precision,
146
180
.format (fmt = date_format ))
147
181
raise ValueError (msg )
148
182
149
- self .schema = build_table_schema (obj )
183
+ self .schema = build_table_schema (obj , index = self . index )
150
184
151
185
# NotImplementd on a column MultiIndex
152
186
if obj .ndim == 2 and isinstance (obj .columns , MultiIndex ):
@@ -173,7 +207,17 @@ def __init__(self, obj, orient, date_format, double_precision,
173
207
self .orient = 'records'
174
208
175
209
def write (self ):
176
- data = super (JSONTableWriter , self ).write ()
210
+ if not self .index :
211
+ self .obj = self .obj .drop ('index' , axis = 1 )
212
+ data = dumps (
213
+ self .obj ,
214
+ orient = self .orient ,
215
+ double_precision = self .double_precision ,
216
+ ensure_ascii = self .ensure_ascii ,
217
+ date_unit = self .date_unit ,
218
+ iso_dates = self .date_format == 'iso' ,
219
+ default_handler = self .default_handler
220
+ )
177
221
serialized = '{{"schema": {schema}, "data": {data}}}' .format (
178
222
schema = dumps (self .schema ), data = data )
179
223
return serialized
0 commit comments