0
点赞
收藏
分享

微信扫一扫

pandas  FutureWarning 警告⚠️

小沙坨 2022-07-18 阅读 153


pandas  FutureWarning 警告⚠️

FutureWarning: 
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative. 

/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py:1494: FutureWarning: 
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

See the documentation here:
https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
return self._getitem_tuple(key)

pandas文档中提到这个警告:

警告:从0.21.0开始,pandas将显示​​FutureWarning​​​带有缺少标签的列表的if索引。将来这会提高一个​​KeyError​​​。请参阅​​list-like使用列表中缺少键的loc是不推荐使用​​。

报出警告代码:

data = data.loc[:, ['title', 'issuer', 'ctime', 'gtime', 'url']]

解释代码警告原因:

这不是错误,这是一个警告。但它告诉你的是,你的一个标签,可能是你的列标签,没有包含在DataFrame中。当前行为将以静默方式失败并返回带有​​NaN​​s 的列。将来,它会引发错误。 

 经过自己Debug,发现了DataFrame数据中确实没有'ctime' 'gitme' 'url' 

修改代码: 此时代码运行不会再抛出警告信息

# 加个if判断
if sort == 'SY304000':
data = data.loc[:, ['title', 'issuer', 'ctime']]
else:
data = data.loc[:, ['title', 'issuer', 'ctime', 'gtime', 'url']]

 

举报

相关推荐

0 条评论