Skip to content

BUG: Integer column index breaks json roundtrip with orient=table #46392

Open
@rikardn

Description

@rikardn

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

col1 = [1.0, 2.0, 3.5, 6.75]
col2 = [2.1, 3.1, 4.1, 5.1]
df = pd.DataFrame({1: col1, 2:col2}, index=[110, 112, 113, 121])
df.index.name = 'ID'
s = df.to_json(orient='table')
new = pd.read_json(s, orient='table')

Issue Description

The new dataframe will become

      1   2
ID         
110 NaN NaN
112 NaN NaN
113 NaN NaN
121 NaN NaN

Expected Behavior

The expected dataframe would look like this:

        1    2
ID            
110  1.00  2.1
112  2.00  3.1
113  3.50  4.1
121  6.75  5.1

Changing to strings instead of integers in the column index will give the expected result:

col1 = [1.0, 2.0, 3.5, 6.75]
col2 = [2.1, 3.1, 4.1, 5.1]
df = pd.DataFrame({'1': col1, '2':col2}, index=[110, 112, 113, 121])
df.index.name = 'ID'
s = df.to_json(orient='table')
new = pd.read_json(s, orient='table')

Installed Versions

This crashed in my environment with the error assert '_distutils' in core.__file__, core.__file__ raised from lib/python3.9/site-packages/_distutils_hack/__init__.py", line 59, in ensure_local_distutils

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO JSONread_json, to_json, json_normalize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions