@@ -235,11 +235,10 @@ async def fetch_chart_data(chart_filters: ChartFilters = ''):
235235 # charts pt1
236236 nested_json1 = (
237237 df .groupby (['id' , 'chart_name' , 'chart_type' ]).apply (
238- lambda x : x [['name' , 'value' , 'unit_of_measurement' ]].to_dict (orient = 'records' ), include_groups = False ).reset_index (
239- name = 'chart_value' )
238+ lambda x : x [['name' , 'value' , 'unit_of_measurement' ]].to_dict (orient = 'records' ), include_groups = False ).reset_index ()
240239 )
240+ nested_json1 .columns = ['id' , 'chart_name' , 'chart_type' , 'chart_value' ]
241241 result1 = nested_json1 .to_dict (orient = 'records' )
242-
243242 sql_stmt = f'''SELECT TOP 1 WITH TIES
244243 mined_topic as name, 'TOPICS' as id, 'Trending Topics' as chart_name, 'table' as chart_type,
245244 lower(sentiment) as average_sentiment,
@@ -258,12 +257,17 @@ async def fetch_chart_data(chart_filters: ChartFilters = ''):
258257 df = pd .DataFrame (rows , columns = column_names )
259258
260259 # charts pt2
261- nested_json2 = (
262- df .groupby (['id' , 'chart_name' , 'chart_type' ]).apply (
263- lambda x : x [['name' , 'call_frequency' , 'average_sentiment' ]].to_dict (orient = 'records' ), include_groups = False ).reset_index (
264- name = 'chart_value' )
265- )
266- result2 = nested_json2 .to_dict (orient = 'records' )
260+ if not df .empty :
261+ nested_json2 = (
262+ df .groupby (['id' , 'chart_name' , 'chart_type' ]).apply (
263+ lambda x : x [['name' , 'call_frequency' , 'average_sentiment' ]].to_dict (orient = 'records' ),
264+ include_groups = False
265+ ).reset_index ()
266+ )
267+ nested_json2 .columns = ['id' , 'chart_name' , 'chart_type' , 'chart_value' ]
268+ result2 = nested_json2 .to_dict (orient = 'records' )
269+ else :
270+ result2 = []
267271
268272 where_clause = where_clause .replace ('mined_topic' , 'topic' )
269273 sql_stmt = f'''select top 15 key_phrase as text,
@@ -293,15 +297,19 @@ async def fetch_chart_data(chart_filters: ChartFilters = ''):
293297
294298 df = df .head (15 )
295299
296- nested_json3 = (
297- df .groupby (['id' , 'chart_name' , 'chart_type' ]).apply (
298- lambda x : x [['text' , 'size' , 'average_sentiment' ]].to_dict (orient = 'records' ), include_groups = False ).reset_index (
299- name = 'chart_value' )
300- )
301- result3 = nested_json3 .to_dict (orient = 'records' )
300+ if not df .empty :
301+ nested_json3 = (
302+ df .groupby (['id' , 'chart_name' , 'chart_type' ]).apply (
303+ lambda x : x [['text' , 'size' , 'average_sentiment' ]].to_dict (orient = 'records' ),
304+ include_groups = False
305+ ).reset_index ()
306+ )
307+ nested_json3 .columns = ['id' , 'chart_name' , 'chart_type' , 'chart_value' ]
308+ result3 = nested_json3 .to_dict (orient = 'records' )
309+ else :
310+ result3 = []
302311
303312 final_result = result1 + result2 + result3
304-
305313 return final_result
306314
307315 finally :
0 commit comments