【问题标题】:How to deal with 'out-of-bounds' error for timestamp in the method 'pandas.read_sql()'如何处理“pandas.read_sql()”方法中时间戳的“越界”错误
【发布时间】:2019-03-29 15:47:19
【问题描述】:

环境:

  • 熊猫 0.20.3
  • python 2.7.13

目标和问题:

我正在使用方法 pandas.read_sql() 从 SQL 中检索还包含无效时间戳 (9999-12-30 23:00:00) 值的表。 但是,这行代码会抛出错误 'Out of bounds nanosecond timestamp : 9999-12-30 23:00:00'

在这种情况下,我该如何处理解析时间戳?

背景:

我已经检查了pandas documentation 的解决方案。但是,似乎没有相关的支持来配置时间戳解析器。

代码片段:

import pyodbc as pyo
import pandas as pd
db_conn = pyo.connect('DSN=MySQL Connection 3;',autocommit=True)
tbl_df = pd.read_sql('select * from orders',db_conn)

我希望要么解析包含无效时间戳的单元格,要么默认避免解析时间戳。

【问题讨论】:

标签: python pandas python-2.7


【解决方案1】:

在 0.20.3 pandas.read_sql()parse_dates : list or dict, default: None

因此,pandas 正在解析在您的数据库中找到的日期,这将是出乎意料的。

select * from orders 删除为select col1, ..., datecol from orders 并删除datecol。会发生什么?

datecol 更改为UNIX_TIMESTAMP(STR_TO_DATE(datecol, '%M %d %Y %h:%i')),以便您可以将数据作为非日期时间格式提取。从那里,您可以在 Pandas 中进行转换。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2022-11-12
    • 2016-04-12
    • 2017-09-23
    相关资源
    最近更新 更多