Writing to PostgreSQL from pandas: AttributeError: 'Engine' object has no attribute 'cursor'

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I am trying to write a table to a PostgreSQL database from a Pandas data frame (following this answer) but I am getting the error AttributeError: 'Engine' object has no attribute 'cursor'

My code is:

import pandas as pd from sqlalchemy import create_engine import numpy as np  df = pd.DataFrame(index=np.arange(1, 11),                    data=np.random.random(size=(10, 10)),                   columns=['c{}'.format(i) for i in np.arange(1, 11)])  engine = create_engine('postgresql://user@localhost:5432/db') df.to_sql('scores', engine) 

engine can connect to the db and return table names.

I am using Python 2.7.8, Pandas 0.15.2, Sqlalchmey 1.0.10 and Postgre 9.4.5.0

---- EDIT ----

I should add that I was getting the error when running this code from within a Jupyter notebook (iPython 3.0.0). When I run from a Python command line it works fine.

---- Full traceback ----

--------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) <ipython-input-276-da2dfbe48036> in <module>()       8        9 engine = create_engine('postgresql://phil@localhost:5432/rh') ---> 10 df.to_sql('scores', con=engine)  /Library/Python/2.7/site-packages/pandas/core/generic.pyc in to_sql(self, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)     964             self, name, con, flavor=flavor, schema=schema, if_exists=if_exists,     965             index=index, index_label=index_label, chunksize=chunksize, --> 966             dtype=dtype)     967      968     def to_pickle(self, path):  /Library/Python/2.7/site-packages/pandas/io/sql.pyc in to_sql(frame, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)     536     pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index,     537                       index_label=index_label, schema=schema, --> 538                       chunksize=chunksize, dtype=dtype)     539      540   /Library/Python/2.7/site-packages/pandas/io/sql.pyc in to_sql(self, frame, name, if_exists, index, index_label, schema, chunksize, dtype)    1502                             if_exists=if_exists, index_label=index_label,    1503                             dtype=dtype) -> 1504         table.create()    1505         table.insert(chunksize)    1506   /Library/Python/2.7/site-packages/pandas/io/sql.pyc in create(self)     635      636     def create(self): --> 637         if self.exists():     638             if self.if_exists == 'fail':     639                 raise ValueError("Table '%s' already exists." % self.name)  /Library/Python/2.7/site-packages/pandas/io/sql.pyc in exists(self)     623      624     def exists(self): --> 625         return self.pd_sql.has_table(self.name, self.schema)     626      627     def sql_schema(self):  /Library/Python/2.7/site-packages/pandas/io/sql.pyc in has_table(self, name, schema)    1512         query = flavor_map.get(self.flavor)    1513  -> 1514         return len(self.execute(query).fetchall()) > 0    1515     1516     def get_table(self, table_name, schema=None):  /Library/Python/2.7/site-packages/pandas/io/sql.pyc in execute(self, *args, **kwargs)    1403             cur = self.con    1404         else: -> 1405             cur = self.con.cursor()    1406         try:    1407             if kwargs:  AttributeError: 'Engine' object has no attribute 'cursor' 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!