BUG: PeriodIndex does not survive JSON serialization roundtrip · Issue #44720 · pandas-dev/pandas (original) (raw)

Reproducible Example

import pandas as pd df = pd.DataFrame( {"column": range(10, 20, 2)}, index=pd.PeriodIndex( year=[2020, 2020, 2020, 2020, 2021], quarter=[1, 2, 3, 4, 1] ), )

df_from_json = pd.read_json(df.to_json(orient='table'), orient='table') assert df.equals(df_from_json)

Issue Description

The index should make the roundtrip.

Possible related issue: #32665

Expected Behavior

From my point of view, this issue has two parts, the title and example report on the overall issue which is the failure to roundtrip the index when using JSON to serialize the DataFrame.

The second part of the issue is that when reading from JSON, the index gets serialized to a DatetimeIndex with freq=None.
In the JSON however, the frequency is there, see the output of df.to_json(orient='table') for the previous example:

{'data': [{'column': 10, 'index': '2020-01-01T00:00:00.000Z'},
          {'column': 12, 'index': '2020-04-01T00:00:00.000Z'},
          {'column': 14, 'index': '2020-07-01T00:00:00.000Z'},
          {'column': 16, 'index': '2020-10-01T00:00:00.000Z'},
          {'column': 18, 'index': '2021-01-01T00:00:00.000Z'}],
 'schema': {'fields': [{'freq': 'Q-DEC', 'name': 'index', 'type': 'datetime'},
                       {'name': 'column', 'type': 'integer'}],
            'pandas_version': '0.20.0',
            'primaryKey': ['index']}}

Notice how the index field has the freq correctly set.

Installed Versions

`INSTALLED VERSIONS

commit : 945c9ed766a61c7d2c0a7cbb251b6edebf9cb7d5 python : 3.8.5.final.0 python-bits : 64 OS : Darwin OS-release : 20.6.0 Version : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8 `

`pandas : 1.3.4

numpy : 1.21.3

pytz : 2021.3

dateutil : 2.8.2

pip : 21.3.1

setuptools : 58.3.0

Cython : None

pytest : 6.2.5

hypothesis : None

sphinx : None

blosc : None

feather : None

xlsxwriter : None

lxml.etree : None

html5lib : 1.1

pymysql : None

psycopg2 : None

jinja2 : None

IPython : 7.30.0

pandas_datareader: None

bs4 : None

bottleneck : None

fsspec : None

fastparquet : None

gcsfs : None

matplotlib : None

numexpr : None

odfpy : None

openpyxl : None

pandas_gbq : None

pyarrow : 5.0.0

pyxlsb : None

s3fs : None

scipy : None

sqlalchemy : 1.4.26

tables : None

tabulate : None

xarray : None

xlrd : None

xlwt : None

numba : None`