我怎么跑pd.read_sql语句没有日期解析?

2024-04-19 16:38:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我要跑了pd.read_sql语句没有日期解析。在

pd.read_sql文档中的parse_dates参数下,它声明它可以是Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword arguments of pandas.to_datetime() Especially useful with databases without native Datetime support, such as SQLite.

to_datetime文档中,默认情况下,errors='raise'。如果我可以将其更改为errors='ignore'或{},则此问题应该得到修复。在

我试着这样实施,见下文:

pd.read_sql(query, con, parse_dates={'col_name': {'errors': 'ignore'}}, chunksize=10**5)

它运行时没有错误,但仍然解析日期。在

代码与此问题不太相关。基本上就是:

df = pandas.read_sql(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, columns=None, chunksize=10**5)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html

需要关闭日期分析以防止此错误:


  File "expense.py", line 20, in <module>

    for df in gen:

  File "C:\Users\rfrigo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 1453, in _query_iterator

    data = cursor.fetchmany(chunksize)

ValueError: year -6371 is out of range

Tags: oftonamein文档nonepandasread
1条回答
网友
1楼 · 发布于 2024-04-19 16:38:45

您的问题是,当您指定chunksize时,请看以下示例:

if __name__ == '__main__':
    empty_query = 'select * from some_table where id = 8456314523;'
    df =pd.DataFrame()
    df = pd.read_sql(empty_query,connection,chunksize=10**5)
    print "df : {}".format(df if not df.empty else "df is empty")
    print 'END'

当我不指定chunksize=10**5时,df只是空的,但是当我指定chunksize时,它导致

^{pr2}$

可能先尝试运行较小的查询,例如使用limit 1,然后使用chunksize成功运行查询

相关问题 更多 >