Skip to content

BUG: pd.read_json May Not Maintain Numeric String Index #38727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ I/O
- Bug in :func:`json_normalize` resulting in the first element of a generator object not being included in the returned ``DataFrame`` (:issue:`35923`)
- Bug in :func:`read_excel` forward filling :class:`MultiIndex` names with multiple header and index columns specified (:issue:`34673`)
- :func:`pandas.read_excel` now respects :func:``pandas.set_option`` (:issue:`34252`)
- Bug in :func:``read_json`` when ``orient="split"`` does not maintan numeric string index (:issue:``28556``)

Period
^^^^^^
Expand Down
13 changes: 6 additions & 7 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,14 +894,11 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
if result:
return new_data, True

result = False

if data.dtype == "object":

# try float
try:
data = data.astype("float64")
result = True
except (TypeError, ValueError):
pass

Expand All @@ -912,7 +909,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
# coerce floats to 64
try:
data = data.astype("float64")
result = True
except (TypeError, ValueError):
pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls revert this to minimize the diff

Expand All @@ -924,7 +920,6 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
new_data = data.astype("int64")
if (new_data == data).all():
data = new_data
result = True
except (TypeError, ValueError, OverflowError):
pass

Expand All @@ -934,11 +929,15 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
# coerce floats to 64
try:
data = data.astype("int64")
result = True
except (TypeError, ValueError):
pass

return data, result
# if we have an index, we want to preserve dtypes
if name == "index" and len(data):
if self.orient == "split":
return data, False

return data, True

def _try_convert_to_date(self, data):
"""
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ def test_roundtrip_str_axes(self, request, orient, convert_axes, numpy, dtype):
# JSON objects. JSON keys are by definition strings, so there's no way
# to disambiguate whether those keys actually were strings or numeric
# beforehand and numeric wins out.
# TODO: Split should be able to support this
if convert_axes and (orient in ("split", "index", "columns")):
if convert_axes and (orient in ("index", "columns")):
expected.columns = expected.columns.astype(np.int64)
expected.index = expected.index.astype(np.int64)
elif orient == "records" and convert_axes:
expected.columns = expected.columns.astype(np.int64)
elif convert_axes and orient == "split":
expected.columns = expected.columns.astype(np.int64)

assert_json_roundtrip_equal(result, expected, orient)

Expand Down