Sunburst Chart - ZeroDivisionError: Weights sum to zero, can't be normalized

Hello - Iโ€™m having an issue with increasing the level of granularity in a sunburst chart. The chart renders fine when I limit the path to two levels of granularity (CUR_FIN_CLASS and CUR_PAYOR):

image

but when including an additional path (AGING_BUCKET), I continually receive a ZeroDivisionError. It must be how the px.sunburst function is calculating the outermost slices. Any idea how I might prep the data in the pivot_table call to proof it so when passed into the px.suburst function, I avoid the ZeroDivisionError? Any help would be greatly appreciated!

Here is my code:

def ArSnBrstdf(dataFrame):

df = dataFrame 
df = pd.pivot_table(df, values='OUTSTANDING_AMT',index=['CUR_FIN_CLASS','CUR_PAYOR','AGING_BUCKET'], aggfunc=np.sum,fill_value=None,dropna=True).reset_index()
df['ALL'] = 'PAYOR_DATA'

fig = px.sunburst(df, path=['ALL','CUR_FIN_CLASS','CUR_PAYOR','AGING_BUCKET'],values='OUTSTANDING_AMT',
                    color='OUTSTANDING_AMT',
                    color_continuous_scale='gnbu',
                    )

fig.show()    
#return df

ArSnBrstdf(dfAr)

1 Like

Found the answer. In the outermost level, one of the child slices contained a negative balance so the sunburst was not building. Apologies to anyone that spent time looking at this. The answer is: clean your data!

1 Like

The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero. In Mathematics, when a number is divided by a zero, the result is an infinite number. It is impossible to write an Infinite number physically. Python interpreter throws โ€œZeroDivisionError: division by zeroโ€ error if the result is infinite number. While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate.

try:
    z = x / y
except ZeroDivisionError:
    z = 0

Or check before you do the division:

if y == 0:
    z = 0
else:
    z = x / y

The latter can be reduced to:

z = 0 if y == 0 else (x / y)