(存入json文件出错)TypeError: Object of type int64 is not JSON serializable

Kara ·
更新时间:2024-09-20
· 714 次阅读

问题描述

因为numpyint类型无法被json化,所以需要将numpyint转为原生类型。

解决方案 # pandas返回的 sex_cnt = marks['sex'].value_counts() type(sex_cnt['男']) # numpy.int64 # 3种转化方法 # examples using a.item() type(np.float32(0).item()) # type(np.float64(0).item()) # type(np.uint32(0).item()) # # examples using np.asscalar(a) type(np.asscalar(np.int16(0))) # type(np.asscalar(np.cfloat(0))) # type(np.asscalar(np.datetime64(0, 'D'))) # type(np.asscalar(np.timedelta64(0, 'D'))) # # 强制转化 int(np.int64(0)) References python中的常见错误
作者:Quant_Learner



JSON NOT IS int64 int object

需要 登录 后方可回复, 如果你还没有账号请 注册新账号