【问题标题】:int too big to convert in pythonint 太大,无法在 python 中转换
【发布时间】:2020-05-13 09:26:35
【问题描述】:

我从模拟数据中计算出“持续时间”值。这不过是计算的“小时”值。我写了以下代码:

Duration = 16571835.2920344
Dyn_ETA = pd.Timedelta(hours=Duration)
Dyn_ETA1 = Dyn_ETA.total_seconds()
hours = Dyn_ETA1/3600
result_Dyn = '{0:02.0f}:{1:02.0f}'.format(*divmod(hours * 60, 60))

但我遇到以下错误:

OverflowError: int too big to convert 
for
Dyn_ETA = pd.Timedelta(hours=Duration)

我不会在实时数据中得到这个“持续时间”值。但作为安全措施,我该如何解决呢?

【问题讨论】:

  • 纯 Python 可以处理任意大小的整数。数据框不是。使用datetime 库!

标签: python dataframe int timedelta


【解决方案1】:

您在这里使用了错误的工具。 pd.Timedelta 将持续时间处理为 int64 纳秒值。这或多或少给出了 5124095 小时(大约 584 年)的最大值。

您的初始持续时间接近最大值的两倍,无法以这种方式表达。句号。

但你不需要它:

hour = Duration # taking the total_seconds() from the Timedelta in hour divided by 3600 is a no-op
result_Dyn = '{0:02.0f}:{1:02.0f}'.format(*divmod(hours * 60, 60))

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 2015-10-12
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2011-03-14
    • 2019-10-27
    相关资源
    最近更新 更多