2828# interface to/from
2929def to_json (path_or_buf , obj , orient = None , date_format = 'epoch' ,
3030 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'" )
3237
3338 path_or_buf = _stringify_path (path_or_buf )
3439 if lines and orient != 'records' :
@@ -49,7 +54,8 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
4954 s = writer (
5055 obj , orient = orient , date_format = date_format ,
5156 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 ()
5359
5460 if lines :
5561 s = _convert_to_line_delimits (s )
@@ -69,7 +75,7 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
6975class Writer (object ):
7076
7177 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 ):
7379 self .obj = obj
7480
7581 if orient is None :
@@ -81,6 +87,7 @@ def __init__(self, obj, orient, date_format, double_precision,
8187 self .ensure_ascii = ensure_ascii
8288 self .date_unit = date_unit
8389 self .default_handler = default_handler
90+ self .index = index
8491
8592 self .is_copy = None
8693 self ._format_axes ()
@@ -108,6 +115,19 @@ def _format_axes(self):
108115 raise ValueError ("Series index must be unique for orient="
109116 "'{orient}'" .format (orient = self .orient ))
110117
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+
111131
112132class FrameWriter (Writer ):
113133 _default_orient = 'columns'
@@ -123,12 +143,26 @@ def _format_axes(self):
123143 raise ValueError ("DataFrame columns must be unique for orient="
124144 "'{orient}'." .format (orient = self .orient ))
125145
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+
126160
127161class JSONTableWriter (FrameWriter ):
128162 _default_orient = 'records'
129163
130164 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 ):
132166 """
133167 Adds a `schema` attribut with the Table Schema, resets
134168 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,
137171 """
138172 super (JSONTableWriter , self ).__init__ (
139173 obj , orient , date_format , double_precision , ensure_ascii ,
140- date_unit , default_handler = default_handler )
174+ date_unit , index , default_handler = default_handler )
141175
142176 if date_format != 'iso' :
143177 msg = ("Trying to write with `orient='table'` and "
@@ -146,7 +180,7 @@ def __init__(self, obj, orient, date_format, double_precision,
146180 .format (fmt = date_format ))
147181 raise ValueError (msg )
148182
149- self .schema = build_table_schema (obj )
183+ self .schema = build_table_schema (obj , index = self . index )
150184
151185 # NotImplementd on a column MultiIndex
152186 if obj .ndim == 2 and isinstance (obj .columns , MultiIndex ):
@@ -173,7 +207,17 @@ def __init__(self, obj, orient, date_format, double_precision,
173207 self .orient = 'records'
174208
175209 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+ )
177221 serialized = '{{"schema": {schema}, "data": {data}}}' .format (
178222 schema = dumps (self .schema ), data = data )
179223 return serialized
0 commit comments