Skip to content

Commit

Permalink
Fix bug when index type casting in read_json table (pandas-dev#25433)
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Villanova del Moral committed Feb 24, 2019
1 parent 85572de commit f536046
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ I/O

- Fixed bug in missing text when using :meth:`to_clipboard` if copying utf-16 characters in Python 3 on Windows (:issue:`25040`)
- Bug in :func:`read_json` for ``orient='table'`` when it tries to infer dtypes by default, which is not applicable as dtypes are already defined in the JSON schema (:issue:`21345`)
- Bug in :func:`read_json` for ``orient='table'`` and float index, as it infers index dtype by default, which is not applicable because index dtype is already defined in the JSON schema (:issue:`25433`)
-
-
-
Expand Down
13 changes: 12 additions & 1 deletion pandas/io/json/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def _write(self, obj, orient, double_precision, ensure_ascii,


def read_json(path_or_buf=None, orient=None, typ='frame', dtype=None,
convert_axes=True, convert_dates=True, keep_default_dates=True,
convert_axes=None, convert_dates=True, keep_default_dates=True,
numpy=False, precise_float=False, date_unit=None, encoding=None,
lines=False, chunksize=None, compression='infer'):
"""
Expand Down Expand Up @@ -289,6 +289,13 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=None,
convert_axes : boolean, default True
Try to convert the axes to the proper dtypes.
Not applicable with ``orient='table'``.
.. versionchanged:: 0.25
Not applicable with ``orient='table'``.
convert_dates : boolean, default True
List of columns to parse for dates; If True, then try to parse
datelike columns default is True; a column label is datelike if
Expand Down Expand Up @@ -417,8 +424,12 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=None,

if orient == 'table' and dtype:
raise ValueError("cannot pass both dtype and orient='table'")
if orient == 'table' and convert_axes:
raise ValueError("cannot pass both convert_axes and orient='table'")

dtype = orient != 'table' if dtype is None else dtype
if convert_axes is None:
convert_axes = orient != 'table'

compression = _infer_compression(path_or_buf, compression)
filepath_or_buffer, _, compression, should_close = get_filepath_or_buffer(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,14 @@ def test_data_frame_size_after_to_json(self):

assert size_before == size_after

@pytest.mark.parametrize('index', [None, [1., 2.], ['1', '2'],
['1.', '2.']])
def test_from_json_to_json_table_index(self, index):
expected = DataFrame({'a': [1, 2]}, index=index)
dfjson = expected.to_json(orient='table')
result = pd.read_json(dfjson, orient='table')
assert_frame_equal(result, expected)

def test_from_json_to_json_table_dtypes(self):
# GH21345
expected = pd.DataFrame({'a': [1, 2], 'b': [3., 4.], 'c': ['5', '6']})
Expand Down

0 comments on commit f536046

Please sign in to comment.