Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?connectable處理方式
一、報錯信息
使用老的書寫方式從數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)到pandas, 會打出一條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對象 。創(chuàng)建create_engine對象(格式類似于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時,可能會遇到一些UserWarning。以下是一些可能會出現(xiàn)的UserWarning:
- Data Validation extension is not supported and will be removed: 這個警告來自openpyxl,當(dāng)使用pandas讀取Excel文件時,會出現(xiàn)這個警告。這個警告是關(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時,會出現(xiàn)這個警告。這個警告是關(guān)于使用SQLAlchemy連接數(shù)據(jù)庫的建議 2。
- Pandas doesn’t allow columns to be created via a new attribute name: 當(dāng)繼承Pandas DataFrame時,如果通過新屬性名創(chuàng)建列,則會出現(xiàn)這個警告 3。
以下是每種情況的代碼示例:Data Validation extension is not supported and will be removed: 這個警告通常與Excel文件的讀取有關(guān),你可以使用openpyxl庫的warnings模塊來忽略這個警告,示例如下:
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): 這個警告建議使用SQLAlchemy連接數(shù)據(jù)庫。示例代碼如下:
from sqlalchemy import create_engine # 創(chuàng)建一個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: 這個警告通常在繼承Pandas DataFrame時出現(xiàn),你應(yīng)該避免通過新屬性名創(chuàng)建列,而是使用標(biāo)準(zhǔn)的Pandas方式來創(chuàng)建列。示例代碼如下:
import pandas as pd # 創(chuàng)建一個空的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] # 這會觸發(fā)警告
這些示例代碼可以幫助你處理這些Pandas UserWarning,并采取適當(dāng)?shù)拇胧┮员苊鉂撛诘膯栴}。
總結(jié)
到此這篇關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable處理方式的文章就介紹到這了,更多相關(guān)Pandas告警UserWarning內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Numpy實現(xiàn)修改數(shù)組形狀
NumPy(Numerical Python)是Python中用于處理數(shù)組和矩陣的重要庫,它提供了豐富的功能,用于科學(xué)計算,本文主要介紹了如何使用NumPy提供的方法來改變數(shù)組的形狀,感興趣的可以了解下2023-11-11Python獲取當(dāng)前腳本文件夾(Script)的絕對路徑方法代碼
在本篇文章中小編給各位整理了關(guān)于Python獲取當(dāng)前腳本文件夾(Script)的絕對路徑實例代碼內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-08-08使用python matploblib庫繪制準(zhǔn)確率,損失率折線圖
這篇文章主要介紹了使用python matploblib庫繪制準(zhǔn)確率,損失率折線圖,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06