无法上传pandas df到mysql由于inf值

yeotifhr  于 2023-03-28  发布在  Mysql
关注(0)|答案(1)|浏览(185)

我有一个Excel文件,其中有无穷大值(inf),我总是得到编程错误上传到MySQL数据库

ProgrammingError                          Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)
   1750                 if not evt_handled:
-> 1751                     self.dialect.do_executemany(
   1752                         cursor, statement, parameters, context

~/.local/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/mysqldb.py in do_executemany(self, cursor, statement, parameters, context)
    196     def do_executemany(self, cursor, statement, parameters, context=None):
--> 197         rowcount = cursor.executemany(statement, parameters)
    198         if context is not None:

~/.local/lib/python3.8/site-packages/pymysql/cursors.py in executemany(self, query, args)
    172             assert q_values[0] == "(" and q_values[-1] == ")"
--> 173             return self._do_execute_many(
    174                 q_prefix,

~/.local/lib/python3.8/site-packages/pymysql/cursors.py in _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding)
    201         for arg in args:
--> 202             v = values % escape(arg, conn)
    203             if isinstance(v, str):

~/.local/lib/python3.8/site-packages/pymysql/cursors.py in _escape_args(self, args, conn)
    108         elif isinstance(args, dict):
--> 109             return {key: conn.literal(val) for (key, val) in args.items()}
    110         else:

~/.local/lib/python3.8/site-packages/pymysql/cursors.py in <dictcomp>(.0)
    108         elif isinstance(args, dict):
--> 109             return {key: conn.literal(val) for (key, val) in args.items()}
    110         else:

~/.local/lib/python3.8/site-packages/pymysql/connections.py in literal(self, obj)
    516         """
--> 517         return self.escape(obj, self.encoders)
    518 

~/.local/lib/python3.8/site-packages/pymysql/connections.py in escape(self, obj, mapping)
    509             return ret
--> 510         return converters.escape_item(obj, self.charset, mapping=mapping)
    511 

~/.local/lib/python3.8/site-packages/pymysql/converters.py in escape_item(val, charset, mapping)
     24     else:
---> 25         val = encoder(val, mapping)
     26     return val

~/.local/lib/python3.8/site-packages/pymysql/converters.py in escape_float(value, mapping)
     59     if s in ("inf", "nan"):
---> 60         raise ProgrammingError("%s can not be used with MySQL" % s)
     61     if "e" not in s:

ProgrammingError: inf can not be used with MySQL

"我的努力"
我试着先将inf转换为0,但它改变了我不想要的数据。然后我将inf转换为3个小数点

df = df.replace(np.nan, 0)
df = np.round(df, decimals = 3)

我还替换了可能有inf值但没有工作的列的类型。

df["col1"]=df["col1"].astype("float")
df["col2"]=df["col2"].astype("float")

我也检查了df后的错误,但没有inf值,但也然后Pandas给予错误。
Checking of inf values (Code)

gcmastyq

gcmastyq1#

您可以尝试:

df  = df.replace([np.inf, -np.inf], np.nan)
df = df.fillna(0)

Link

相关问题