16
16
### interface to/from ###
17
17
18
18
def to_json (path_or_buf , obj , orient = None , date_format = 'epoch' , double_precision = 10 , force_ascii = True ):
19
-
19
+
20
20
if isinstance (obj , Series ):
21
- s = SeriesWriter (obj , orient = orient , date_format = date_format , double_precision = double_precision ,
21
+ s = SeriesWriter (obj , orient = orient , date_format = date_format , double_precision = double_precision ,
22
22
ensure_ascii = force_ascii ).write ()
23
23
elif isinstance (obj , DataFrame ):
24
24
s = FrameWriter (obj , orient = orient , date_format = date_format , double_precision = double_precision ,
@@ -41,7 +41,7 @@ def __init__(self, obj, orient, date_format, double_precision, ensure_ascii):
41
41
42
42
if orient is None :
43
43
orient = self ._default_orient
44
-
44
+
45
45
self .orient = orient
46
46
self .date_format = date_format
47
47
self .double_precision = double_precision
@@ -64,7 +64,7 @@ def _format_to_date(self, data):
64
64
if self ._needs_to_date (data ):
65
65
return data .apply (lambda x : x .isoformat ())
66
66
return data
67
-
67
+
68
68
def copy_if_needed (self ):
69
69
""" copy myself if necessary """
70
70
if not self .is_copy :
@@ -119,7 +119,8 @@ def _format_dates(self):
119
119
self .obj [c ] = self ._format_to_date (self .obj [c ])
120
120
121
121
def read_json (path_or_buf = None , orient = None , typ = 'frame' , dtype = True ,
122
- convert_axes = True , convert_dates = True , keep_default_dates = True , numpy = False ):
122
+ convert_axes = True , convert_dates = True , keep_default_dates = True ,
123
+ numpy = False , precise_float = False ):
123
124
"""
124
125
Convert JSON string to pandas object
125
126
@@ -154,8 +155,10 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
154
155
default is True
155
156
keep_default_dates : boolean, default True. If parsing dates,
156
157
then parse the default datelike columns
157
- numpy: direct decoding to numpy arrays. default is False.Note that the JSON ordering MUST be the same
158
+ numpy : direct decoding to numpy arrays. default is False.Note that the JSON ordering MUST be the same
158
159
for each term if numpy=True.
160
+ precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function
161
+ when decoding string to double values. Default (False) is to use fast but less precise builtin functionality
159
162
160
163
Returns
161
164
-------
@@ -186,28 +189,31 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
186
189
return obj
187
190
188
191
class Parser (object ):
189
-
190
- def __init__ (self , json , orient , dtype = True , convert_axes = True , convert_dates = True , keep_default_dates = False , numpy = False ):
192
+
193
+ def __init__ (self , json , orient , dtype = True , convert_axes = True ,
194
+ convert_dates = True , keep_default_dates = False , numpy = False ,
195
+ precise_float = False ):
191
196
self .json = json
192
197
193
198
if orient is None :
194
199
orient = self ._default_orient
195
-
200
+
196
201
self .orient = orient
197
202
self .dtype = dtype
198
203
199
204
if orient == "split" :
200
205
numpy = False
201
206
202
207
self .numpy = numpy
208
+ self .precise_float = precise_float
203
209
self .convert_axes = convert_axes
204
210
self .convert_dates = convert_dates
205
211
self .keep_default_dates = keep_default_dates
206
212
self .obj = None
207
213
208
214
def parse (self ):
209
215
210
- # try numpy
216
+ # try numpy
211
217
numpy = self .numpy
212
218
if numpy :
213
219
self ._parse_numpy ()
@@ -269,7 +275,7 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
269
275
pass
270
276
271
277
if data .dtype == 'float' :
272
-
278
+
273
279
# coerce floats to 64
274
280
try :
275
281
data = data .astype ('float64' )
@@ -291,7 +297,7 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
291
297
292
298
# coerce ints to 64
293
299
if data .dtype == 'int' :
294
-
300
+
295
301
# coerce floats to 64
296
302
try :
297
303
data = data .astype ('int64' )
@@ -322,7 +328,7 @@ def _try_convert_to_date(self, data):
322
328
if issubclass (new_data .dtype .type ,np .number ):
323
329
if not ((new_data == iNaT ) | (new_data > 31536000000000000L )).all ():
324
330
return data , False
325
-
331
+
326
332
try :
327
333
new_data = to_datetime (new_data )
328
334
except :
@@ -342,29 +348,35 @@ class SeriesParser(Parser):
342
348
_default_orient = 'index'
343
349
344
350
def _parse_no_numpy (self ):
345
-
351
+
346
352
json = self .json
347
353
orient = self .orient
348
354
if orient == "split" :
349
355
decoded = dict ((str (k ), v )
350
- for k , v in loads (json ).iteritems ())
356
+ for k , v in loads (
357
+ json ,
358
+ precise_float = self .precise_float ).iteritems ())
351
359
self .obj = Series (dtype = None , ** decoded )
352
360
else :
353
- self .obj = Series (loads (json ), dtype = None )
361
+ self .obj = Series (
362
+ loads (json , precise_float = self .precise_float ), dtype = None )
354
363
355
364
def _parse_numpy (self ):
356
365
357
366
json = self .json
358
367
orient = self .orient
359
368
if orient == "split" :
360
- decoded = loads (json , dtype = None , numpy = True )
369
+ decoded = loads (json , dtype = None , numpy = True ,
370
+ precise_float = self .precise_float )
361
371
decoded = dict ((str (k ), v ) for k , v in decoded .iteritems ())
362
372
self .obj = Series (** decoded )
363
373
elif orient == "columns" or orient == "index" :
364
374
self .obj = Series (* loads (json , dtype = None , numpy = True ,
365
- labelled = True ))
375
+ labelled = True ,
376
+ precise_float = self .precise_float ))
366
377
else :
367
- self .obj = Series (loads (json , dtype = None , numpy = True ))
378
+ self .obj = Series (loads (json , dtype = None , numpy = True ,
379
+ precise_float = self .precise_float ))
368
380
369
381
def _try_convert_types (self ):
370
382
if self .obj is None : return
@@ -381,34 +393,43 @@ def _parse_numpy(self):
381
393
orient = self .orient
382
394
383
395
if orient == "columns" :
384
- args = loads (json , dtype = None , numpy = True , labelled = True )
396
+ args = loads (json , dtype = None , numpy = True , labelled = True ,
397
+ precise_float = self .precise_float )
385
398
if args :
386
399
args = (args [0 ].T , args [2 ], args [1 ])
387
400
self .obj = DataFrame (* args )
388
401
elif orient == "split" :
389
- decoded = loads (json , dtype = None , numpy = True )
402
+ decoded = loads (json , dtype = None , numpy = True ,
403
+ precise_float = self .precise_float )
390
404
decoded = dict ((str (k ), v ) for k , v in decoded .iteritems ())
391
405
self .obj = DataFrame (** decoded )
392
406
elif orient == "values" :
393
- self .obj = DataFrame (loads (json , dtype = None , numpy = True ))
407
+ self .obj = DataFrame (loads (json , dtype = None , numpy = True ,
408
+ precise_float = self .precise_float ))
394
409
else :
395
- self .obj = DataFrame (* loads (json , dtype = None , numpy = True , labelled = True ))
410
+ self .obj = DataFrame (* loads (json , dtype = None , numpy = True , labelled = True ,
411
+ precise_float = self .precise_float ))
396
412
397
413
def _parse_no_numpy (self ):
398
414
399
415
json = self .json
400
416
orient = self .orient
401
417
402
418
if orient == "columns" :
403
- self .obj = DataFrame (loads (json ), dtype = None )
419
+ self .obj = DataFrame (
420
+ loads (json , precise_float = self .precise_float ), dtype = None )
404
421
elif orient == "split" :
405
422
decoded = dict ((str (k ), v )
406
- for k , v in loads (json ).iteritems ())
423
+ for k , v in loads (
424
+ json ,
425
+ precise_float = self .precise_float ).iteritems ())
407
426
self .obj = DataFrame (dtype = None , ** decoded )
408
427
elif orient == "index" :
409
- self .obj = DataFrame (loads (json ), dtype = None ).T
428
+ self .obj = DataFrame (
429
+ loads (json , precise_float = self .precise_float ), dtype = None ).T
410
430
else :
411
- self .obj = DataFrame (loads (json ), dtype = None )
431
+ self .obj = DataFrame (
432
+ loads (json , precise_float = self .precise_float ), dtype = None )
412
433
413
434
def _try_convert_types (self ):
414
435
if self .obj is None : return
0 commit comments