@@ -679,6 +679,93 @@ def to_dict(self):
679
679
"""
680
680
return dict ((k , v .to_dict ()) for k , v in self .iteritems ())
681
681
682
+ @classmethod
683
+ def from_json (cls , json , orient = "columns" , dtype = None , numpy = True ):
684
+ """
685
+ Convert JSON string to DataFrame
686
+
687
+ Parameters
688
+ ----------
689
+ json : The JSON string to parse.
690
+ orient : {'split', 'records', 'index', 'columns', 'values'},
691
+ default 'columns'
692
+ The format of the JSON string
693
+ split : dict like
694
+ {index -> [index], columns -> [columns], data -> [values]}
695
+ records : list like [{column -> value}, ... , {column -> value}]
696
+ index : dict like {index -> {column -> value}}
697
+ columns : dict like {column -> {index -> value}}
698
+ values : just the values array
699
+ dtype : dtype of the resulting DataFrame
700
+ nupmpy: direct decoding to numpy arrays. default True but falls back
701
+ to standard decoding if a problem occurs.
702
+
703
+ Returns
704
+ -------
705
+ result : DataFrame
706
+ """
707
+ from pandas ._ujson import loads
708
+ df = None
709
+
710
+ if numpy :
711
+ try :
712
+ if orient == "columns" :
713
+ args = loads (json , dtype = dtype , numpy = True , labelled = True )
714
+ if args :
715
+ args = (args [0 ].T , args [2 ], args [1 ])
716
+ df = DataFrame (* args )
717
+ elif orient == "split" :
718
+ df = DataFrame (** loads (json , dtype = dtype , numpy = True ))
719
+ elif orient == "values" :
720
+ df = DataFrame (loads (json , dtype = dtype , numpy = True ))
721
+ else :
722
+ df = DataFrame (* loads (json , dtype = dtype , numpy = True ,
723
+ labelled = True ))
724
+ except ValueError :
725
+ numpy = False
726
+ if not numpy :
727
+ if orient == "columns" :
728
+ df = DataFrame (loads (json ), dtype = dtype )
729
+ elif orient == "split" :
730
+ df = DataFrame (dtype = dtype , ** loads (json ))
731
+ elif orient == "index" :
732
+ df = DataFrame (loads (json ), dtype = dtype ).T
733
+ else :
734
+ df = DataFrame (loads (json ), dtype = dtype )
735
+
736
+ return df
737
+
738
+ def to_json (self , orient = "columns" , double_precision = 10 ,
739
+ force_ascii = True ):
740
+ """
741
+ Convert DataFrame to a JSON string.
742
+
743
+ Note NaN's and None will be converted to null and datetime objects
744
+ will be converted to UNIX timestamps.
745
+
746
+ Parameters
747
+ ----------
748
+ orient : {'split', 'records', 'index', 'columns', 'values'},
749
+ default 'columns'
750
+ The format of the JSON string
751
+ split : dict like
752
+ {index -> [index], columns -> [columns], data -> [values]}
753
+ records : list like [{column -> value}, ... , {column -> value}]
754
+ index : dict like {index -> {column -> value}}
755
+ columns : dict like {column -> {index -> value}}
756
+ values : just the values array
757
+ double_precision : The number of decimal places to use when encoding
758
+ floating point values, default 10.
759
+ force_ascii : force encoded string to be ASCII, default True.
760
+
761
+ Returns
762
+ -------
763
+ result : JSON compatible string
764
+ """
765
+ from pandas ._ujson import dumps
766
+ return dumps (self , orient = orient , double_precision = double_precision ,
767
+ ensure_ascii = force_ascii )
768
+
682
769
@classmethod
683
770
def from_records (cls , data , index = None , exclude = None , columns = None ,
684
771
names = None , coerce_float = False ):
0 commit comments