Skip to content

Commit e31f839

Browse files
wesmjreback
authored andcommitted
ENH: pull pandasjson back into pandas
1 parent 4d06037 commit e31f839

File tree

8 files changed

+1987
-0
lines changed

8 files changed

+1987
-0
lines changed

pandas/core/frame.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

pandas/core/series.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,6 +3298,88 @@ def str(self):
32983298
from pandas.core.strings import StringMethods
32993299
return StringMethods(self)
33003300

3301+
3302+
@classmethod
3303+
def from_json(cls, json, orient="index", dtype=None, numpy=True):
3304+
"""
3305+
Convert JSON string to Series
3306+
3307+
Parameters
3308+
----------
3309+
json : The JSON string to parse.
3310+
orient : {'split', 'records', 'index'}, default 'index'
3311+
The format of the JSON string
3312+
split : dict like
3313+
{index -> [index], name -> name, data -> [values]}
3314+
records : list like [value, ... , value]
3315+
index : dict like {index -> value}
3316+
dtype : dtype of the resulting Series
3317+
nupmpy: direct decoding to numpy arrays. default True but falls back
3318+
to standard decoding if a problem occurs.
3319+
3320+
Returns
3321+
-------
3322+
result : Series
3323+
"""
3324+
from pandas.json import loads
3325+
s = None
3326+
3327+
if dtype is not None and orient == "split":
3328+
numpy = False
3329+
3330+
if numpy:
3331+
try:
3332+
if orient == "split":
3333+
decoded = loads(json, dtype=dtype, numpy=True)
3334+
decoded = dict((str(k), v) for k, v in decoded.iteritems())
3335+
s = Series(**decoded)
3336+
elif orient == "columns" or orient == "index":
3337+
s = Series(*loads(json, dtype=dtype, numpy=True,
3338+
labelled=True))
3339+
else:
3340+
s = Series(loads(json, dtype=dtype, numpy=True))
3341+
except ValueError:
3342+
numpy = False
3343+
if not numpy:
3344+
if orient == "split":
3345+
decoded = dict((str(k), v)
3346+
for k, v in loads(json).iteritems())
3347+
s = Series(dtype=dtype, **decoded)
3348+
else:
3349+
s = Series(loads(json), dtype=dtype)
3350+
3351+
return s
3352+
Series.from_json = from_json
3353+
3354+
def to_json(self, orient="index", double_precision=10, force_ascii=True):
3355+
"""
3356+
Convert Series to a JSON string
3357+
3358+
Note NaN's and None will be converted to null and datetime objects
3359+
will be converted to UNIX timestamps.
3360+
3361+
Parameters
3362+
----------
3363+
orient : {'split', 'records', 'index'}, default 'index'
3364+
The format of the JSON string
3365+
split : dict like
3366+
{index -> [index], name -> name, data -> [values]}
3367+
records : list like [value, ... , value]
3368+
index : dict like {index -> value}
3369+
double_precision : The number of decimal places to use when encoding
3370+
floating point values, default 10.
3371+
force_ascii : force encoded string to be ASCII, default True.
3372+
3373+
Returns
3374+
-------
3375+
result : JSON compatible string
3376+
"""
3377+
from pandas.json import dumps
3378+
return dumps(self, orient=orient, double_precision=double_precision,
3379+
ensure_ascii=force_ascii)
3380+
Series.to_json = to_json
3381+
3382+
33013383
_INDEX_TYPES = ndarray, Index, list, tuple
33023384

33033385
#------------------------------------------------------------------------------

0 commit comments

Comments
 (0)