TypeError: ‘decimal.Decimal’ object cannot be interpreted as an integer

huangapple go评论79阅读模式
英文:

TypeError: 'decimal.Decimal' object cannot be interpreted as an integer

问题

我正在运行此代码以将sample_time变量转换为日期时间。然而,我收到了这个错误:TypeError: 无法将'decimal.Decimal'对象解释为整数。

如何在不更改我的Python版本为旧的不推荐版本的情况下纠正这个错误?

df['sample_time'] = df['sample_time'].apply(lambda x: datetime.utcfromtimestamp(x / 1000).strftime('%Y-%m-%D %H:%M:%S'))
英文:

I am running this code to convert the sample_time variables to datetime. However, I am receiving this error: TypeError: 'decimal.Decimal' object cannot be interpreted as an integer.

How can I rectify this without changing my python version to an older non deprecated version?

df['sample_time'] = df['sample_time'].apply(lambda x: datetime.utcfromtimestamp(x / 1000).strftime('%Y-%m-%D %H:%M:%S'))

TypeError: ‘decimal.Decimal’ object cannot be interpreted as an integer

答案1

得分: 2

你的 'sample_time' 列的数据类型是 decimal.Decimal。在处理之前,要么将该列转换为 floatint,要么在你的 apply 语句中进行转换:

df['sample_time'] = df['sample_time'].apply(lambda x: datetime.utcfromtimestamp(float(x / 1000)).strftime('%Y-%m-%D %H:%M:%S'))
英文:

Your 'sample_time' column is of decimal.Decimal type. Either convert that column to float or int before processing or do that in your apply statement:

df['sample_time'] = df['sample_time'].apply(lambda x: datetime.utcfromtimestamp(float(x / 1000)).strftime('%Y-%m-%D %H:%M:%S'))

huangapple
  • 本文由 发表于 2023年3月4日 00:50:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定