Skip to content

Error when converting df to json table (utc timezone date time object causes the error) #39537

Open
@franz101

Description

@franz101

When converting df.to_json(orient="table",index=False)
and there are datetime.now(timezone.utc) objects in the table it causes the following error (Without orient table it works though.):

`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
----> 1 x = current_opportunites.to_json(orient="table",index=False)

/opt/conda/lib/python3.8/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, storage_options)
2463 indent = indent or 0
2464
-> 2465 return json.to_json(
2466 path_or_buf=path_or_buf,
2467 obj=self,

/opt/conda/lib/python3.8/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, storage_options)
83 raise NotImplementedError("'obj' should be a Series or a DataFrame")
84
---> 85 s = writer(
86 obj,
87 orient=orient,

/opt/conda/lib/python3.8/site-packages/pandas/io/json/_json.py in init(self, obj, orient, date_format, double_precision, ensure_ascii, date_unit, index, default_handler, indent)
249 raise ValueError(msg)
250
--> 251 self.schema = build_table_schema(obj, index=self.index)
252
253 # NotImplemented on a column MultiIndex

/opt/conda/lib/python3.8/site-packages/pandas/io/json/_table_schema.py in build_table_schema(data, index, primary_key, version)
261 if data.ndim > 1:
262 for column, s in data.items():
--> 263 fields.append(convert_pandas_type_to_json_field(s))
264 else:
265 fields.append(convert_pandas_type_to_json_field(data))

/opt/conda/lib/python3.8/site-packages/pandas/io/json/_table_schema.py in convert_pandas_type_to_json_field(arr)
122 field["freq"] = dtype.freq.freqstr
123 elif is_datetime64tz_dtype(dtype):
--> 124 field["tz"] = dtype.tz.zone
125 return field
126

AttributeError: 'datetime.timezone' object has no attribute 'zone'`

elif is_datetime64tz_dtype(dtype):

Will have a deeper look later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO JSONread_json, to_json, json_normalizeTimezonesTimezone data dtype

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions