Skip to content

Commit bb21639

Browse files
committed
BUG: pd.read_json May Not Maintain Numeric String Index
1 parent 7f912a4 commit bb21639

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

pandas/io/json/_json.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
from pandas.errors import AbstractMethodError
1919
from pandas.util._decorators import deprecate_kwarg, deprecate_nonkeyword_arguments, doc
2020

21-
from pandas.core.dtypes.common import ensure_str, is_period_dtype
22-
21+
from pandas.core.dtypes.common import ensure_str, is_period_dtype, is_string_like_dtype
22+
from pandas.core.indexes.api import Index
2323
from pandas import DataFrame, MultiIndex, Series, isna, notna, to_datetime
2424
from pandas.core import generic
2525
from pandas.core.construction import create_series_with_explicit_dtype
@@ -892,14 +892,14 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
892892
if result:
893893
return new_data, True
894894

895-
result = False
895+
result = True
896896

897897
if data.dtype == "object":
898-
898+
if isinstance(data, Index) and name == 'index' and self.orient == 'split' and len(data):
899+
result = False
899900
# try float
900901
try:
901902
data = data.astype("float64")
902-
result = True
903903
except (TypeError, ValueError):
904904
pass
905905

@@ -910,7 +910,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
910910
# coerce floats to 64
911911
try:
912912
data = data.astype("float64")
913-
result = True
914913
except (TypeError, ValueError):
915914
pass
916915

@@ -922,7 +921,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
922921
new_data = data.astype("int64")
923922
if (new_data == data).all():
924923
data = new_data
925-
result = True
926924
except (TypeError, ValueError, OverflowError):
927925
pass
928926

@@ -932,7 +930,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
932930
# coerce floats to 64
933931
try:
934932
data = data.astype("int64")
935-
result = True
936933
except (TypeError, ValueError):
937934
pass
938935

pandas/tests/io/json/test_pandas.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
192192
# to disambiguate whether those keys actually were strings or numeric
193193
# beforehand and numeric wins out.
194194
# TODO: Split should be able to support this
195-
if convert_axes and (orient in ("split", "index", "columns")):
195+
if convert_axes and (orient in ("index", "columns")):
196196
expected.columns = expected.columns.astype(np.int64)
197197
expected.index = expected.index.astype(np.int64)
198198
elif orient == "records" and convert_axes:
199199
expected.columns = expected.columns.astype(np.int64)
200+
elif convert_axes and orient == "split":
201+
expected.columns = expected.columns.astype(np.int64)
202+
expected.index = expected.index.astype(str)
200203

201204
assert_json_roundtrip_equal(result, expected, orient)
202205

0 commit comments

Comments
 (0)