@@ -766,6 +766,93 @@ def to_dict(self):
766
766
"""
767
767
return dict ((k , v .to_dict ()) for k , v in self .iteritems ())
768
768
769
+ @classmethod
770
+ def from_json (cls , json , orient = "columns" , dtype = None , numpy = True ):
771
+ """
772
+ Convert JSON string to DataFrame
773
+
774
+ Parameters
775
+ ----------
776
+ json : The JSON string to parse.
777
+ orient : {'split', 'records', 'index', 'columns', 'values'},
778
+ default 'columns'
779
+ The format of the JSON string
780
+ split : dict like
781
+ {index -> [index], columns -> [columns], data -> [values]}
782
+ records : list like [{column -> value}, ... , {column -> value}]
783
+ index : dict like {index -> {column -> value}}
784
+ columns : dict like {column -> {index -> value}}
785
+ values : just the values array
786
+ dtype : dtype of the resulting DataFrame
787
+ nupmpy: direct decoding to numpy arrays. default True but falls back
788
+ to standard decoding if a problem occurs.
789
+
790
+ Returns
791
+ -------
792
+ result : DataFrame
793
+ """
794
+ from pandas ._ujson import loads
795
+ df = None
796
+
797
+ if numpy :
798
+ try :
799
+ if orient == "columns" :
800
+ args = loads (json , dtype = dtype , numpy = True , labelled = True )
801
+ if args :
802
+ args = (args [0 ].T , args [2 ], args [1 ])
803
+ df = DataFrame (* args )
804
+ elif orient == "split" :
805
+ df = DataFrame (** loads (json , dtype = dtype , numpy = True ))
806
+ elif orient == "values" :
807
+ df = DataFrame (loads (json , dtype = dtype , numpy = True ))
808
+ else :
809
+ df = DataFrame (* loads (json , dtype = dtype , numpy = True ,
810
+ labelled = True ))
811
+ except ValueError :
812
+ numpy = False
813
+ if not numpy :
814
+ if orient == "columns" :
815
+ df = DataFrame (loads (json ), dtype = dtype )
816
+ elif orient == "split" :
817
+ df = DataFrame (dtype = dtype , ** loads (json ))
818
+ elif orient == "index" :
819
+ df = DataFrame (loads (json ), dtype = dtype ).T
820
+ else :
821
+ df = DataFrame (loads (json ), dtype = dtype )
822
+
823
+ return df
824
+
825
+ def to_json (self , orient = "columns" , double_precision = 10 ,
826
+ force_ascii = True ):
827
+ """
828
+ Convert DataFrame to a JSON string.
829
+
830
+ Note NaN's and None will be converted to null and datetime objects
831
+ will be converted to UNIX timestamps.
832
+
833
+ Parameters
834
+ ----------
835
+ orient : {'split', 'records', 'index', 'columns', 'values'},
836
+ default 'columns'
837
+ The format of the JSON string
838
+ split : dict like
839
+ {index -> [index], columns -> [columns], data -> [values]}
840
+ records : list like [{column -> value}, ... , {column -> value}]
841
+ index : dict like {index -> {column -> value}}
842
+ columns : dict like {column -> {index -> value}}
843
+ values : just the values array
844
+ double_precision : The number of decimal places to use when encoding
845
+ floating point values, default 10.
846
+ force_ascii : force encoded string to be ASCII, default True.
847
+
848
+ Returns
849
+ -------
850
+ result : JSON compatible string
851
+ """
852
+ from pandas ._ujson import dumps
853
+ return dumps (self , orient = orient , double_precision = double_precision ,
854
+ ensure_ascii = force_ascii )
855
+
769
856
@classmethod
770
857
def from_records (cls , data , index = None , exclude = None , columns = None ,
771
858
names = None , coerce_float = False ):
0 commit comments