【问题标题】:Python 3 Timedelta OverflowErrorPython 3 时间增量溢出错误
【发布时间】:2018-03-13 18:38:04
【问题描述】:

我有一个大型数据库,我正在加载到内存缓存中。我有一个流程可以每天遍历数据。

最近这个进程开始抛出如下错误:

OverflowError: date value out of range 代表线路

start_day = start_day - datetime.timedelta(days = 1)

这是在 Ubuntu 14.04.5 上的 Python 3.4.3 中运行的

【问题讨论】:

  • start_day的初始值是多少?
  • datetime.utcnow().date() 进程开始运行时

标签: python django python-3.x datetime overflow


【解决方案1】:

您已到达datetime.date.min,或第一年的一月一日:

>>> from datetime import date, timedelta
>>> date.min
datetime.date(1, 1, 1)
>>> date.min - timedelta(days=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range

如果您从 datetime.date.today() 开始,那么您的代码需要 736k 多步才能到达那里:

>>> date.today().toordinal()
736766

您的代码可能在某处存在错误,该错误经常减去。

【讨论】:

    【解决方案2】:

    为了克服错误,你可以改变

    datetime.date(1, 1, 1)
    

    更多更大的价值,例如

    datetime.date(2, 2, 2)
    

    但如果您编辑时间列,例如请注意您在此列中的日期是人工 2、2、2 或其他东西

    【讨论】:

      【解决方案3】:

      如果您在使用 pandas 读取 excel/csv 文件时遇到此问题,请检查您的 excel/csv 文件,任何一列或多列都会包含类似 ############# 的值,即表示值为负 ex: (-11111) 或日期太长而无法放入单元格中

      import pandas as pd
      
      df = pd.read_excel(file path,dtype='string') #this will convert all the column 
           type to string
      
      or if you want to convert specific column then 
      
      df = pd.read_excel(file path,converters={'column name':str})

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-15
        • 2023-02-17
        • 2019-11-13
        • 2017-02-03
        • 1970-01-01
        相关资源
        最近更新 更多