隐藏熊猫警告:SQLAlchemy



我想隐藏此警告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

但警告仍然存在。

我的python脚本从数据库中读取数据。我将pandas.read_sql用于SQL查询,将psycopg2用于数据库连接。

我也想知道哪一行触发了警告。

使用以下方法对我来说效果很好:

import warnings
warnings.filterwarnings('ignore').

我正在使用熊猫和脓毒症,之前也收到过同样的警告。

我试过了,但它不起作用。

import warnings
warnings.filterwarnings('ignore')

因此,我使用了SQLAlchemy(因为警告消息希望我这样做(来包装psycopg2连接。

我遵循了这里的说明:用于psycopg2文档的SQLAlchemy

一个简单的例子:

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)

警告不再被触发。

您现在正在筛选的警告是FutureWarning类型的警告。您收到的警告类型为UserWarning,因此应将警告类别更改为UserWarning。我希望这能回答你关于熊猫为什么发出警告的问题。

我在使用psycopg2连接到PostgreSQL数据库时也遇到了同样的错误。我成功地用以下行隐藏了这个警告:

import warnings
warnings.simplefilter(action='ignore', category=UserWarning)

我认为这里的关键是UserWarning需要是一个类,而不是像您在示例中那样的字符串。

希望这对你有用。

最新更新