Pandas GenericGBQException

2020-05-09 09:43发布

问题:

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.

回答1:

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