pandas Python 错误:TypeError:'Timestamp' 类型的对象不是 JSON 可序列化的'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50404559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:34:28  来源:igfitidea点击:

Python error : TypeError: Object of type 'Timestamp' is not JSON serializable'

pythonjsonpandasdatetime

提问by dark horse

I have a Dataframe that has a time stamp column of type 'datetime64[ns]'. When I try to insert it to Salesforce platform get an error 'TypeError: Object of type 'Timestamp' is not JSON serializable'. How could I change this timestamp column to have it updated properly. Given below is the view of the Dataframe.

我有一个 Dataframe,它有一个类型为“datetime64[ns]”的时间戳列。当我尝试将其插入 Salesforce 平台时,出现错误“ TypeError: 'Timestamp' 类型的对象不是 JSON 可序列化的”。我如何更改此时间戳列以使其正确更新。下面给出的是数据框的视图。

Id,Name,Date,Type
1,ProdA,2018-05-18 04:45:08,S
1,ProdB,2018-05-18 02:15:00,S
1,ProdC,2018-05-16 10:20:00,S

Datatype for each of these 4 columns:

这 4 列中每一列的数据类型:

Id                                     object
Name                                   object
Date                           datetime64[ns]
Type                                   object
dtype: object

Could anyone assist on this. Thanks.

任何人都可以提供帮助。谢谢。

回答by jezrael

You can try convert datetime to string:

您可以尝试将日期时间转换为字符串:

df['Date'] = df['Date'].astype(str)

Or:

或者:

df['Date'] = df['Date'].dt.strftime('%Y-%m-%d %H:%M:%S')

print (df.dtypes)
Id      object
Name    object
Date    object
Type    object
dtype: object