Skip to content

Commit 01488e4

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

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

pandas/io/json/_json.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from pandas.util._decorators import deprecate_kwarg, deprecate_nonkeyword_arguments, doc
2020

2121
from pandas.core.dtypes.common import ensure_str, is_period_dtype
22-
2322
from pandas import DataFrame, MultiIndex, Series, isna, notna, to_datetime
2423
from pandas.core import generic
2524
from pandas.core.construction import create_series_with_explicit_dtype
2625
from pandas.core.generic import NDFrame
26+
from pandas.core.indexes.api import Index
2727
from pandas.core.reshape.concat import concat
2828

2929
from pandas.io.common import (
@@ -892,14 +892,19 @@ 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 (
899+
isinstance(data, Index)
900+
and name == "index"
901+
and self.orient == "split"
902+
and len(data)
903+
):
904+
result = False
899905
# try float
900906
try:
901907
data = data.astype("float64")
902-
result = True
903908
except (TypeError, ValueError):
904909
pass
905910

@@ -910,7 +915,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
910915
# coerce floats to 64
911916
try:
912917
data = data.astype("float64")
913-
result = True
914918
except (TypeError, ValueError):
915919
pass
916920

@@ -922,7 +926,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
922926
new_data = data.astype("int64")
923927
if (new_data == data).all():
924928
data = new_data
925-
result = True
926929
except (TypeError, ValueError, OverflowError):
927930
pass
928931

@@ -932,7 +935,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
932935
# coerce floats to 64
933936
try:
934937
data = data.astype("int64")
935-
result = True
936938
except (TypeError, ValueError):
937939
pass
938940

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)