-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Error when converting df to json table (utc timezone date time object causes the error) #39537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
can you share a minimalist reproducible snippet to validate? |
test = pd.DataFrame([{"name":"foo","time":datetime.now(timezone.utc)}]).to_json( this works like a charm |
Not sure what the best practise is with datetime, json and pandas. won't parse the dates... also won't work with date_format='iso' this will return the datetimeformat... Maybe I made some small mistakes. what is the best practice here with datetime and utc timezones? |
The error you are getting can be patched with:
Apparently the way you are creating a DateTime object, e.g.:
leads to the Note if you define your datetimes using pandas natives then you don't suffer the error in the first place:
also the following works if you convert your objects, which are initialised with a timezone, to pandas native :
|
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'`
pandas/pandas/io/json/_table_schema.py
Line 123 in d378852
Will have a deeper look later.
The text was updated successfully, but these errors were encountered: