Skip to content

Commit bfd8e89

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

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

pandas/io/json/_json.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pandas.core import generic
2525
from pandas.core.construction import create_series_with_explicit_dtype
2626
from pandas.core.generic import NDFrame
27+
from pandas.core.indexes.api import Index
2728
from pandas.core.reshape.concat import concat
2829

2930
from pandas.io.common import (
@@ -892,14 +893,19 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
892893
if result:
893894
return new_data, True
894895

895-
result = False
896+
result = True
896897

897898
if data.dtype == "object":
898-
899+
if (
900+
isinstance(data, Index)
901+
and name == "index"
902+
and self.orient == "split"
903+
and len(data)
904+
):
905+
result = False
899906
# try float
900907
try:
901908
data = data.astype("float64")
902-
result = True
903909
except (TypeError, ValueError):
904910
pass
905911

@@ -910,7 +916,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
910916
# coerce floats to 64
911917
try:
912918
data = data.astype("float64")
913-
result = True
914919
except (TypeError, ValueError):
915920
pass
916921

@@ -922,7 +927,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
922927
new_data = data.astype("int64")
923928
if (new_data == data).all():
924929
data = new_data
925-
result = True
926930
except (TypeError, ValueError, OverflowError):
927931
pass
928932

@@ -932,7 +936,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
932936
# coerce floats to 64
933937
try:
934938
data = data.astype("int64")
935-
result = True
936939
except (TypeError, ValueError):
937940
pass
938941

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)