Osc*_*sca 5 python postgresql pandas
我想隐藏这个警告UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy并且我已经尝试过
import warnings
warnings.simplefilter(action='ignore', category=UserWarning)
import pandas
Run Code Online (Sandbox Code Playgroud)
但警告仍然显示。
我的 python 脚本从数据库读取数据。我用于pandas.read_sqlSQL 查询和psycopg2数据库连接。
我还想知道哪一行触发了警告。
我尝试了这个,但它不起作用。
import warnings
warnings.filterwarnings('ignore')
Run Code Online (Sandbox Code Playgroud)
因此,我使用 SQLAlchemy(因为警告消息希望我这样做)来包装 psycopg2 连接。
我按照此处的说明进行操作:SQLAlchemy for psycopg2 文档
一个简单的例子:
import psycopg2
import sqlalchemy
import pandas as pd
conn = sqlalchemy.create_engine(f"postgresql+psycopg2://{user}:{pw}@{host}:{port}/{db}")
query = "select count(*) from my_table"
pd.read_sql(query, conn)
Run Code Online (Sandbox Code Playgroud)
警告不再被触发。
| 归档时间: |
|
| 查看次数: |
12354 次 |
| 最近记录: |