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:
try:
df["my column"] = df.baddata + df.morebaddata
except DataError:
print "Caught Error!"
Gives: NameError: name 'DataError' is not defined
Then I tried pd.core.frame.DataError
and 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
?
The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program's response to any exceptions in the preceding try clause.
This is the simplest and the easiest way to create an empty pandas DataFrame object using pd. DataFrame() function. In this method, we simply call the pandas DataFrame class constructor without any parameters which in turn returns an empty pandas DataFrame object.
Use DataFrame.empty attribute empty attribute to check if the given dataframe is empty or not.
In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.
For Pandas<=0.22 (previous answer was given for Django), the solution is as proposed by @henrique-marciel but with the Pandas import. So
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:
from pandas.core.groupby.groupby import DataError
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With