我正在安装一台新计算机,并且在另一台计算机上安装了anaconda和其他各种软件包之后,我正在尝试运行一些可以在另一台计算机上正常运行的代码。
但是,当尝试使用SQLalchemy导入redshift时,出现了一个新错误,我无法通过google找到任何内容:
“ SQLTable”对象没有属性“ insert_statement”
这似乎是padas.io.sql的问题,但我不知道是什么
下面是代码块:
import io
from pandas.io.sql import SQLTable
def _execute_insert(self,conn,keys,data_iter):
print("Using monkey-patched _execute_insert")
data = [dict((k,v) for k,v in zip(keys,row)) for row in data_iter]
conn.execute(self.insert_statement().values(data))
SQLTable._execute_insert = _execute_insert
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy import text
dbschema='xref'
engine = create_engine('not_showing_you_this_part',connect_args={'options': '-csearch_path={}'.format(dbschema)})
# test
from sqlalchemy import event,create_engine
@event.listens_for(engine,'before_cursor_execute')
def receive_before_cursor_execute(conn,cursor,statement,params,context,executemany):
if executemany:
cursor.fast_executemany = True
cursor.commit()
# end test
api_start_time = time.time()
print('starting SQL query')
# change yh to the dataframe you want to upload
# under name = : enter in the name of the table you want to create or append to
df.to_sql(name='computer_test',con = engine,if_exists = 'append',index=False)
print('sql insert took: ' + str((time.time() - api_start_time)) + ' seconds')
供参考,猴子补丁部分来自: How to speed up insertion from pandas.DataFrame .to_sql
图片完全错误