read_json changes dtype (int => float) · Issue #12866 · pandas-dev/pandas (original) (raw)

Hi there!

This might be a well-known problem but could not find a track/explaination about it. When reading a json to create a pandas object (Series or DataFrame), the index dtype is changed from int to float.

The doc only mentions the inverse trans-typing:

a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1.

Is this behaviour expected as well? Is the only solution giving the read_json a dtype argument?

Code Sample, a copy-pastable example if possible

    In [1]: import pandas as pd

    In [2]: s = pd.Series([11, 12, 13])

    In [3]: s
    Out[3]: 
    0    11
    1    12
    2    13
    dtype: int64

    In [4]: s.to_json()
    Out[4]: '{"0":11,"1":12,"2":13}'

    In [5]: pd.read_json(s.to_json(), typ="serie")
    Out[5]:  
    0.0    11
    1.0    12
    2.0    13
    dtype: int64

    In [6]: s.index.dtype  
    Out[6]: dtype('int64')

    In [7]: pd.read_json(s.to_json(), typ="serie").index.dtype
    Out[7]: dtype('float64')

Expected Output

I would like to recover the integer index for the new Series :

    In [5]: pd.read_json(s.to_json(), typ="serie")
    Out[5]:  
    0    11
    1    12
    2    13
    dtype: int64

    In [7]: pd.read_json(s.to_json(), typ="serie").index.dtype
    Out[7]: dtype('int64')

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Linux
OS-release: 3.16.0-44-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: fr_FR.UTF-8

pandas: 0.18.0
nose: 1.3.4
pip: 1.5.6
setuptools: 12.2
Cython: None
numpy: 1.11.0
scipy: 0.14.1
statsmodels: 0.5.0
xarray: None
IPython: 4.0.0
sphinx: 1.2.3
patsy: 0.3.0
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: 3.1.1
numexpr: 2.4
matplotlib: 1.3.1
openpyxl: 2.2.0-b1
xlrd: 0.9.2
xlwt: 0.7.5
xlsxwriter: None
lxml: 3.4.2
bs4: 4.3.2
html5lib: 0.999
httplib2: 0.9
apiclient: None
sqlalchemy: 1.0.0
pymysql: None
psycopg2: 2.6 (dt dec mx pq3 ext lo64)
jinja2: 2.8
boto: None

Thanks!