Skip to content

Commit

Permalink
BUG: Fix read_json orient='table' without index (pandas-dev#25170) (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvillanova authored and Pingviinituutti committed Feb 28, 2019
1 parent 353e505 commit 4e82f87
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Bug Fixes
**I/O**

- Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`)
- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`)
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)
-

Expand Down
15 changes: 8 additions & 7 deletions pandas/io/json/table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,3 +1262,13 @@ 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'])
@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
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, expected)

0 comments on commit 4e82f87

Please sign in to comment.