22
22
def to_json (path_or_buf , obj , orient = None , date_format = 'epoch' ,
23
23
double_precision = 10 , force_ascii = True , date_unit = 'ms' ,
24
24
default_handler = None , lines = False ):
25
-
26
25
if lines and orient != 'records' :
27
- raise ValueError (
28
- "'lines' keyword only valid when 'orient' is records" )
26
+ raise ValueError (
27
+ "'lines' keyword only valid when 'orient' is records" )
29
28
30
29
if isinstance (obj , Series ):
31
30
s = SeriesWriter (
@@ -53,7 +52,6 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
53
52
54
53
55
54
class Writer (object ):
56
-
57
55
def __init__ (self , obj , orient , date_format , double_precision ,
58
56
ensure_ascii , date_unit , default_handler = None ):
59
57
self .obj = obj
@@ -291,7 +289,6 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
291
289
292
290
293
291
class Parser (object ):
294
-
295
292
_STAMP_UNITS = ('s' , 'ms' , 'us' , 'ns' )
296
293
_MIN_STAMPS = {
297
294
's' : long (31536000 ),
@@ -492,8 +489,8 @@ def _parse_no_numpy(self):
492
489
if orient == "split" :
493
490
decoded = dict ((str (k ), v )
494
491
for k , v in compat .iteritems (loads (
495
- json ,
496
- precise_float = self .precise_float )))
492
+ json ,
493
+ precise_float = self .precise_float )))
497
494
self .check_keys_split (decoded )
498
495
self .obj = Series (dtype = None , ** decoded )
499
496
else :
@@ -567,8 +564,8 @@ def _parse_no_numpy(self):
567
564
elif orient == "split" :
568
565
decoded = dict ((str (k ), v )
569
566
for k , v in compat .iteritems (loads (
570
- json ,
571
- precise_float = self .precise_float )))
567
+ json ,
568
+ precise_float = self .precise_float )))
572
569
self .check_keys_split (decoded )
573
570
self .obj = DataFrame (dtype = None , ** decoded )
574
571
elif orient == "index" :
@@ -595,7 +592,6 @@ def _process_converter(self, f, filt=None):
595
592
new_obj [i ] = c
596
593
597
594
if needs_new_obj :
598
-
599
595
# possibly handle dup columns
600
596
new_obj = DataFrame (new_obj , index = self .obj .index )
601
597
new_obj .columns = self .obj .columns
@@ -628,9 +624,9 @@ def is_ok(col):
628
624
col_lower = col .lower ()
629
625
if (col_lower .endswith ('_at' ) or
630
626
col_lower .endswith ('_time' ) or
631
- col_lower == 'modified' or
632
- col_lower == 'date' or
633
- col_lower == 'datetime' or
627
+ col_lower == 'modified' or
628
+ col_lower == 'date' or
629
+ col_lower == 'datetime' or
634
630
col_lower .startswith ('timestamp' )):
635
631
return True
636
632
return False
@@ -640,6 +636,7 @@ def is_ok(col):
640
636
lambda col , c : ((self .keep_default_dates and is_ok (col )) or
641
637
col in convert_dates ))
642
638
639
+
643
640
# ---------------------------------------------------------------------
644
641
# JSON normalization routines
645
642
@@ -723,7 +720,7 @@ def nested_to_record(ds, prefix="", level=0):
723
720
724
721
def json_normalize (data , record_path = None , meta = None ,
725
722
meta_prefix = None ,
726
- record_prefix = None ):
723
+ record_prefix = None , errors = 'raise' ):
727
724
"""
728
725
"Normalize" semi-structured JSON data into a flat table
729
726
@@ -740,6 +737,8 @@ def json_normalize(data, record_path=None, meta=None,
740
737
If True, prefix records with dotted (?) path, e.g. foo.bar.field if
741
738
path to records is ['foo', 'bar']
742
739
meta_prefix : string, default None
740
+ error: {'raise', 'ignore'}, default 'raise'
741
+ * ignore: will ignore keyErrors if keys listed in meta are not always present
743
742
744
743
Returns
745
744
-------
@@ -775,6 +774,7 @@ def json_normalize(data, record_path=None, meta=None,
775
774
4 Cuyahoga 1337 John Kasich Ohio OH
776
775
777
776
"""
777
+
778
778
def _pull_field (js , spec ):
779
779
result = js
780
780
if isinstance (spec , list ):
@@ -841,8 +841,11 @@ def _recursive_extract(data, path, seen_meta, level=0):
841
841
else :
842
842
try :
843
843
meta_val = _pull_field (obj , val [level :])
844
- except :
845
- meta_val = np .nan
844
+ except KeyError as e :
845
+ if errors == 'ignore' :
846
+ meta_val = np .nan
847
+ else :
848
+ raise KeyError ("Try running with errors='ignore' as the following key may not always be present: " + str (e ))
846
849
meta_vals [key ].append (meta_val )
847
850
848
851
records .extend (recs )
0 commit comments