Skip to content

Commit 0762111

Browse files
committed
BUG: pd.read_json May Not Maintain Numeric String Index
1 parent d642b67 commit 0762111

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ I/O
269269
for subclasses of ``DataFrame`` or ``Series`` (:issue:`33748`).
270270
- Bug in :func:`json_normalize` resulting in the first element of a generator object not being included in the returned ``DataFrame`` (:issue:`35923`)
271271
- Bug in :func:`read_excel` forward filling :class:`MultiIndex` names with multiple header and index columns specified (:issue:`34673`)
272-
272+
- Bug in :func:`read_json` when ``orient="split"`` does not maintan numeric string index (:issue:`28556`)
273273

274274
Period
275275
^^^^^^

pandas/io/json/_json.py

+4-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,13 @@ 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 name == "index" and self.orient == "split" and len(data):
936+
return data, False
937+
else:
938+
return data, True
942939

943940
def _try_convert_to_date(self, data):
944941
"""

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)