python的錯誤提示非常人性化,通常報錯時就會提供解決辦法,比如一些syntax error就很容易解決,整理了一下遇到的稍微麻煩一些的:
按住Ctrl+F在本頁搜索
1. Matplotlib Deprecation Warning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.
原因:輸入import mpl_finance as finance,程序說沒有這個模塊。因為新版本mpl 2.2中,finance才會被替換成mpl_finance,目前import matplotlib.finance是不會影響使用的。為了兼容新版本,可以下載mpl_finance這個模塊,這樣才可以import
解決:
windows: 命令行輸入:pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
linux: sudo pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
__________________________________________________________________________________________________________________________________________________
2. Python IndexError: list assignment index out of range
原因:通常是因為直接對空的list進行賦值,比如A=[];A[0]=1
解決:A.append(1)
__________________________________________________________________________________________________________________________________________________
3. IndexError: list index out of range
原因:用pandas.read_excel讀取excel文件時報錯,可能是有未顯示的多余列
解決:將excel的內容拷貝到一個新的文件內
__________________________________________________________________________________________________________________________________________________
4. If using all scalar values, you must pass an index
原因:在將dict轉為dataframe格式時,未指定index
解決:df=pd.dataframe(dict,index=[0]),這里index的值由dict內的格式決定
__________________________________________________________________________________________________________________________________________________
5. JSONDecodeError: Expecting value: line 1 column 1 (char 0)
原因:用okcoin的api獲取數據時出現,原因不明
解決:在url前面添加https://解決
__________________________________________________________________________________________________________________________________________________
6. TypeError: can't multiply sequence by non-int of type 'float'
原因:用dataframe進行數據類型轉換(object轉np.int)時遇到,python的int無限制,但C的int和long都會溢出,因為python的很多函數底層用C實現的,所以python有時也會出現這種錯誤
解決:將np.int改成np.float,再轉換過來
__________________________________________________________________________________________________________________________________________________
7.Keyerror:(某個數字)
原因:在dataframe過濾或刪除一些行以后,index不連續,這樣在遍歷的時候會出現keyerror
解決:可以換成其他index,也可以創建新的連續的索引
df['index']=range(len(df['direction']))
df=df.set_index(df['index'])
__________________________________________________________________________________________________________________________________________________
8. A value is trying to be set on a copy of a slice from a DataFrame
原因:在對dataframe的某一行某一列進行改動時,其他程序正在對dataframe占用,通常是因為代碼的邏輯位置不對
解決:調整代碼邏輯,或者另外拷貝一份dataframe進行改動
不要直接對dataframe的某一個位置進行賦值
參考:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
或者新建一個list,將需要增加的數據添加,然后將dataframe列的數據改成list的數據a.append(x);pd.Dataframe(a)
__________________________________________________________________________________________________________________________________________________
9. PermissionError: [WinError 5] 拒絕訪問。: 'C:/Users/xxx/Desktop/'
原因:用os.remove()刪除文件夾下的文件時,文件或文件夾在被占用
解決:改用send2trash模塊(把文件移到回收站)或者shutil模塊(這個會連文件夾一起直接刪除)
import send2trash
send2trash.send2trash(file_dir)
import shutil
shutil.rmtree(file_dir)
__________________________________________________________________________________________________________________________________________________
10. second must be in 0..59 & ValueError: unconverted data remains: .0
原因:在將數字轉化成datetime格式時,second有大於59的數,判斷大於59時,在minutes+1,報第二個error
解決:對於大於59的時間數字直接賦值為59,然后可選擇去重
__________________________________________________________________________________________________________________________________________________
11. No module named 'psycopg2._psycopg
原因:即使已經安裝psycopg,正常使用過,也有可能會報這個錯
解決:重新安裝
pip uninstall psycopy2
pip install psycopg2
__________________________________________________________________________________________________________________________________________________
12. TypeError: unsupported operand type(s) for /: 'method' and 'int'
原因:method不能用在運算符的地方,通常是因為method忘記括號
解決:加上括號,比如sum()等容易忘記的地方
__________________________________________________________________________________________________________________________________________________
13. TypeError: only list-like objects are allowed to be passed to isin(), you passed a [int]
原因:對dataframe進行篩選時篩選值是int,不能用df.isin(list)
解決:
df=df[column name].isin(list name) #篩選對象是list
df=df[df[column name]==i] #篩選對象是int
__________________________________________________________________________________________________________________________________________________
14. TypeError: unhashable type: 'slice
原因:對dataframe切片錯誤
解決:將df[:,i]改成df.iloc[:,i]
__________________________________________________________________________________________________________________________________________________
15. JSONDecodeError: Expecting value: line 1 column 1 (char 0)
原因:request網頁信息,轉換成json格式時報錯
解決:如果檢測json轉換的信息沒有錯的話,可能是頁面拒絕request類型的訪問
__________________________________________________________________________________________________________________________________________________
16. ValueError: dictionary update sequence element #0 has length 7; 2 is required
原因:將list轉換成dict時直接用了dict(list)
解決:改用eval(list)
__________________________________________________________________________________________________________________________________________________
17. ValueError: No engine for filetype: ''
原因:pandas輸出文件時未指定文件后綴名
解決:filename后加.xls或者.csv
__________________________________________________________________________________________________________________________________________________
18. redis exception connectionError :error -2 connecting to redis:6379. name or service not known.
redis.exceptions.ConnectionError: Error 11001 connecting to host:6379. getaddrinfo failed.
原因:檢查redis連接的host名稱,通常都是因為拼寫之類的錯誤
解決:
__________________________________________________________________________________________________________________________________________________
19. ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
原因:對dataframe的值進行判斷並提取時使用了錯誤的格式:df [df.a>1 and df.b<0]
解決:改成:df [df.a>1 & df.b<0]
__________________________________________________________________________________________________________________________________________________
20. IndexError: single positional indexer is out-of-bounds
原因:索引出界
解決:檢查dataframe實際列數行數
__________________________________________________________________________________________________________________________________________________
21. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 3: invalid continuation byte
原因:用pandas讀取csv文件時報錯,csv文件里有中文,utf-8不能解析
解決:
df = pd.read_csv('path' + 'filename.csv', encoding='GB2312')