Skip to content

Add indexName to DataFrame.to_json(orient='split') #17393

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

Closed
Suor opened this issue Aug 31, 2017 · 3 comments
Closed

Add indexName to DataFrame.to_json(orient='split') #17393

Suor opened this issue Aug 31, 2017 · 3 comments
Labels
IO JSON read_json, to_json, json_normalize

Comments

@Suor
Copy link

Suor commented Aug 31, 2017

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame([[1,2,3],[4,5,6]], columns=['a','b','c'])
df.index = df.a
df_copy = pd.io.read.read_json(df.to_json(orient='split'), orient='split')
assert df_copy.index.name == df.index.name

Problem description

I use .to_json(orient='split') as a compact way to serialize and store dataframes, however, I loose index name on dump/load cycle. I would suggest adding "indexName" to json, which should be fairly backwards compatible.

Expected Output

>>>  df.to_json(orient='split')
'{"columns":["a","b","c"],"index":[1,4],"indexName":"a","data":[[1,2,3],[4,5,6]]}'

Behavior should probably stay as it is when index name is None.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.13.final.0 python-bits: 64 OS: Linux OS-release: 4.10.0-28-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: 2.6.4
pip: 9.0.1
setuptools: 36.2.5
Cython: None
numpy: 1.13.1
scipy: 0.16.0
xarray: None
IPython: 5.4.1
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.2
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: 2.6 (dt dec pq3 ext lo64)
jinja2: 2.7.3
s3fs: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Aug 31, 2017

this makes the JSON pretty pandas specific, so really not in favor of this you can simply .reset_index() and make it a column.

@jreback jreback added the IO JSON read_json, to_json, json_normalize label Aug 31, 2017
@Suor
Copy link
Author

Suor commented Sep 10, 2017

This makes dataframe fully recoverable from json. Also, it's already specific since there is both columns and index, and anyone can ignore it, so don't see an issue here.

@WillAyd
Copy link
Member

WillAyd commented Jan 9, 2020

I think this is realistically already achievable with the table orient, so unlikely to add to split

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO JSON read_json, to_json, json_normalize
Projects
None yet
Development

No branches or pull requests

3 participants