import io
import pandas as pd
# Get the SQL that would be generated by the create table statement
create_table_sql = pd.io.sql.get_schema(df, tmp_table_name)
# Replace the `CREATE TABLE` part of the generated statement with
# whatever you need.
create_tmp_table_sql = re.sub(
"^(CREATE TABLE)?",
"CREATE TEMP TABLE",
create_table_sql
)
然后,您可以像这样使用它:
# Write to the database in a transaction (psycopg2)
with conn.cursor() as cur:
cur.execute(create_tmp_table_sql)
output = io.StringIO()
df.to_csv(output, sep="\t", header=False, index=False, na_rep="NULL")
output.seek(0)
cur.copy_from(output, tmp_table_name, null="NULL")
3条答案
按热度按时间bprjcwpo1#
DataFrame.to_sql()
使用内置的pandapandas.io.sql
包,它本身依赖于SQLAlchemy作为数据库抽象层。为了在SQLAlchemy ORM中创建一个“临时”表,you need to supply a prefix:据我所知,
pandas.io.sql
不允许指定prefixes
,也不允许轻松地更改表的创建方式。解决此问题的一种方法是 * 预先创建临时表 *,并将
to_sql()
与if_exists="append"
一起使用(所有这些都使用同一个数据库连接)。下面也是我尝试做的事情:重写
pandas.io.sql.SQLTable
的_create_table_setup()
方法,并将prefixes
传递给Table
构造函数。由于某些原因,该表仍然是非临时创建的。不确定是否有帮助,但以下是我使用的代码:gist。这是一种很老套的方法,但我希望它至少可以作为一个示例代码,让您开始使用这种方法。jutyujz02#
简单的解决方案,无需花哨的魔法
对我来说,这是一个快速而简单的变通办法。
只需将RegEx应用于生成的SQL,即可添加所需的任何语句。
然后,您可以像这样使用它:
归功于Aseem,它提供了一种快速写入Postgres的方法。
vu8f3i0k3#
这可能有点笨拙,而且从技术上讲,它并没有创建临时表,它只是像一个临时表一样工作,但是你可以使用
contextlib
中的@contextmanager
装饰器在打开上下文时创建表,并在关闭上下文时删除它。我用Teradata测试了它,它运行得很好。我没有MySQL可以测试它,但是只要
DROP
语句在MySQL中工作,它就应该能按预期工作。