pandas Python“接口错误:错误绑定参数 2 - 可能是不受支持的类型。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33511687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:09:53  来源:igfitidea点击:

Python "InterfaceError: Error binding parameter 2 - probably unsupported type."

pythonsqlsqlitepandas

提问by Andrew

When I run the following code, I keep getting the "InterfaceError: Error binding parameter 2 - probably unsupported type" error, and I need help identifying where the problem is. Everything works fine up until I try to send the data to sql through.

当我运行以下代码时,我不断收到“InterfaceError:错误绑定参数 2 - 可能不受支持的类型”错误,我需要帮助确定问题所在。一切正常,直到我尝试将数据发送到 sql。

anagramsdf.to_sql('anagrams',con=conn,if_exists='replace',index=False)
cdf=pd.read_sql("select (distinct ID) from anagrams;",conn)


import pandas as pd
import sqlite3

conn = sqlite3.connect("anagrams")
xsorted=sorted(anagrams,key=sorted)
xunique=[x[0] for x in anagrams]

xunique=pd.Series(xunique)
xanagrams=pd.Series(anagrams)
anagramsdf=pd.concat([xunique,dfcount,xanagrams],axis=1)
anagramsdf.columns=['ID','anagram_count','anagram_list']

c=conn.cursor()
c.execute("create table anagrams(ID, anagram_count, anagram_list)")
conn.commit()

anagramsdf.to_sql('anagrams',con=conn,if_exists='replace',index=False)
cdf=pd.read_sql("select (distinct ID) from anagrams;",conn)

cdf=pd.read_sql("select max(anagram_count) from anagrams;",conn)
cdf
def print_full(x):
    pd.set_option('display.max_rows', len(x))
    print(x)
    pd.reset_option('display.max_rows')

cdf=pd.read_sql("select * from anagrams where anagram_count=12;",conn)
pd.set_option('max_colwidth',200)

Full traceback error:

完整回溯错误:

Traceback (most recent call last):
  File "sqlpandas.py", line 88, in <module>
    anagramsdf.to_sql('anagrams',con=conn,if_exists='replace',index=False)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 982, in to_sql
    dtype=dtype)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 549, in to_sql
    chunksize=chunksize, dtype=dtype)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 1567, in to_sql
    table.insert(chunksize)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 728, in insert
    self._execute_insert(conn, keys, chunk_iter)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 1357, in _execute_insert
    conn.executemany(self.insert_statement(), data_list)
sqlite3.InterfaceError: Error binding parameter 2 - probably unsupported type.

Snippet from Dataframe:

来自数据框的片段:

             ID  anagram_count           anagram_list
0            aa              1                  (aa,)
1      anabaena              1            (anabaena,)
2      baaskaap              1            (baaskaap,)
3      caracara              1            (caracara,)
4      caragana              1            (caragana,)

采纳答案by Andrew

I used the following code to change the datatypes to strings, and this solved the problem:

我使用以下代码将数据类型更改为字符串,这解决了问题:

anagramsdf.dtypes
anagramsdf['ID']= anagramsdf['ID'].astype('str')
anagramsdf['anagram_list']= anagramsdf['anagram_list'].astype('str')
anagramsdf.to_sql('anagramsdf',con=conn,if_exists='append',index=False)

回答by sparrow

Using Pandas 0.23.4 I have a column with datetime values (format '%Y-%m-%d %H:%M:%S') that is of data type "string" that was throwing the same error when I tried to pass it to "to_sql" method. After converting to "datetime" dtype it worked. Hope that's helpful to someone with the same issue :).

使用 Pandas 0.23.4 我有一列日期时间值(格式 '%Y-%m-%d %H:%M:%S')是数据类型“字符串”,当我尝试时抛出相同的错误将其传递给“to_sql”方法。转换为“日期时间”数据类型后,它起作用了。希望这对有同样问题的人有所帮助:)。

To convert:

转换:

df['date'] = pd.to_datetime(df['date'],format=datetimeFormat,errors='coerce')

回答by Shervin

Sparrow's solution worked for me. If the index is not converted to a datetime then SQL is going to throw "error binding parameter"

麻雀的解决方案对我有用。如果索引未转换为日期时间,则 SQL 将抛出“错误绑定参数”

I used a column that had the datetimes to first convert to the correct format and then use it as the index:

我使用了一个具有日期时间的列,首先将其转换为正确的格式,然后将其用作索引:

df.set_index(pd.to_datetime(df['datetime']), inplace=True)