From 8e7b162176e45689ac00a80430cbca98fc336e08 Mon Sep 17 00:00:00 2001 From: robmarkcole Date: Sat, 10 Mar 2018 14:18:30 +0000 Subject: [PATCH 1/4] Fix validation errors on to_json() Fixes validation errors and adds an example for each orientation --- pandas/core/generic.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a893b2ba1a189..93945dec71273 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1665,8 +1665,9 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, Parameters ---------- path_or_buf : the path or buffer to write the result string - if this is None, return the converted string + If this is None, return the converted string. orient : string + Indication of expected JSON string format. * Series @@ -1692,16 +1693,18 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, describing the data, and the data component is like ``orient='records'``. - .. versionchanged:: 0.20.0 + .. versionchanged:: 0.20.0. date_format : {None, 'epoch', 'iso'} Type of date conversion. `epoch` = epoch milliseconds, `iso` = ISO8601. The default depends on the `orient`. For `orient='table'`, the default is `'iso'`. For all other orients, the default is `'epoch'`. - double_precision : The number of decimal places to use when encoding - floating point values, default 10. - force_ascii : force encoded string to be ASCII, default True. + double_precision : int, default 10 + The number of decimal places to use when encoding + floating point values. + force_ascii : boolean, default True + Force encoded string to be ASCII. date_unit : string, default 'ms' (milliseconds) The time unit to encode to, governs timestamp and ISO8601 precision. One of 's', 'ms', 'us', 'ns' for second, millisecond, @@ -1715,20 +1718,20 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, throw ValueError if incorrect 'orient' since others are not list like. - .. versionadded:: 0.19.0 + .. versionadded:: 0.19.0. compression : {None, 'gzip', 'bz2', 'xz'} A string representing the compression to use in the output file, only used when the first argument is a filename - .. versionadded:: 0.21.0 + .. versionadded:: 0.21.0. index : boolean, default True Whether to include the index values in the JSON string. Not including the index (``index=False``) is only supported when orient is 'split' or 'table'. - .. versionadded:: 0.23.0 + .. versionadded:: 0.23.0. Returns ------- @@ -1749,16 +1752,26 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, "index":["row 1","row 2"], "data":[["a","b"],["c","d"]]}' + Encoding/decoding a Dataframe using ``'records'`` formatted JSON. + Note that index labels are not preserved with this encoding. + + >>> df.to_json(orient='records') + '[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]' + Encoding/decoding a Dataframe using ``'index'`` formatted JSON: >>> df.to_json(orient='index') '{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}' - Encoding/decoding a Dataframe using ``'records'`` formatted JSON. - Note that index labels are not preserved with this encoding. + Encoding/decoding a Dataframe using ``'columns'`` formatted JSON: - >>> df.to_json(orient='records') - '[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]' + >>> df.to_json(orient='columns') + '{"col 1":{"row 1":"a","row 2":"c"},"col 2":{"row 1":"b","row 2":"d"}}' + + Encoding/decoding a Dataframe using ``'values'`` formatted JSON: + + >>> df.to_json(orient='values') + '[["a","b"],["c","d"]]' Encoding with Table Schema From 5a5123cefceab9a9082de9aab6cb597124a8b5b2 Mon Sep 17 00:00:00 2001 From: robmarkcole Date: Tue, 13 Mar 2018 19:24:38 +0000 Subject: [PATCH 2/4] Addresses reviewer comments Adds quotes in parameters and updates path_or_buf to match to_csv --- pandas/core/generic.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 93945dec71273..11bdb2f6637e4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1664,8 +1664,9 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, Parameters ---------- - path_or_buf : the path or buffer to write the result string - If this is None, return the converted string. + path_or_buf : string or file handle, default None + File path or object, if None is provided the result is returned as + a string. orient : string Indication of expected JSON string format. @@ -1682,14 +1683,14 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, * The format of the JSON string - - split : dict like + - 'split' : dict like {index -> [index], columns -> [columns], data -> [values]} - - records : list like + - 'records' : list like [{column -> value}, ... , {column -> value}] - - index : dict like {index -> {column -> value}} - - columns : dict like {column -> {index -> value}} - - values : just the values array - - table : dict like {'schema': {schema}, 'data': {data}} + - 'index' : dict like {index -> {column -> value}} + - 'columns' : dict like {column -> {index -> value}} + - 'values' : just the values array + - 'table' : dict like {'schema': {schema}, 'data': {data}} describing the data, and the data component is like ``orient='records'``. @@ -1739,7 +1740,7 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, See Also -------- - pd.read_json + pandas.read_json Examples -------- From 91f258c21d3654cb37362be265025201d7fd31aa Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 13 Mar 2018 22:25:21 +0100 Subject: [PATCH 3/4] fix some quoting --- pandas/core/generic.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 11bdb2f6637e4..29ee8bc2bc28c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1664,8 +1664,8 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, Parameters ---------- - path_or_buf : string or file handle, default None - File path or object, if None is provided the result is returned as + path_or_buf : string or file handle, optional + File path or object. If not specified, the result is returned as a string. orient : string Indication of expected JSON string format. @@ -1683,8 +1683,8 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, * The format of the JSON string - - 'split' : dict like - {index -> [index], columns -> [columns], data -> [values]} + - 'split' : dict like {'index' -> [index], + 'columns' -> [columns], 'data' -> [values]} - 'records' : list like [{column -> value}, ... , {column -> value}] - 'index' : dict like {index -> {column -> value}} @@ -1697,10 +1697,10 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, .. versionchanged:: 0.20.0. date_format : {None, 'epoch', 'iso'} - Type of date conversion. `epoch` = epoch milliseconds, - `iso` = ISO8601. The default depends on the `orient`. For - `orient='table'`, the default is `'iso'`. For all other orients, - the default is `'epoch'`. + Type of date conversion. 'epoch' = epoch milliseconds, + 'iso' = ISO8601. The default depends on the `orient`. For + ``orient='table'``, the default is 'iso'. For all other orients, + the default is 'epoch'. double_precision : int, default 10 The number of decimal places to use when encoding floating point values. From 579532ad22dc19a1f05da2767493e5e683b8db7e Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 13 Mar 2018 22:26:30 +0100 Subject: [PATCH 4/4] remove incorrect return --- pandas/core/generic.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 29ee8bc2bc28c..bc5b77a8c19cb 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1694,7 +1694,7 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, describing the data, and the data component is like ``orient='records'``. - .. versionchanged:: 0.20.0. + .. versionchanged:: 0.20.0 date_format : {None, 'epoch', 'iso'} Type of date conversion. 'epoch' = epoch milliseconds, @@ -1719,24 +1719,20 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None, throw ValueError if incorrect 'orient' since others are not list like. - .. versionadded:: 0.19.0. + .. versionadded:: 0.19.0 compression : {None, 'gzip', 'bz2', 'xz'} A string representing the compression to use in the output file, only used when the first argument is a filename - .. versionadded:: 0.21.0. + .. versionadded:: 0.21.0 index : boolean, default True Whether to include the index values in the JSON string. Not including the index (``index=False``) is only supported when orient is 'split' or 'table'. - .. versionadded:: 0.23.0. - - Returns - ------- - same type as input object with filtered info axis + .. versionadded:: 0.23.0 See Also --------