@@ -5593,6 +5593,106 @@ def mask(self, cond):
55935593 """
55945594 return self .where (~ cond , NA )
55955595
5596+
5597+ @classmethod
5598+ def from_json (cls , json , orient = "columns" , dtype = None , numpy = True ):
5599+ """
5600+ Convert JSON string to DataFrame
5601+
5602+ Parameters
5603+ ----------
5604+ json : The JSON string to parse.
5605+ orient : {'split', 'records', 'index', 'columns', 'values'},
5606+ default 'columns'
5607+ The format of the JSON string
5608+ split : dict like
5609+ {index -> [index], columns -> [columns], data -> [values]}
5610+ records : list like [{column -> value}, ... , {column -> value}]
5611+ index : dict like {index -> {column -> value}}
5612+ columns : dict like {column -> {index -> value}}
5613+ values : just the values array
5614+ dtype : dtype of the resulting DataFrame
5615+ nupmpy: direct decoding to numpy arrays. default True but falls back
5616+ to standard decoding if a problem occurs.
5617+
5618+ Returns
5619+ -------
5620+ result : DataFrame
5621+ """
5622+ from pandas .json import loads
5623+
5624+ df = None
5625+
5626+ if dtype is not None and orient == "split" :
5627+ numpy = False
5628+
5629+ if numpy :
5630+ try :
5631+ if orient == "columns" :
5632+ args = loads (json , dtype = dtype , numpy = True , labelled = True )
5633+ if args :
5634+ args = (args [0 ].T , args [2 ], args [1 ])
5635+ df = DataFrame (* args )
5636+ elif orient == "split" :
5637+ decoded = loads (json , dtype = dtype , numpy = True )
5638+ decoded = dict ((str (k ), v ) for k , v in decoded .iteritems ())
5639+ df = DataFrame (** decoded )
5640+ elif orient == "values" :
5641+ df = DataFrame (loads (json , dtype = dtype , numpy = True ))
5642+ else :
5643+ df = DataFrame (* loads (json , dtype = dtype , numpy = True ,
5644+ labelled = True ))
5645+ except ValueError :
5646+ numpy = False
5647+ if not numpy :
5648+ if orient == "columns" :
5649+ df = DataFrame (loads (json ), dtype = dtype )
5650+ elif orient == "split" :
5651+ decoded = dict ((str (k ), v )
5652+ for k , v in loads (json ).iteritems ())
5653+ df = DataFrame (dtype = dtype , ** decoded )
5654+ elif orient == "index" :
5655+ df = DataFrame (loads (json ), dtype = dtype ).T
5656+ else :
5657+ df = DataFrame (loads (json ), dtype = dtype )
5658+
5659+ return df
5660+ DataFrame .from_json = from_json
5661+
5662+
5663+ def to_json (self , orient = "columns" , double_precision = 10 ,
5664+ force_ascii = True ):
5665+ """
5666+ Convert DataFrame to a JSON string.
5667+
5668+ Note NaN's and None will be converted to null and datetime objects
5669+ will be converted to UNIX timestamps.
5670+
5671+ Parameters
5672+ ----------
5673+ orient : {'split', 'records', 'index', 'columns', 'values'},
5674+ default 'columns'
5675+ The format of the JSON string
5676+ split : dict like
5677+ {index -> [index], columns -> [columns], data -> [values]}
5678+ records : list like [{column -> value}, ... , {column -> value}]
5679+ index : dict like {index -> {column -> value}}
5680+ columns : dict like {column -> {index -> value}}
5681+ values : just the values array
5682+ double_precision : The number of decimal places to use when encoding
5683+ floating point values, default 10.
5684+ force_ascii : force encoded string to be ASCII, default True.
5685+
5686+ Returns
5687+ -------
5688+ result : JSON compatible string
5689+ """
5690+ from pandas .json import dumps
5691+ return dumps (self , orient = orient , double_precision = double_precision ,
5692+ ensure_ascii = force_ascii )
5693+ DataFrame .to_json = to_json
5694+
5695+
55965696_EMPTY_SERIES = Series ([])
55975697
55985698
0 commit comments