Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 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,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):
albertvillanova marked this conversation as resolved.
Show resolved Hide resolved
# 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)
albertvillanova marked this conversation as resolved.
Show resolved Hide resolved