Repository: pandas-dev/pandas
Pull request: BUG: Fix read_json orient='table' without index (#25170)
- [x] closes #25170
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Problem statement:
Error when read_json created by to_json with orient='table and index=False
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
dfjson = df.to_json(orient='table', index=False)
result = pd.read_json(dfjson, orient='table')
```
#### Problem description
An error is raised while the original DataFrame should be recovered:
```shell
pandas/io/json/table_schema.py in parse_table_schema(json, precise_float)
    315     df = df.astype(dtypes)
    316 
--> 317     df = df.set_index(table['schema']['primaryKey'])
    318     if len(df.index.names) == 1:
    319         if df.index.name == 'index':

KeyError: 'primaryKey'
```

My pandas version is 0.24.0.

#### Expected Output
The expected output `result` should be the original DataFrame `df`.

#### Output of ``pd.show_versions()``

<details>
pandas/util/_print_versions.py in <lambda>(mod)
     78         ("blosc", lambda mod: mod.__version__),
     79         ("bottleneck", lambda mod: mod.__version__),
---> 80         ("tables", lambda mod: mod.__version__),
     81         ("numexpr", lambda mod: mod.__version__),
     82         ("feather", lambda mod: mod.__version__),

AttributeError: module 'tables' has no attribute '__version__'
</details>

Hints from issue discussion:
Thanks for the report. Investigation and PRs are always welcome.

Commit message: BUG: Fix read_json orient='table' without index (#25170)

Patch to review:
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)


Code review comments:
diff_hunk: @@ -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)
path: pandas/tests/io/json/test_pandas.py
text: @user1:
Usually like to do `assert_frame_equal(result, expected)` so if you could update naming conventions here would be great