BUG: Fix to_json when converting Period column by colonesej · Pull Request #32665 · pandas-dev/pandas (original) (raw)
I made this roundtrip test completely based on the one for a time series with a DatetimeIndex. Some points:
- I could not find any option to make
read_json
treat the data and generate a PeriodIndex whenorient
equals to"column" or "index"
. Conversion does not carry frequency information. I'm not sure what exactly should be the behaviour so just converted manually to Period and inferred freq is OK. - When
orient = "split"
, the test fails asread_json
cannot convert back the dict created byto_json
. It works with a DatetimeIndex, which conversion results in float formatted dates. With Period it converts to separated keys for different aspects of Period object. I could not find where these were determined nor what should be preferred.
To illustrate, using series.to_json(orient='split')
when index is:
DatetimeIndex:
'{"name":null,"index":[946857600000,946944000000,947030400000,947116800000,947203200000,947462400000,947548800000,947635200000,947721600000,947808000000,948067200000,948153600000,948240000000,948326400000,948412800000,948672000000,948758400000,948844800000,948931200000,949017600000,949276800000,949363200000,949449600000,949536000000,949622400000,949881600000,949968000000,950054400000,950140800000,950227200000],"data":[-0.7273489893,0.43763153,0.0064352361,-1.0478282669,0.6605651751,0.2847295244,1.6006677017,-1.2731074841,0.3004426454,0.274394972,0.0402248331,1.5284606656,0.5741635466,-0.3126890855,1.4765392606,0.3663030854,0.3132193099,-0.0730319057,-0.3956868944,1.4626210011,0.2129331654,-0.6671879793,-1.6477379639,0.7626651704,-1.7852083837,0.0707591559,1.3089143648,-1.0282706187,-0.7853299219,-0.2119391625]}'
PeriodIndex:
'{"name":null,"index":[{"day":3,"dayofyear":3,"daysinmonth":31,"freqstr":"B","is_leap_year":true,"month":1,"ordinal":7827,"qyear":2000,"start_time":946857600000,"week":1,"weekofyear":1},