Skip to content

Commit db10b73

Browse files
committed
BUG: pd.read_json May Not Maintain Numeric String Index
1 parent 2b4bcf2 commit db10b73

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ I/O
267267
- Bug in :func:`to_hdf` raising ``KeyError`` when trying to apply
268268
for subclasses of ``DataFrame`` or ``Series`` (:issue:`33748`).
269269
- Bug in :func:`json_normalize` resulting in the first element of a generator object not being included in the returned ``DataFrame`` (:issue:`35923`)
270-
270+
- Bug in :func:`read_json` does not maintan numeric string index (:issue:`28556`)
271271

272272
Period
273273
^^^^^^

pandas/io/json/_json.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pandas.core import generic
2727
from pandas.core.construction import create_series_with_explicit_dtype
2828
from pandas.core.generic import NDFrame
29+
from pandas.core.indexes.api import Index
2930
from pandas.core.reshape.concat import concat
3031

3132
from pandas.io.common import (
@@ -894,14 +895,19 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
894895
if result:
895896
return new_data, True
896897

897-
result = False
898+
result = True
898899

899900
if data.dtype == "object":
900-
901+
if (
902+
isinstance(data, Index)
903+
and name == "index"
904+
and self.orient == "split"
905+
and len(data)
906+
):
907+
result = False
901908
# try float
902909
try:
903910
data = data.astype("float64")
904-
result = True
905911
except (TypeError, ValueError):
906912
pass
907913

@@ -912,7 +918,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
912918
# coerce floats to 64
913919
try:
914920
data = data.astype("float64")
915-
result = True
916921
except (TypeError, ValueError):
917922
pass
918923

@@ -924,7 +929,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
924929
new_data = data.astype("int64")
925930
if (new_data == data).all():
926931
data = new_data
927-
result = True
928932
except (TypeError, ValueError, OverflowError):
929933
pass
930934

@@ -934,7 +938,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
934938
# coerce floats to 64
935939
try:
936940
data = data.astype("int64")
937-
result = True
938941
except (TypeError, ValueError):
939942
pass
940943

pandas/tests/io/json/test_pandas.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
191191
# JSON objects. JSON keys are by definition strings, so there's no way
192192
# to disambiguate whether those keys actually were strings or numeric
193193
# beforehand and numeric wins out.
194-
# TODO: Split should be able to support this
195-
if convert_axes and (orient in ("split", "index", "columns")):
194+
if convert_axes and (orient in ("index", "columns")):
196195
expected.columns = expected.columns.astype(np.int64)
197196
expected.index = expected.index.astype(np.int64)
198197
elif orient == "records" and convert_axes:
199198
expected.columns = expected.columns.astype(np.int64)
199+
elif convert_axes and orient == "split":
200+
expected.columns = expected.columns.astype(np.int64)
200201

201202
assert_json_roundtrip_equal(result, expected, orient)
202203

0 commit comments

Comments
 (0)