From dcbb22b076a4cde5828ca7eba85ed818c36dd1dd Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Tue, 5 Feb 2019 23:13:17 +0100 Subject: [PATCH 1/4] BUG: Fix read_json orient='table' without index (#25170) --- pandas/io/json/table_schema.py | 15 ++++++++------- pandas/tests/io/json/test_pandas.py | 9 +++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/io/json/table_schema.py b/pandas/io/json/table_schema.py index 2bd93b19d4225..971386c91944e 100644 --- a/pandas/io/json/table_schema.py +++ b/pandas/io/json/table_schema.py @@ -314,12 +314,13 @@ def parse_table_schema(json, precise_float): df = df.astype(dtypes) - df = df.set_index(table['schema']['primaryKey']) - if len(df.index.names) == 1: - if df.index.name == 'index': - df.index.name = None - else: - df.index.names = [None if x.startswith('level_') else x for x in - df.index.names] + if 'primaryKey' in table['schema']: + df = df.set_index(table['schema']['primaryKey']) + if len(df.index.names) == 1: + if df.index.name == 'index': + df.index.name = None + else: + df.index.names = [None if x.startswith('level_') else x for x in + df.index.names] return df diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 23c40276072d6..16fab6e74f51d 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -1262,3 +1262,12 @@ def test_index_false_error_to_json(self, orient): "'orient' is 'split' or 'table'") with pytest.raises(ValueError, match=msg): df.to_json(orient=orient, index=False) + + @pytest.mark.parametrize('orient', ['split', 'table']) + def test_index_false_from_json_to_json(self, orient): + # GH25170 + # Test index=False in from_json to_json + df = DataFrame({'a': [1, 2], 'b': [3, 4]}) + dfjson = df.to_json(orient=orient, index=False) + result = read_json(dfjson, orient=orient) + assert_frame_equal(result, df) From 23939964995f6f7fab5acd5162b0d7f066184eb1 Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Wed, 6 Feb 2019 06:44:20 +0100 Subject: [PATCH 2/4] Address requested changes --- pandas/tests/io/json/test_pandas.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 16fab6e74f51d..51a1d5488b191 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -1264,10 +1264,11 @@ def test_index_false_error_to_json(self, orient): df.to_json(orient=orient, index=False) @pytest.mark.parametrize('orient', ['split', 'table']) - def test_index_false_from_json_to_json(self, orient): + @pytest.mark.parametrize('index', [True, False]) + def test_index_false_from_json_to_json(self, orient, index): # GH25170 # Test index=False in from_json to_json - df = DataFrame({'a': [1, 2], 'b': [3, 4]}) - dfjson = df.to_json(orient=orient, index=False) + expected = DataFrame({'a': [1, 2], 'b': [3, 4]}) + dfjson = expected.to_json(orient=orient, index=index) result = read_json(dfjson, orient=orient) - assert_frame_equal(result, df) + assert_frame_equal(result, expected) From 6d23237eff716df817cda997b6118248441b30fe Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Wed, 6 Feb 2019 08:17:30 +0100 Subject: [PATCH 3/4] Add whatsnew note --- doc/source/whatsnew/v0.24.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.2.rst b/doc/source/whatsnew/v0.24.2.rst index a047ad46e4887..274f92a344828 100644 --- a/doc/source/whatsnew/v0.24.2.rst +++ b/doc/source/whatsnew/v0.24.2.rst @@ -50,6 +50,7 @@ Bug Fixes **I/O** - Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`) +- KeyError raised by :func:`parse_table_schema` when reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` - - From 27dc024786326f62fc4c0f39c9d240819cc9c744 Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Fri, 8 Feb 2019 07:20:58 +0100 Subject: [PATCH 4/4] Fix whatsnew note --- doc/source/whatsnew/v0.24.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.2.rst b/doc/source/whatsnew/v0.24.2.rst index 274f92a344828..7f35969fff790 100644 --- a/doc/source/whatsnew/v0.24.2.rst +++ b/doc/source/whatsnew/v0.24.2.rst @@ -50,7 +50,7 @@ Bug Fixes **I/O** - Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`) -- KeyError raised by :func:`parse_table_schema` when reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` +- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`) - -