Skip to content

Commit 11b0db1

Browse files
theonikoluckyvs1
authored andcommitted
BUG: pd.read_json May Not Maintain Numeric String Index (pandas-dev#38727)
1 parent bd42bc6 commit 11b0db1

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ I/O
276276
- Bug in :func:`json_normalize` resulting in the first element of a generator object not being included in the returned ``DataFrame`` (:issue:`35923`)
277277
- Bug in :func:`read_excel` forward filling :class:`MultiIndex` names with multiple header and index columns specified (:issue:`34673`)
278278
- :func:`pandas.read_excel` now respects :func:``pandas.set_option`` (:issue:`34252`)
279+
- Bug in :func:``read_json`` when ``orient="split"`` does not maintan numeric string index (:issue:`28556`)
279280

280281
Period
281282
^^^^^^

pandas/io/json/_json.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -894,14 +894,11 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
894894
if result:
895895
return new_data, True
896896

897-
result = False
898-
899897
if data.dtype == "object":
900898

901899
# try float
902900
try:
903901
data = data.astype("float64")
904-
result = True
905902
except (TypeError, ValueError):
906903
pass
907904

@@ -912,7 +909,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
912909
# coerce floats to 64
913910
try:
914911
data = data.astype("float64")
915-
result = True
916912
except (TypeError, ValueError):
917913
pass
918914

@@ -924,7 +920,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
924920
new_data = data.astype("int64")
925921
if (new_data == data).all():
926922
data = new_data
927-
result = True
928923
except (TypeError, ValueError, OverflowError):
929924
pass
930925

@@ -934,11 +929,15 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
934929
# coerce floats to 64
935930
try:
936931
data = data.astype("int64")
937-
result = True
938932
except (TypeError, ValueError):
939933
pass
940934

941-
return data, result
935+
# if we have an index, we want to preserve dtypes
936+
if name == "index" and len(data):
937+
if self.orient == "split":
938+
return data, False
939+
940+
return data, True
942941

943942
def _try_convert_to_date(self, data):
944943
"""

pandas/tests/io/json/test_pandas.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,13 @@ def test_roundtrip_str_axes(self, request, orient, convert_axes, numpy, dtype):
193193
# JSON objects. JSON keys are by definition strings, so there's no way
194194
# to disambiguate whether those keys actually were strings or numeric
195195
# beforehand and numeric wins out.
196-
# TODO: Split should be able to support this
197-
if convert_axes and (orient in ("split", "index", "columns")):
196+
if convert_axes and (orient in ("index", "columns")):
198197
expected.columns = expected.columns.astype(np.int64)
199198
expected.index = expected.index.astype(np.int64)
200199
elif orient == "records" and convert_axes:
201200
expected.columns = expected.columns.astype(np.int64)
201+
elif convert_axes and orient == "split":
202+
expected.columns = expected.columns.astype(np.int64)
202203

203204
assert_json_roundtrip_equal(result, expected, orient)
204205

0 commit comments

Comments
 (0)