Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?connectable處理方式
一、報(bào)錯(cuò)信息
使用老的書寫方式從數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)到pandas, 會(huì)打出一條warning信息:
UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
二、老的書寫方式
老的書寫方式為:
import pymysql
import pandas as pd
db_host = 'localhost'
user = 'root'
passwd = '123456'
db = 'mytestdb'
conn = pymysql.connect(host=db_host,
user=user,
passwd=passwd,
db=db,
charset='utf8')
sql = 'SELECT * FROM students'
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_sql(sql, conn)
print(df)
conn.close()
三、新的書寫方式
按照提示,推薦使用SQLAlchemy,需要先安裝SQLAlchemy庫:
pip install sqlalchemy

新版本的pandas庫中con參數(shù)使用sqlalchemy庫創(chuàng)建的create_engine對(duì)象 。創(chuàng)建create_engine對(duì)象(格式類似于URL地址)
from sqlalchemy import create_engine
import pandas as pd
MYSQL_HOST = 'localhost'
MYSQL_PORT = '3306'
MYSQL_USER = 'root'
MYSQL_PASSWORD = '123456'
MYSQL_DB = 'mytestdb'
engine = create_engine('mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8'
% (MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_DB))
sql = 'SELECT * FROM students'
df = pd.read_sql(sql, engine)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
print(df)

附:pandas還有哪些userwarning
當(dāng)使用Pandas時(shí),可能會(huì)遇到一些UserWarning。以下是一些可能會(huì)出現(xiàn)的UserWarning:
- Data Validation extension is not supported and will be removed: 這個(gè)警告來自openpyxl,當(dāng)使用pandas讀取Excel文件時(shí),會(huì)出現(xiàn)這個(gè)警告。這個(gè)警告是關(guān)于一些規(guī)范的擴(kuò)展,不影響數(shù)據(jù)的讀取 1。
- pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.: 當(dāng)使用老的書寫方式從數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)到pandas時(shí),會(huì)出現(xiàn)這個(gè)警告。這個(gè)警告是關(guān)于使用SQLAlchemy連接數(shù)據(jù)庫的建議 2。
- Pandas doesn’t allow columns to be created via a new attribute name: 當(dāng)繼承Pandas DataFrame時(shí),如果通過新屬性名創(chuàng)建列,則會(huì)出現(xiàn)這個(gè)警告 3。
以下是每種情況的代碼示例:Data Validation extension is not supported and will be removed: 這個(gè)警告通常與Excel文件的讀取有關(guān),你可以使用openpyxl庫的warnings模塊來忽略這個(gè)警告,示例如下:
import openpyxl
import warnings
from openpyxl.utils.exceptions import DataValidationError
# 忽略DataValidationError的警告
warnings.simplefilter("ignore", category=DataValidationError)# 使用Pandas讀取Excel文件
df = pd.read_excel('your_excel_file.xlsx')pandas only supports SQLAlchemy connectable (engine/connection): 這個(gè)警告建議使用SQLAlchemy連接數(shù)據(jù)庫。示例代碼如下:
from sqlalchemy import create_engine
# 創(chuàng)建一個(gè)SQLAlchemy數(shù)據(jù)庫連接
engine = create_engine('database_connection_string')# 使用SQLAlchemy連接從數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)到Pandas DataFrame
df = pd.read_sql_query('SELECT * FROM your_table', con=engine)Pandas doesn't allow columns to be created via a new attribute name: 這個(gè)警告通常在繼承Pandas DataFrame時(shí)出現(xiàn),你應(yīng)該避免通過新屬性名創(chuàng)建列,而是使用標(biāo)準(zhǔn)的Pandas方式來創(chuàng)建列。示例代碼如下:
import pandas as pd # 創(chuàng)建一個(gè)空的DataFrame df = pd.DataFrame() # 使用標(biāo)準(zhǔn)方式創(chuàng)建列 df['column_name'] = [1, 2, 3, 4, 5] # 避免使用新屬性名創(chuàng)建列 # df.new_column = [6, 7, 8, 9, 10] # 這會(huì)觸發(fā)警告
這些示例代碼可以幫助你處理這些Pandas UserWarning,并采取適當(dāng)?shù)拇胧┮员苊鉂撛诘膯栴}。
總結(jié)
到此這篇關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable處理方式的文章就介紹到這了,更多相關(guān)Pandas告警UserWarning內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Numpy實(shí)現(xiàn)修改數(shù)組形狀
NumPy(Numerical Python)是Python中用于處理數(shù)組和矩陣的重要庫,它提供了豐富的功能,用于科學(xué)計(jì)算,本文主要介紹了如何使用NumPy提供的方法來改變數(shù)組的形狀,感興趣的可以了解下2023-11-11
Python獲取當(dāng)前腳本文件夾(Script)的絕對(duì)路徑方法代碼
在本篇文章中小編給各位整理了關(guān)于Python獲取當(dāng)前腳本文件夾(Script)的絕對(duì)路徑實(shí)例代碼內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-08-08
使用python matploblib庫繪制準(zhǔn)確率,損失率折線圖
這篇文章主要介紹了使用python matploblib庫繪制準(zhǔn)確率,損失率折線圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
實(shí)例講解python中的序列化知識(shí)點(diǎn)
本篇文章通過代碼實(shí)例給大家詳細(xì)分享了關(guān)于python中的序列化知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2018-10-10
Python中Requests POST請(qǐng)求解讀
POST請(qǐng)求是HTTP協(xié)議中一種用于提交數(shù)據(jù)的方法,與GET請(qǐng)求獲取數(shù)據(jù)不同,POST常用于提交表單數(shù)據(jù)和上傳文件,本文介紹了如何使用Python的Requests包發(fā)送POST請(qǐng)求,包括基本的表單數(shù)據(jù)提交、發(fā)送JSON格式數(shù)據(jù),以及如何處理響應(yīng)狀態(tài)碼和錯(cuò)誤2024-11-11

