Skip to content

Commit 748819c

Browse files
albertvillanovaAlbert Villanova del Moral
authored and
Albert Villanova del Moral
committed
BUG: Fix read_json orient='table' without index (pandas-dev#25170) (pandas-dev#25171)
1 parent b134253 commit 748819c

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Bug Fixes
5252
**I/O**
5353

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

pandas/io/json/table_schema.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,13 @@ def parse_table_schema(json, precise_float):
314314

315315
df = df.astype(dtypes)
316316

317-
df = df.set_index(table['schema']['primaryKey'])
318-
if len(df.index.names) == 1:
319-
if df.index.name == 'index':
320-
df.index.name = None
321-
else:
322-
df.index.names = [None if x.startswith('level_') else x for x in
323-
df.index.names]
317+
if 'primaryKey' in table['schema']:
318+
df = df.set_index(table['schema']['primaryKey'])
319+
if len(df.index.names) == 1:
320+
if df.index.name == 'index':
321+
df.index.name = None
322+
else:
323+
df.index.names = [None if x.startswith('level_') else x for x in
324+
df.index.names]
324325

325326
return df

pandas/tests/io/json/test_pandas.py

+10
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,13 @@ def test_index_false_error_to_json(self, orient):
12621262
"'orient' is 'split' or 'table'")
12631263
with pytest.raises(ValueError, match=msg):
12641264
df.to_json(orient=orient, index=False)
1265+
1266+
@pytest.mark.parametrize('orient', ['split', 'table'])
1267+
@pytest.mark.parametrize('index', [True, False])
1268+
def test_index_false_from_json_to_json(self, orient, index):
1269+
# GH25170
1270+
# Test index=False in from_json to_json
1271+
expected = DataFrame({'a': [1, 2], 'b': [3, 4]})
1272+
dfjson = expected.to_json(orient=orient, index=index)
1273+
result = read_json(dfjson, orient=orient)
1274+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)