Skip to content

Commit 73763af

Browse files
Komnomnomnomwesm
authored andcommitted
ENH: Add JSON export option for DataFrame take 2
1 parent 9a07494 commit 73763af

13 files changed

+5832
-3
lines changed

pandas/core/frame.py

+87
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,93 @@ def to_dict(self):
766766
"""
767767
return dict((k, v.to_dict()) for k, v in self.iteritems())
768768

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+
769856
@classmethod
770857
def from_records(cls, data, index=None, exclude=None, columns=None,
771858
names=None, coerce_float=False):

pandas/core/series.py

+71
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,77 @@ def to_dict(self):
919919
"""
920920
return dict(self.iteritems())
921921

922+
@classmethod
923+
def from_json(cls, json, orient="index", dtype=None, numpy=True):
924+
"""
925+
Convert JSON string to Series
926+
927+
Parameters
928+
----------
929+
json : The JSON string to parse.
930+
orient : {'split', 'records', 'index'}, default 'index'
931+
The format of the JSON string
932+
split : dict like
933+
{index -> [index], name -> name, data -> [values]}
934+
records : list like [value, ... , value]
935+
index : dict like {index -> value}
936+
dtype : dtype of the resulting Series
937+
nupmpy: direct decoding to numpy arrays. default True but falls back
938+
to standard decoding if a problem occurs.
939+
940+
Returns
941+
-------
942+
result : Series
943+
"""
944+
from pandas._ujson import loads
945+
s = None
946+
947+
if numpy:
948+
try:
949+
if orient == "split":
950+
s = Series(**loads(json, dtype=dtype, numpy=True))
951+
elif orient == "columns" or orient == "index":
952+
s = Series(*loads(json, dtype=dtype, numpy=True,
953+
labelled=True))
954+
else:
955+
s = Series(loads(json, dtype=dtype, numpy=True))
956+
except ValueError:
957+
numpy = False
958+
if not numpy:
959+
if orient == "split":
960+
s = Series(dtype=dtype, **loads(json))
961+
else:
962+
s = Series(loads(json), dtype=dtype)
963+
964+
return s
965+
966+
def to_json(self, orient="index", double_precision=10, force_ascii=True):
967+
"""
968+
Convert Series to a JSON string
969+
970+
Note NaN's and None will be converted to null and datetime objects
971+
will be converted to UNIX timestamps.
972+
973+
Parameters
974+
----------
975+
orient : {'split', 'records', 'index'}, default 'index'
976+
The format of the JSON string
977+
split : dict like
978+
{index -> [index], name -> name, data -> [values]}
979+
records : list like [value, ... , value]
980+
index : dict like {index -> value}
981+
double_precision : The number of decimal places to use when encoding
982+
floating point values, default 10.
983+
force_ascii : force encoded string to be ASCII, default True.
984+
985+
Returns
986+
-------
987+
result : JSON compatible string
988+
"""
989+
from pandas._ujson import dumps
990+
return dumps(self, orient=orient, double_precision=double_precision,
991+
ensure_ascii=force_ascii)
992+
922993
def to_sparse(self, kind='block', fill_value=None):
923994
"""
924995
Convert Series to SparseSeries

0 commit comments

Comments
 (0)