Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch exception and return empty dataframe

Tags:

python

pandas

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

#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

like image 296
ArchieTiger Avatar asked Jun 30 '16 11:06

ArchieTiger


People also ask

How do I return an empty data frame?

We can use the dropna() function of the pandas DataFrame class to remove all the NaN values in the DataFrame. Then we apply the empty property on the DataFrame object to check the result and it will return True.

How do you handle an empty data frame?

If Series/DataFrame is empty, return True, if not return False. Return series without null values. Return DataFrame with labels on given axis omitted where (all or any) data are missing. If Series/DataFrame contains only NaNs, it is still not considered empty.

How do you find the empty value of a DataFrame?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

How check Pyspark DataFrame is empty?

isEmpty. Returns True if this DataFrame is empty.


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 to brenbarn who found the error name for dataerror at How can I catch a pandas DataError?

like image 135
Yarnspinner Avatar answered Nov 08 '22 04:11

Yarnspinner


You should probably check whether the dataframe is empty or not

if df.empty:
    return df

before calling pivot_table

like image 25
Régis Makhmara Avatar answered Nov 08 '22 04:11

Régis Makhmara