pandas to_sql不处理nan

发布于 2025-02-07 20:40:34 字数 819 浏览 3 评论 0原文

我只是想发送到Sybase ASE ASE一些包含NAN的浮点数据,预期的结果是数据库中的零。 Pandas版本为1.3.5,Python Python 3.8.12和Sqlalchemy 1.4.28。

表的创建:

create table test(head VARCHAR(20) NOT NULL, val FLOAT NULL, primary key(head))

数据框的创建:

df = pd.DataFrame({"head":["A","B","C"], "val":[1.2,np.nan,42.0]}) #math.nan gets the same result

现在崩溃的代码:

df.to_sql("test",
      con = engine,
      if_exists="replace",
      index=False,
      dtype={'head': db.types.VARCHAR(20), 'val':  db.types.FLOAT},
      chunksize=1)

错误消息:

[SAP][ASE ODBC Driver][Adaptive Server Enterprise]Implicit conversion from datatype 'VARCHAR' to 'FLOAT' is not allowed.  Use the CONVERT function to run this query.

对发生的事情有任何想法吗?我到处看,似乎应该像魅力一样工作!

I'm just trying to send to sybase ASE some float data containing nan, the expected result being a NULL in the database.
The pandas version is 1.3.5, and python Python 3.8.12, and sqlalchemy 1.4.28 .

Creation of the table:

create table test(head VARCHAR(20) NOT NULL, val FLOAT NULL, primary key(head))

Creation of the dataframe:

df = pd.DataFrame({"head":["A","B","C"], "val":[1.2,np.nan,42.0]}) #math.nan gets the same result

And now the code that crashes :

df.to_sql("test",
      con = engine,
      if_exists="replace",
      index=False,
      dtype={'head': db.types.VARCHAR(20), 'val':  db.types.FLOAT},
      chunksize=1)

the error message:

[SAP][ASE ODBC Driver][Adaptive Server Enterprise]Implicit conversion from datatype 'VARCHAR' to 'FLOAT' is not allowed.  Use the CONVERT function to run this query.

Any idea about what's happening? Everywhere I looked, it seemed like this ought to work like a charm!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文