diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py index a172374..c9bc587 100644 --- a/api/api_v1/endpoints/query.py +++ b/api/api_v1/endpoints/query.py @@ -168,9 +168,9 @@ async def event_model( df = pd.concat([df, pd.DataFrame(concat_data, columns=df.columns)]) df.sort_values('date', inplace=True) if len(df) >= 2: - q['chain_ratio'] = round((df.iloc[-1, 1] - df.iloc[-2, 1]) * 100 / df.iloc[-2, 1], 2) + q['chain_ratio'] = division((df.iloc[-1, 1] - df.iloc[-2, 1]) * 100 , df.iloc[-2, 1], 2) if len(df) >= 8: - q['wow'] = round((df.iloc[-1, 1] - df.iloc[-8, 1]) * 100 / df.iloc[-8, 1], 2) + q['wow'] = division((df.iloc[-1, 1] - df.iloc[-8, 1]) * 100 / df.iloc[-8, 1], 2) or 0 q['values'].append(df['values'].to_list()) q['sum'].append(int(df['values'].sum())) if item['time_particle'] in ('P1D', 'P1W'): diff --git a/common/compute.py b/common/compute.py index 05a5aa6..38e7bd5 100644 --- a/common/compute.py +++ b/common/compute.py @@ -1,7 +1,10 @@ +import numpy as np def division(a, b, n=2): res = 0 try: res = round(a / b, n) + if np.isnan(res): + res = 0 except ZeroDivisionError: pass return res