Pandas創(chuàng)建DataFrame提示:type?object?'object'?has?no?attribute?'dtype'解決方案
發(fā)現(xiàn)問題
pandas版本0.25.3
import pandas as pd symbol_info_columns = ['1', '持倉方向', '持倉量', '持倉收益率', '持倉收益', '持倉均價', '當前價格', '最大杠桿'] # v3 symbol_config = {'BTC': 'BTC-USDT-210924', 'LTC': 'LTC-USDT-210924', 'EOS': 'EOS-USDT-210924', 'ETH': 'ETH-USDT-210924', 'XRP': 'XRP-USDT-210924', 'FIL': 'FIL-USDT-210924'} symbol_info = pd.DataFrame() # dates = pd.date_range('20190101', periods=6) # num_df = pd.DataFrame(data=np.random.randn(6, 8), index=dates, columns=symbol_info_columns) symbol_info = pd.DataFrame(index=symbol_config.keys(), columns=symbol_info_columns)
data為空,且dtype默認為空時
出現(xiàn)type object ‘object’ has no attribute 'dtype’告警
原因分析:
創(chuàng)建DataFrame時,data字段為空
會默認創(chuàng)建一個空字典作為data
def __init__(self, data=None, index=None, columns=None, dtype=None, copy=False): if data is None: data = {}
然后初始化字典
elif isinstance(data, dict): mgr = init_dict(data, index, columns, dtype=dtype)
init_dict函數(shù)中:
columns非空,且dtype默認為None時,會賦值nan_dtype = object
if columns is not None: if missing.any() and not is_integer_dtype(dtype): if dtype is None or np.issubdtype(dtype, np.flexible): # GH#1783 nan_dtype = object
該object下無dtype方法
可能是object引用錯誤
解決方案:
pandas(版本0.25.3)init_dict函數(shù)位于
D:\Users\。。。\Anaconda3\envs\Python3.7\Lib\site-packages\pandas\core\internals\construction.py
參考Python3.9環(huán)境中pandas(版本1.2.5)
同名函數(shù)(D:\Users\。。。\Anaconda3\envs\Python3.7\Lib\site-packages\pandas\core\internals\construction.py)寫法
nan_dtype = np.dtype(object)
可見該問題應該是pandas(版本0.25.3)的bug
總結
到此這篇關于Pandas創(chuàng)建DataFrame提示:type object 'object' has no attribute 'dtype'解決方案的文章就介紹到這了,更多相關Pandas創(chuàng)建DataFrame報錯內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python根據(jù)用戶需求輸入想爬取的內容及頁數(shù)爬取圖片方法詳解
這篇文章主要介紹了python根據(jù)用戶需求輸入想爬取的內容及頁數(shù)爬取圖片方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Python?torch.onnx.export用法詳細介紹
這篇文章主要給大家介紹了關于Python?torch.onnx.export用法詳細介紹的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-07-07Python編程入門之Hello World的三種實現(xiàn)方式
這篇文章主要介紹了Python編程入門之Hello World的三種實現(xiàn)方式,實例分析了print輸出函數(shù)的使用及控制臺輸出的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11Python 隨機生成測試數(shù)據(jù)的模塊:faker基本使用方法詳解
這篇文章主要介紹了Python 隨機生成測試數(shù)據(jù)的模塊:faker基本使用方法,結合實例形式詳細分析了Python 隨機生成測試數(shù)據(jù)的模塊faker基本功能、原理、使用方法及操作注意事項,需要的朋友可以參考下2020-04-04Pycharm中安裝wordcloud等庫失敗問題及終端通過pip安裝的Python庫如何添加到Pycharm解釋器中(
這篇文章主要介紹了Pycharm中安裝wordcloud等庫失敗問題及終端通過pip安裝的Python庫如何添加到Pycharm解釋器中,本文給大家介紹的非常詳細,需要的朋友可以參考下2020-05-05