@@ -953,7 +953,7 @@ A ``Series`` or ``DataFrame`` can be converted to a valid JSON string. Use ``to_
953
953
with optional parameters:
954
954
955
955
- path_or_buf : the pathname or buffer to write the output
956
- This can be ``None `` in which case a `` StringIO `` converted string is returned
956
+ This can be ``None `` in which case a JSON string is returned
957
957
- orient : The format of the JSON string, default is ``index `` for ``Series ``, ``columns `` for ``DataFrame ``
958
958
959
959
* split : dict like {index -> [index], columns -> [columns], data -> [values]}
@@ -969,9 +969,19 @@ Note NaN's and None will be converted to null and datetime objects will be conve
969
969
970
970
.. ipython :: python
971
971
972
- df = DataFrame(randn(10 , 2 ), columns = list (' AB' ))
973
- json = df.to_json(None )
974
- json.getvalue()
972
+ dfj = DataFrame(randn(5 , 2 ), columns = list (' AB' ))
973
+ json = dfj.to_json()
974
+ json
975
+
976
+ Writing to a file, with a date index and a date column
977
+
978
+ .. ipython :: python
979
+
980
+ dfj2 = dfj.copy()
981
+ dfj2[' date' ] = Timestamp(' 20130101' )
982
+ dfj2.index = date_range(' 20130101' ,periods = 5 )
983
+ dfj2.to_json(' test.json' )
984
+ open (' test.json' ).read()
975
985
976
986
Reading JSON
977
987
~~~~~~~~~~~~
@@ -984,16 +994,17 @@ is ``None``. To explicity force ``Series`` parsing, pass ``typ=series``
984
994
a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host
985
995
is expected. For instance, a local file could be
986
996
file ://localhost/path/to/table.json
987
- - json : a VALID JSON string, optional, used if filepath_or_buffer is not provided
988
997
- typ : type of object to recover (series or frame), default 'frame'
989
998
- orient : The format of the JSON string, one of the following
990
999
991
1000
* split : dict like {index -> [index], name -> name, data -> [values]}
992
1001
* records : list like [value, ... , value]
993
1002
* index : dict like {index -> value}
994
1003
995
- - dtype : dtype of the resulting Series
1004
+ - dtype : dtype of the resulting object
996
1005
- numpy : direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs.
1006
+ - parse_dates : a list of columns to parse for dates; If True, then try to parse datelike columns, default is True
1007
+ - keep_default_dates : boolean, default True. If parsing dates, then parse the default datelike columns
997
1008
998
1009
The parser will raise one of ``ValueError/TypeError/AssertionError `` if the JSON is
999
1010
not parsable.
@@ -1002,13 +1013,19 @@ Reading from a JSON string
1002
1013
1003
1014
.. ipython :: python
1004
1015
1005
- pd.read_json(json = ' {"0":{"0":1,"1":3},"1":{"0":2,"1":4}} ' )
1016
+ pd.read_json(json)
1017
+
1018
+ Reading from a file, parsing dates
1019
+
1020
+ .. ipython :: python
1006
1021
1007
- Reading from a StringIO
1022
+ pd.read_json( ' test.json ' , parse_dates = True )
1008
1023
1009
1024
.. ipython :: python
1025
+ :suppress:
1010
1026
1011
- pd.read_json(json)
1027
+ import os
1028
+ os.remove(' test.json' )
1012
1029
1013
1030
HTML
1014
1031
----
0 commit comments