python - 将熊猫更新为 postgres
问题描述
我正在尝试将 pandas 数据框上传到 PostgreSQL 数据库,但遇到错误。
import pandas as pd
import psycopg2
import pandas.io.sql as sql
conn_string = "host='localhost' dbname='**' user='postgres' password='**' port=5432"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
hh = pd.read_csv("C:/opus/data/mrcog/inputs/synthpop/synth_hhlds.csv")
hh.to_sql('buildings_updated', conn)
但是当我尝试上传表格时,我收到一个错误,我不明白它在说什么。
DatabaseError Traceback (most recent call last)
<ipython-input-12-b1b2758437b2> in <module>()
16
17 hh = pd.read_csv("C:/opus/data/mrcog/inputs/synthpop/synth_hhlds.csv")
---> 18 hh.to_sql('buildings_updated', conn)
C:\Anaconda2\lib\site-packages\pandas\core\generic.pyc in to_sql(self, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)
1343 sql.to_sql(self, name, con, flavor=flavor, schema=schema,
1344 if_exists=if_exists, index=index, index_label=index_label,
-> 1345 chunksize=chunksize, dtype=dtype)
1346
1347 def to_pickle(self, path, compression='infer'):
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in to_sql(frame, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)
469 pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index,
470 index_label=index_label, schema=schema,
--> 471 chunksize=chunksize, dtype=dtype)
472
473
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in to_sql(self, frame, name, if_exists, index, index_label, schema, chunksize, dtype)
1503 if_exists=if_exists, index_label=index_label,
1504 dtype=dtype)
-> 1505 table.create()
1506 table.insert(chunksize)
1507
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in create(self)
584
585 def create(self):
--> 586 if self.exists():
587 if self.if_exists == 'fail':
588 raise ValueError("Table '%s' already exists." % self.name)
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in exists(self)
572
573 def exists(self):
--> 574 return self.pd_sql.has_table(self.name, self.schema)
575
576 def sql_schema(self):
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in has_table(self, name, schema)
1515 "WHERE type='table' AND name=%s;") % wld
1516
-> 1517 return len(self.execute(query, [name, ]).fetchall()) > 0
1518
1519 def get_table(self, table_name, schema=None):
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in execute(self, *args, **kwargs)
1414 ex = DatabaseError(
1415 "Execution failed on sql '%s': %s" % (args[0], exc))
-> 1416 raise_with_traceback(ex)
1417
1418 @staticmethod
C:\Anaconda2\lib\site-packages\pandas\io\sql.pyc in execute(self, *args, **kwargs)
1402 cur.execute(*args, **kwargs)
1403 else:
-> 1404 cur.execute(*args)
1405 return cur
1406 except Exception as exc:
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': relation "sqlite_master" does not exist
LINE 1: SELECT name FROM sqlite_master WHERE type='table' AND name=?...
如何解决此错误?上传数据框应该很简单。我正在使用熊猫版本 4.3.34。
解决方案
文档说:
风味:'sqlite',默认无
0.19.0 版后已弃用:如果不使用 SQLAlchemy,则“sqlite”是唯一受支持的选项。
http://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.to_sql.html
因此,您似乎应该为此使用 SQLAlchemy,除非您的错误是关于已经存在的表,但似乎更有可能是因为您没有使用 SQLLite 或 SQLAlchemy 代理到像 Postgres 这样的真实数据库。
文档的另一部分:
if_exists : {'fail', 'replace', 'append'}, 默认'fail'</p>
失败:如果表存在,什么也不做。replace:如果表存在,删除它,重新创建它,然后插入数据。append:如果表存在,则插入数据。如果不存在则创建。