How can I catch a pandas DataError?

Since then, I fixed the error that caused the DataError, but I can’t understand for the rest of my life how to catch it explicitly:

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

It gives: NameError: name 'DataError' is not defined

Then I tried pd.core.frame.DataErrorand received AttributeError. I also tried Google, but could not find a list of pandas error types. What is the right way for DataError?

+2
source share
2 answers

I had the same problem, you can solve the following:

from django.db import DataError

Add exception

except DataError:

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

Documentation

+2
source

For Pandas (the previous answer was for Django), a solution was proposed by @ henrique-marciel, but with Pandas imports. So

from pandas.core.groupby import DataError

and add an exception

except DataError:
+2

All Articles