Serialise and deserialise pandas periodIndex series

Viewed 105

The pandas Series.to_json() function is creating unreadable JSON when using a PeriodIndex. The error that occurs is: json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 5 (char 4)

I've tried changing the orient, but in all of these combinations of serialising and deserialising the index is lost.

idx = pd.PeriodIndex(['2019', '2020'], freq='A')
series = pd.Series([1, 2], index=idx)
json_series = series.to_json() # This is a demo - in reality I'm storing this in a database, but this code throws the same error
value = json.loads(json_series)

A link to the pandas to_json docs A link to the python json lib docs

The reason I'm not using json.dumps is that the pandas series object is not serialisable.

Python 3.7.3 Pandas 0.24.2

1 Answers

A workaround is to convert PeriodIndex to regular Index before dump and convert it back to PeriodIndex after load:

regular_idx = period_idx.astype(str)
# then dump
# after load
period_idx = pd.to_datetime(regular_idx).to_period()
Related