@@ -35,6 +35,7 @@ object.
35
35
* ``read_excel ``
36
36
* ``read_hdf ``
37
37
* ``read_sql ``
38
+ * ``read_json ``
38
39
* ``read_html ``
39
40
* ``read_stata ``
40
41
* ``read_clipboard ``
@@ -45,6 +46,7 @@ The corresponding ``writer`` functions are object methods that are accessed like
45
46
* ``to_excel ``
46
47
* ``to_hdf ``
47
48
* ``to_sql ``
49
+ * ``to_json ``
48
50
* ``to_html ``
49
51
* ``to_stata ``
50
52
* ``to_clipboard ``
@@ -937,6 +939,104 @@ The Series object also has a ``to_string`` method, but with only the ``buf``,
937
939
which, if set to ``True ``, will additionally output the length of the Series.
938
940
939
941
942
+ JSON
943
+ ----
944
+
945
+ Read and write ``JSON `` format files.
946
+
947
+ .. _io.json :
948
+
949
+ Writing JSON
950
+ ~~~~~~~~~~~~
951
+
952
+ A ``Series `` or ``DataFrame `` can be converted to a valid JSON string. Use ``to_json ``
953
+ with optional parameters:
954
+
955
+ - path_or_buf : the pathname or buffer to write the output
956
+ This can be ``None `` in which case a JSON string is returned
957
+ - orient : The format of the JSON string, default is ``index `` for ``Series ``, ``columns `` for ``DataFrame ``
958
+
959
+ * split : dict like {index -> [index], columns -> [columns], data -> [values]}
960
+ * records : list like [{column -> value}, ... , {column -> value}]
961
+ * index : dict like {index -> {column -> value}}
962
+ * columns : dict like {column -> {index -> value}}
963
+ * values : just the values array
964
+
965
+ - date_format : type of date conversion (epoch = epoch milliseconds, iso = ISO8601), default is epoch
966
+ - double_precision : The number of decimal places to use when encoding floating point values, default 10.
967
+ - force_ascii : force encoded string to be ASCII, default True.
968
+
969
+ Note NaN's and None will be converted to null and datetime objects will be converted based on the date_format parameter
970
+
971
+ .. ipython :: python
972
+
973
+ dfj = DataFrame(randn(5 , 2 ), columns = list (' AB' ))
974
+ json = dfj.to_json()
975
+ json
976
+
977
+ Writing in iso date format
978
+
979
+ .. ipython :: python
980
+
981
+ dfd = DataFrame(randn(5 , 2 ), columns = list (' AB' ))
982
+ dfd[' date' ] = Timestamp(' 20130101' )
983
+ json = dfd.to_json(date_format = ' iso' )
984
+ json
985
+
986
+ Writing to a file, with a date index and a date column
987
+
988
+ .. ipython :: python
989
+
990
+ dfj2 = dfj.copy()
991
+ dfj2[' date' ] = Timestamp(' 20130101' )
992
+ dfj2.index = date_range(' 20130101' ,periods = 5 )
993
+ dfj2.to_json(' test.json' )
994
+ open (' test.json' ).read()
995
+
996
+ Reading JSON
997
+ ~~~~~~~~~~~~
998
+
999
+ Reading a JSON string to pandas object can take a number of parameters.
1000
+ The parser will try to parse a ``DataFrame `` if ``typ `` is not supplied or
1001
+ is ``None ``. To explicity force ``Series `` parsing, pass ``typ=series ``
1002
+
1003
+ - filepath_or_buffer : a **VALID ** JSON string or file handle / StringIO. The string could be
1004
+ a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host
1005
+ is expected. For instance, a local file could be
1006
+ file ://localhost/path/to/table.json
1007
+ - typ : type of object to recover (series or frame), default 'frame'
1008
+ - orient : The format of the JSON string, one of the following
1009
+
1010
+ * split : dict like {index -> [index], name -> name, data -> [values]}
1011
+ * records : list like [value, ... , value]
1012
+ * index : dict like {index -> value}
1013
+
1014
+ - dtype : dtype of the resulting object
1015
+ - numpy : direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs.
1016
+ - parse_dates : a list of columns to parse for dates; If True, then try to parse datelike columns, default is False
1017
+ - keep_default_dates : boolean, default True. If parsing dates, then parse the default datelike columns
1018
+
1019
+ The parser will raise one of ``ValueError/TypeError/AssertionError `` if the JSON is
1020
+ not parsable.
1021
+
1022
+ Reading from a JSON string
1023
+
1024
+ .. ipython :: python
1025
+
1026
+ pd.read_json(json)
1027
+
1028
+ Reading from a file, parsing dates
1029
+
1030
+ .. ipython :: python
1031
+
1032
+ pd.read_json(' test.json' ,parse_dates = True )
1033
+
1034
+ .. ipython :: python
1035
+ :suppress:
1036
+
1037
+ import os
1038
+ os.remove(' test.json' )
1039
+
940
1040
HTML
941
1041
----
942
1042
@@ -2193,7 +2293,6 @@ into a .dta file. The format version of this file is always the latest one, 115.
2193
2293
2194
2294
.. ipython:: python
2195
2295
2196
- from pandas.io.stata import StataWriter
2197
2296
df = DataFrame(randn(10 , 2 ), columns = list (' AB' ))
2198
2297
df.to_stata(' stata.dta' )
2199
2298
0 commit comments