Skip to content

to_json of Series with period dtype results in AttributeError #31917

Open
@gerrymanoim

Description

@gerrymanoim

Code Sample, a copy-pastable example if possible

import pandas as pd
test = pd.Series(pd.period_range('1/1/2011', freq='B', periods=3))
test.to_json(None, orient='table')

Leads to:

~/miniconda3/envs/qgrid/lib/python3.7/site-packages/pandas/core/generic.py in to_json(self, path_or_buf, orient, date_format, double_precision, force_ascii, date_unit, default_handler, lines, compression, index, indent)
   2362             compression=compression,
   2363             index=index,
-> 2364             indent=indent,
   2365         )
   2366

~/miniconda3/envs/qgrid/lib/python3.7/site-packages/pandas/io/json/_json.py in to_json(path_or_buf, obj, orient, date_format, double_precision, force_ascii, date_unit, default_handler, lines, compression, index, indent)
     83         default_handler=default_handler,
     84         index=index,
---> 85         indent=indent,
     86     ).write()
     87

~/miniconda3/envs/qgrid/lib/python3.7/site-packages/pandas/io/json/_json.py in __init__(self, obj, orient, date_format, double_precision, ensure_ascii, date_unit, index, default_handler, indent)
    289             raise ValueError(msg)
    290
--> 291         self.schema = build_table_schema(obj, index=self.index)
    292
    293         # NotImplemented on a column MultiIndex

~/miniconda3/envs/qgrid/lib/python3.7/site-packages/pandas/io/json/_table_schema.py in build_table_schema(data, index, primary_key, version)
    251     if data.ndim > 1:
    252         for column, s in data.items():
--> 253             fields.append(convert_pandas_type_to_json_field(s))
    254     else:
    255         fields.append(convert_pandas_type_to_json_field(data))

~/miniconda3/envs/qgrid/lib/python3.7/site-packages/pandas/io/json/_table_schema.py in convert_pandas_type_to_json_field(arr, dtype)
    115         field["ordered"] = ordered
    116     elif is_period_dtype(arr):
--> 117         field["freq"] = arr.freqstr
    118     elif is_datetime64tz_dtype(arr):
    119         if hasattr(arr, "dt"):

~/miniconda3/envs/qgrid/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5273                 return self[name]
-> 5274             return object.__getattribute__(self, name)
   5275
   5276     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'freqstr'

This issue also crops up in dataframes where a column is a period Series and orient='table'.

Problem description

I can't find anything that says this would be unsupported.

Expected Output

Json string of series representation.

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0.post20200210
Cython : None
pytest : 5.3.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIO JSONread_json, to_json, json_normalizePeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions