pandas-错误合集
1. 错误
C:\Users\Gao\AppData\Local\Temp\ipykernel_30488\4043406211.py:9: 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.
mysql_page = pd.read_sql(sql="select * from dept", con=conn)
参考:
2. KeyError: "['A'] not found in axis"
描述:在使用df.drop('A')时,报错:KeyError: "['A'] not found in axis"
3.ValueError: invalid literal for int() with base 10: '13.7'
描述:df.loc[:, "气温(度)"] = df["气温(度)"].str.replace("℃", "").astype("int32")
问题:数据类型为浮点型,直接转为int类型,报错ValueError!
4.AttributeError: Can only use .str accessor with string values!
描述:df.loc[:, "气温(度)"] = df["气温(度)"].str.replace("℃", "").astype("float64")
问题:属性错误:只能使用带有字符串值的 .str 访问器!
修改为:df.loc[:, "气温(度)"] = df["气温(度)"].astype("str").str.replace("℃", "").astype("float64")
参考:
5.TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]
描述:df.loc[df["气温(度)"] > 26 & df["气温(度)"] < 29 & df["相对湿度(%)"] > 20 & df["相对湿度(%)"] < 55, :]
问题:TypeError:不能对类型为[float64]的数组和类型为[bool]的标量执行'rand_'操作
修改:缺括号(),df.loc[(df["气温(度)"] > 26) & (df["气温(度)"] < 29) & (df["相对湿度(%)"] > 20) & (df["相对湿度(%)"] < 55), :]
6.ValueError: This Series is a view of some other array, to sort in-place you must create a copy
描述:df["相对湿度(%)"].sort_values(inplace=True)
问题:不能直接对view进行修改,要么copy一份进行修改(对原df无影响);要么在原df中直接修改(原df改变)
修改:使用df.sort_values(by="相对湿度(%)", inplace=True)
7.FutureWarning: The default value of regex will change from True to False in a future version.
描述:使用正则表达式:df["中文日期"].str.replace("[年月日]","")
问题:在未来版本中,正则表达式的默认值将从True更改为False
修改:df["中文日期"].str.replace("[年月日]","",regex=True)
8.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 3114: invalid continuation byte
问题:使用pd.read_csv读取csv文件时,编码格式错误
修改:pd.read_csv参数中添加 encoding = "iso_8859_1"
9.missing from current font
问题:在使用plot绘图时,warning:中文乱码
修改:
import matplotlib导入matplotlib包matplotlib.rcParams['font.family'] = 'SimHei'
参考:
浙公网安备 33010602011771号