pandas 我怎样才能捕捉到熊猫数据错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29500778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:09:47  来源:igfitidea点击:

How can I catch a pandas DataError?

pythonpandaserror-handling

提问by A User

I have since fixed the bug that caused the DataError, but I can not for the life of me figure out how to catch it explicitly:

我已经修复了导致 DataError 的错误,但我终生无法弄清楚如何明确捕获它:

try:
    df["my column"] = df.baddata + df.morebaddata
except DataError:
   print "Caught Error!"

Gives: NameError: name 'DataError' is not defined

给出: NameError: name 'DataError' is not defined

Then I tried pd.core.frame.DataErrorand received an AttributeError. I also tried Googling this but could not find a list of pandas error types. What is the correct path for DataError?

然后我尝试pd.core.frame.DataError并收到了AttributeError. 我也试过谷歌搜索这个,但找不到Pandas错误类型的列表。什么是正确的路径DataError

采纳答案by Christian O'Reilly

For Pandas<=0.22 (previous answer was given for Django), the solution is as proposed by @henrique-marciel but with the Pandas import. So

对于 Pandas<=0.22(先前的答案是针对 Django 给出的),解决方案是由 @henrique-marciel 提出的,但使用 Pandas 导入。所以

from pandas.core.groupby import DataError

and add the exception

并添加例外

except DataError:

For Pandas>=0.23, as noted by ytu, the API changed and the following import should be used instead:

对于 Pandas>=0.23,如 ytu 所述,API 已更改,应改为使用以下导入:

from pandas.core.groupby.groupby import DataError

回答by Henrique Maciel

I had the same problem, you can solve as follows:

我有同样的问题,你可以解决如下:

from django.db import DataError

Add the exception

添加例外

except DataError:

I managed to solve this way, below is the link of the documentation.

我设法以这种方式解决,下面是文档的链接。

Documentation

文档