From cf34164451839ec185ef5ee2b4924a28770d63c7 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Sun, 10 Jun 2018 15:14:31 +0530 Subject: [PATCH 1/2] Update json.py BUG : read_json with table='orient' causes unexpected type coercion #21345 --- pandas/io/json/json.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/io/json/json.py b/pandas/io/json/json.py index 1627b2f4d3ec3..be0df071ecbb7 100644 --- a/pandas/io/json/json.py +++ b/pandas/io/json/json.py @@ -706,12 +706,12 @@ def _try_convert_data(self, name, data, use_dtypes=True, except (TypeError, ValueError): pass - # do't coerce 0-len data + # don't coerce 0-len data if len(data) and (data.dtype == 'float' or data.dtype == 'object'): - # coerce ints if we can + # coerce float if we can try: - new_data = data.astype('int64') + new_data = data.astype('float64') if (new_data == data).all(): data = new_data result = True @@ -721,7 +721,7 @@ def _try_convert_data(self, name, data, use_dtypes=True, # coerce ints to 64 if data.dtype == 'int': - # coerce floats to 64 + # coerce ints to 64 try: data = data.astype('int64') result = True From 5b6b6d23689edab0bce386f8c6fb004b0a190f3f Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Sun, 10 Jun 2018 16:37:55 +0530 Subject: [PATCH 2/2] Update json.py --- pandas/io/json/json.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pandas/io/json/json.py b/pandas/io/json/json.py index be0df071ecbb7..641265a95e93b 100644 --- a/pandas/io/json/json.py +++ b/pandas/io/json/json.py @@ -707,11 +707,11 @@ def _try_convert_data(self, name, data, use_dtypes=True, pass # don't coerce 0-len data - if len(data) and (data.dtype == 'float' or data.dtype == 'object'): + if len(data) and (data.dtype == 'object'): - # coerce float if we can + # coerce int if we can try: - new_data = data.astype('float64') + new_data = data.astype('int64') if (new_data == data).all(): data = new_data result = True @@ -728,6 +728,16 @@ def _try_convert_data(self, name, data, use_dtypes=True, except (TypeError, ValueError): pass + # coerce floats to 64 + if data.dtype == 'float': + + # coerce floats to 64 + try: + data = data.astype('float64') + result = True + except (TypeError, ValueError): + pass + return data, result def _try_convert_to_date(self, data):