Custom Error Messaging In Python For No Rows Returned

Sometimes when using python code, there will be cases when no data is returned. To account for this in python if you can first check if the dataframe has empty rows with.
if df.size == 0:
(then run all of my actual code)
else:
(run code to show error message)
With python matplotlib charts, this can be accomplished as follow:
# SQL output is imported as a pandas dataframe variable called "df"
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Output: matplot ax.text object
fig, ax = plt.subplots(figsize = (8,3))
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
ax.axis('off')
# Function that defines the matplotlib object
def actual_text():
string = df.iloc[0,0]
ax.text(y = 0.7,
x = 0.5,
s = str(df.iloc[0,0]),
horizontalalignment='center',
verticalalignment='center',
fontsize = find_font_size(string),
fontweight = 300,
family = 'sans-serif')
return ax
# Function that defines the error text
def error_text():
ax.text(x = 0.5, y = 0.5, s = 'Please input site_id into salesforce',
horizontalalignment='center',
verticalalignment='center',
color = 'gray', fontsize = 20, family = 'sans-serif')
return ax
# Final logic to figure out if an error message should be returned
if df.size == 0:
periscope.output(error_text())
else:
periscope.output(actual_text())
Updated 02-09-2024
intapiuser
Admin
Joined December 15, 2022