Catch exception and return empty framework

I query the database and save the result as a data framework, which I then transform with factorizec pivot_table. This works fine when a database query returns data, but it throws an error when data is not returned (this is to be expected). How to catch this exception and return an empty framework?

#When dataframe is non-empty, transformation works fine:
print df

   sale name  year
0   41  Jason  2012
1   24  Molly  2012
2   31  Jason  2013
3   32  Jason  2014
4   31  Molly  2014


df['groups'] = (pd.factorize(df.year)[0] + 1).astype(str)

df1 = (df.pivot_table(index='name', columns='groups', values=['sale', 'year']))
df1.columns = [''.join(col) for col in df1.columns]
print (df1)

       sale1  sale2  sale3   year1   year2   year3
name                                              
Jason   41.0   31.0   32.0  2012.0  2013.0  2014.0
Molly   24.0    NaN   31.0  2012.0     NaN  2014.0

#But when dataframe is empty, factorize by pivot_table throws error

df  = pd.DataFrame(columns=['sales','name','year'])
df1 = (df.pivot_table(index='name', columns='groups', values=['sale', 'year']))
df1.columns = [''.join(col) for col in df1.columns]
print (df1)

DataError: No numeric types to aggregate

+4
source share
2 answers
try:
    df1 = df.pivot_table(index='name', columns='name', values=['sale', 'year'])
except pd.core.groupby.DataError:
    df1 = pd.DataFrame()
except:
    raise

Credits for brenbarn that found the error name for dataerror in How can I catch a pandas DataError?

+3
source

You should probably check if the data frame is free or not

if df.empty:
    return df

before calling pivot_table

0
source

All Articles