Python错误:TypeError:“时间戳记"类型的对象不可JSON序列化" [英] Python error : TypeError: Object of type 'Timestamp' is not JSON serializable'

查看:94
本文介绍了Python错误:TypeError:“时间戳记"类型的对象不可JSON序列化"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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列中每列的数据类型:

Datatype for each of these 4 columns:

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

任何人都可以提供协助.谢谢.

Could anyone assist on this. Thanks.

推荐答案

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

You can try convert datetime to string:

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:“时间戳记"类型的对象不可JSON序列化"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆