Reputation: 31
I'm trying to use try/except to query BigQuery tables, sometimes the query may not be correct in which case pandas raises a GenericGBQException error.
My problem is I get name 'GenericGBQException' is not defined when trying to handle this error, example code below:
try:
df = pd.read_gbq(query, projID)
query_fail = 0
except GenericGBQException:
query_fail = 1
if query_fail == 1:
do some stuff
I can get by with catching all exceptions though obviously it's not ideal.
Upvotes: 0
Views: 2146
Reputation: 183
For those stumbling upon this issue today, the pd.read_gbq()
pandas function is now deprecated, instead you can use the pandas_gbq.read_gbq()
function from the pandas_gbq
library.
On the exception mentioned in the question, you can reference it with the following: pandas_gbq.gbq.GenericGBQException
(see GitHub page).
Code snippet / example below:
import pandas_gbq
try:
pandas_gbq.read_gbq(f"YOUR_QUERY_HERE`", project_id="PROJECT_ID")
print('yep')
except pandas_gbq.gbq.GenericGBQException:
print('nope')
Upvotes: 0
Reputation: 2057
I suspect you want to catch pd.GenericGBQException
. (Or perhaps gbq.GenericGBQException
-- it depends on your imports. Are you importing the module that defines the exception you're trying to catch?)
Also, consider catching PandasError, the base class of all exceptions from the package: https://github.com/pydata/pandas/blob/master/pandas/io/gbq.py#L85
Upvotes: 1