英文:
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'))
答案1
得分: 2
你的 'sample_time' 列的数据类型是 decimal.Decimal
。在处理之前,要么将该列转换为 float
或 int
,要么在你的 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'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论