Skip to content

Commit 8e673cf

Browse files
committed
ENH: removed json argument, now path_or_buf can be a path,buffer,url,or JSON string
added keywords parse_dates,keep_default_dates to allow for date parsing in columns of a Frame (default is False, not to parse dates)
1 parent 6422041 commit 8e673cf

File tree

4 files changed

+263
-142
lines changed

4 files changed

+263
-142
lines changed

doc/source/io.rst

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ A ``Series`` or ``DataFrame`` can be converted to a valid JSON string. Use ``to_
953953
with optional parameters:
954954

955955
- 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
957957
- orient : The format of the JSON string, default is ``index`` for ``Series``, ``columns`` for ``DataFrame``
958958

959959
* 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
969969

970970
.. ipython:: python
971971
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()
975985
976986
Reading JSON
977987
~~~~~~~~~~~~
@@ -984,16 +994,17 @@ is ``None``. To explicity force ``Series`` parsing, pass ``typ=series``
984994
a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host
985995
is expected. For instance, a local file could be
986996
file ://localhost/path/to/table.json
987-
- json : a VALID JSON string, optional, used if filepath_or_buffer is not provided
988997
- typ : type of object to recover (series or frame), default 'frame'
989998
- orient : The format of the JSON string, one of the following
990999

9911000
* split : dict like {index -> [index], name -> name, data -> [values]}
9921001
* records : list like [value, ... , value]
9931002
* index : dict like {index -> value}
9941003

995-
- dtype : dtype of the resulting Series
1004+
- dtype : dtype of the resulting object
9961005
- 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
9971008

9981009
The parser will raise one of ``ValueError/TypeError/AssertionError`` if the JSON is
9991010
not parsable.
@@ -1002,13 +1013,19 @@ Reading from a JSON string
10021013

10031014
.. ipython:: python
10041015
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
10061021
1007-
Reading from a StringIO
1022+
pd.read_json('test.json',parse_dates=True)
10081023
10091024
.. ipython:: python
1025+
:suppress:
10101026
1011-
pd.read_json(json)
1027+
import os
1028+
os.remove('test.json')
10121029
10131030
HTML
10141031
----

pandas/core/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def to_clipboard(self):
495495
from pandas.io import clipboard
496496
clipboard.to_clipboard(self)
497497

498-
def to_json(self, path_or_buf, orient=None, double_precision=10,
498+
def to_json(self, path_or_buf=None, orient=None, double_precision=10,
499499
force_ascii=True):
500500
"""
501501
Convert the object to a JSON string.
@@ -529,7 +529,7 @@ def to_json(self, path_or_buf, orient=None, double_precision=10,
529529
"""
530530

531531
from pandas.io import json
532-
return json.to_json(path_or_buf, self, orient=orient, double_precision=double_precision,
532+
return json.to_json(path_or_buf=path_or_buf, obj=self, orient=orient, double_precision=double_precision,
533533
force_ascii=force_ascii)
534534

535535
# install the indexerse

0 commit comments

Comments
 (0)