Skip to content

Commit 186a4f8

Browse files
committed
ENH: added convert_axes argument to control whether to coerce axes
ENH: changed dtype argument to accept a dict for a per-column dtype conversion, or turn off conversion (default is True) ENH: changed parse_dates to convert_dates, now defaulting to True BUG: not processing correctly some parsable JSON
1 parent 8f8b177 commit 186a4f8

File tree

3 files changed

+272
-120
lines changed

3 files changed

+272
-120
lines changed

doc/source/io.rst

+51-5
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,8 @@ Writing to a file, with a date index and a date column
989989
990990
dfj2 = dfj.copy()
991991
dfj2['date'] = Timestamp('20130101')
992+
dfj2['ints'] = range(5)
993+
dfj2['bools'] = True
992994
dfj2.index = date_range('20130101',periods=5)
993995
dfj2.to_json('test.json')
994996
open('test.json').read()
@@ -1011,25 +1013,69 @@ is ``None``. To explicity force ``Series`` parsing, pass ``typ=series``
10111013
* records : list like [value, ... , value]
10121014
* index : dict like {index -> value}
10131015

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
1016+
- dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don't infer dtypes at all, default is True, apply only to the data
1017+
- convert_axes : boolean, try to convert the axes to the proper dtypes, default is True
1018+
- convert_dates : a list of columns to parse for dates; If True, then try to parse datelike columns, default is True
10171019
- keep_default_dates : boolean, default True. If parsing dates, then parse the default datelike columns
1020+
- numpy: direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs.
10181021

10191022
The parser will raise one of ``ValueError/TypeError/AssertionError`` if the JSON is
10201023
not parsable.
10211024

1025+
The default of ``convert_axes=True``, ``dtype=True``, and ``convert_dates=True`` will try to parse the axes, and all of the data
1026+
into appropriate types, including dates. If you need to override specific dtypes, pass a dict to ``dtype``. ``convert_axes`` should only
1027+
be set to ``False`` if you need to preserve string-like numbers (e.g. '1', '2') in an axes.
1028+
1029+
.. warning::
1030+
1031+
When reading JSON data, automatic coercing into dtypes has some quirks:
1032+
1033+
* an index can be in a different order, that is the returned order is not guaranteed to be the same as before serialization
1034+
* a column that was ``float`` data can safely be converted to ``integer``, e.g. a column of ``1.``
1035+
* bool columns will be converted to ``integer`` on reconstruction
1036+
1037+
Thus there are times where you may want to specify specific dtypes via the ``dtype`` keyword argument.
1038+
10221039
Reading from a JSON string
10231040

10241041
.. ipython:: python
10251042
10261043
pd.read_json(json)
10271044
1028-
Reading from a file, parsing dates
1045+
Reading from a file
1046+
1047+
.. ipython:: python
1048+
1049+
pd.read_json('test.json')
1050+
1051+
Don't convert any data (but still convert axes and dates)
1052+
1053+
.. ipython:: python
1054+
1055+
pd.read_json('test.json',dtype=object).dtypes
1056+
1057+
Specify how I want to convert data
1058+
1059+
.. ipython:: python
1060+
1061+
pd.read_json('test.json',dtype={'A' : 'float32', 'bools' : 'int8'}).dtypes
1062+
1063+
I like my string indicies
10291064

10301065
.. ipython:: python
10311066
1032-
pd.read_json('test.json',parse_dates=True)
1067+
si = DataFrame(np.zeros((4, 4)),
1068+
columns=range(4),
1069+
index=[str(i) for i in range(4)])
1070+
si
1071+
si.index
1072+
si.columns
1073+
json = si.to_json()
1074+
1075+
sij = pd.read_json(json,convert_axes=False)
1076+
sij
1077+
sij.index
1078+
sij.columns
10331079
10341080
.. ipython:: python
10351081
:suppress:

0 commit comments

Comments
 (0)