postgresql pangres python:索引错误:必须命名所有索引级别

ecfsfe2w  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(152)

我想做一个upsert在我的sql数据库使用“pangres”,但它返回此错误

raise IndexError("All index levels must be named!")
IndexError: All index levels must be named!

我该怎么办?

df = pd.read_excel('personne.xlsx')
upsert(engine=engine,
       df=df,
       table_name='personne',
       if_row_exists='update')
cbeh67ev

cbeh67ev1#

命名df索引或将列设置为索引。参见the Pangres wiki
对于df['unique_id']用作主键的dfs:

# create SQL table with first df, set if_exists as needed
df.to_sql('table_name', engine, if_exists = 'fail', index = False)

with engine.connect() as con: # set df['unique_id'] as primary key in SQL
    con.execute("ALTER TABLE table_name ADD PRIMARY KEY (unique_id);")

# set df2['unique_id'] as index
df2.set_index(['unique_id'], inplace = True, drop = False)
pangres.upsert(engine, df2, 'table_name', if_row_exists ='update')

相关问题