gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:19:34 44 4
gpt4 key购买 nike

我有一个 Dataframe,它有一个类型为“datetime64[ns]”的时间戳列。当我尝试将其插入 Salesforce 平台时出现错误“TypeError:‘Timestamp’类型的对象不是 JSON 可序列化的”。我怎样才能更改此时间戳列以使其正确更新。下面给出的是 Dataframe 的 View 。

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

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

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

任何人都可以协助解决这个问题。谢谢。

最佳答案

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

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

或者:

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

关于Python 错误 : TypeError: Object of type 'Timestamp' is not JSON serializable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50404559/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com