调整pandas read_sql_query NULL值处理?

时间:2015-06-04 19:26:59

标签: python pandas

当我这样做时

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine('sqlite://')
conn = engine.connect()
conn.execute("create table test (a float)")
for _ in range(5):
    conn.execute("insert into test values (NULL)")

df = pd.read_sql_query("select * from test", engine)
#df = pd.read_sql_table("test", engine)
df.a

结果是一列None值,而不是float("nan")。这非常令人讨厌,特别是如果您以chunk方式读取具有NULL值的float列。

read_sql_table版本工作正常,因为我认为它可以使用类型信息。

我是否可以轻松调整read_sql_query以将NULL值解释为float("nan")

1 个答案:

答案 0 :(得分:0)

根据wesm在链接页面中的评论,似乎an issue被引发了类似的东西 - componentOne参数 - 被添加到版本0.7.2中的pandas:

  

hi arthur,我添加了一个选项coerce_float(在上面的提交中),它转换了Decimal - >使用NaN浮动并填充无。将Decimal转换为float仍然非常慢。将成为0.7.2的一部分即将发布

虽然pandas.read_sql_query 0.18.1 docs中的描述似乎令人困惑:

  

coerce_float:boolean,默认为True

     

尝试将值转换为非字符串,非数字对象(如decimal.Decimal)为浮点,对SQL结果集很有用

相关问题