Skip to content

Commit fc42e6e

Browse files
robmarkcolejorisvandenbossche
authored andcommitted
DOC: update the to_json() docstring (#20149)
1 parent 76a87ca commit fc42e6e

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

pandas/core/generic.py

+35-25
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,11 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
17171717
17181718
Parameters
17191719
----------
1720-
path_or_buf : the path or buffer to write the result string
1721-
if this is None, return the converted string
1720+
path_or_buf : string or file handle, optional
1721+
File path or object. If not specified, the result is returned as
1722+
a string.
17221723
orient : string
1724+
Indication of expected JSON string format.
17231725
17241726
* Series
17251727
@@ -1734,27 +1736,29 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
17341736
17351737
* The format of the JSON string
17361738
1737-
- split : dict like
1738-
{index -> [index], columns -> [columns], data -> [values]}
1739-
- records : list like
1739+
- 'split' : dict like {'index' -> [index],
1740+
'columns' -> [columns], 'data' -> [values]}
1741+
- 'records' : list like
17401742
[{column -> value}, ... , {column -> value}]
1741-
- index : dict like {index -> {column -> value}}
1742-
- columns : dict like {column -> {index -> value}}
1743-
- values : just the values array
1744-
- table : dict like {'schema': {schema}, 'data': {data}}
1743+
- 'index' : dict like {index -> {column -> value}}
1744+
- 'columns' : dict like {column -> {index -> value}}
1745+
- 'values' : just the values array
1746+
- 'table' : dict like {'schema': {schema}, 'data': {data}}
17451747
describing the data, and the data component is
17461748
like ``orient='records'``.
17471749
17481750
.. versionchanged:: 0.20.0
17491751
17501752
date_format : {None, 'epoch', 'iso'}
1751-
Type of date conversion. `epoch` = epoch milliseconds,
1752-
`iso` = ISO8601. The default depends on the `orient`. For
1753-
`orient='table'`, the default is `'iso'`. For all other orients,
1754-
the default is `'epoch'`.
1755-
double_precision : The number of decimal places to use when encoding
1756-
floating point values, default 10.
1757-
force_ascii : force encoded string to be ASCII, default True.
1753+
Type of date conversion. 'epoch' = epoch milliseconds,
1754+
'iso' = ISO8601. The default depends on the `orient`. For
1755+
``orient='table'``, the default is 'iso'. For all other orients,
1756+
the default is 'epoch'.
1757+
double_precision : int, default 10
1758+
The number of decimal places to use when encoding
1759+
floating point values.
1760+
force_ascii : boolean, default True
1761+
Force encoded string to be ASCII.
17581762
date_unit : string, default 'ms' (milliseconds)
17591763
The time unit to encode to, governs timestamp and ISO8601
17601764
precision. One of 's', 'ms', 'us', 'ns' for second, millisecond,
@@ -1783,13 +1787,9 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
17831787
17841788
.. versionadded:: 0.23.0
17851789
1786-
Returns
1787-
-------
1788-
same type as input object with filtered info axis
1789-
17901790
See Also
17911791
--------
1792-
pd.read_json
1792+
pandas.read_json
17931793
17941794
Examples
17951795
--------
@@ -1802,16 +1802,26 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
18021802
"index":["row 1","row 2"],
18031803
"data":[["a","b"],["c","d"]]}'
18041804
1805+
Encoding/decoding a Dataframe using ``'records'`` formatted JSON.
1806+
Note that index labels are not preserved with this encoding.
1807+
1808+
>>> df.to_json(orient='records')
1809+
'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
1810+
18051811
Encoding/decoding a Dataframe using ``'index'`` formatted JSON:
18061812
18071813
>>> df.to_json(orient='index')
18081814
'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'
18091815
1810-
Encoding/decoding a Dataframe using ``'records'`` formatted JSON.
1811-
Note that index labels are not preserved with this encoding.
1816+
Encoding/decoding a Dataframe using ``'columns'`` formatted JSON:
18121817
1813-
>>> df.to_json(orient='records')
1814-
'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
1818+
>>> df.to_json(orient='columns')
1819+
'{"col 1":{"row 1":"a","row 2":"c"},"col 2":{"row 1":"b","row 2":"d"}}'
1820+
1821+
Encoding/decoding a Dataframe using ``'values'`` formatted JSON:
1822+
1823+
>>> df.to_json(orient='values')
1824+
'[["a","b"],["c","d"]]'
18151825
18161826
Encoding with Table Schema
18171827

0 commit comments

Comments
 (0)