allowing for nan values in year comparison

This commit is contained in:
simon 2021-09-01 11:55:10 +07:00
parent 15c0945d1a
commit f4dd0d08aa
1 changed files with 27 additions and 12 deletions

View File

@ -93,13 +93,13 @@ class MonthGenerator():
def get_data(self):
""" export from postgres """
m_query = ('SELECT epoch_time, aqi_value FROM aqi WHERE '
f'epoch_time > {self.m_stamp[0]} AND '
f'epoch_time < {self.m_stamp[1]} '
'ORDER BY epoch_time DESC;')
f'epoch_time > {self.m_stamp[0]} AND '
f'epoch_time < {self.m_stamp[1]} '
'ORDER BY epoch_time DESC;')
y_query = ('SELECT epoch_time, aqi_value FROM aqi WHERE '
f'epoch_time > {self.y_stamp[0]} AND '
f'epoch_time < {self.y_stamp[1]} '
'ORDER BY epoch_time DESC;')
f'epoch_time > {self.y_stamp[0]} AND '
f'epoch_time < {self.y_stamp[1]} '
'ORDER BY epoch_time DESC;')
# make the call
db_handler = DatabaseConnect()
m_rows = db_handler.db_execute(m_query)
@ -128,10 +128,15 @@ class MonthGenerator():
df = pd.DataFrame(data)
indexed = df.set_index('timestamp')
indexed.sort_values(by=['timestamp'], inplace=True)
y_mean = indexed.resample('8h').mean().round()
# skip if empty
if len(indexed) == 0:
y_mean = indexed
else:
y_mean = indexed.resample('8h').mean().round()
# reset timestamp to day
y_mean.reset_index(level=0, inplace=True)
y_mean['timestamp'] = y_mean['timestamp'].dt.strftime('%d %H:%M')
if len(indexed):
y_mean['timestamp'] = y_mean['timestamp'].dt.strftime('%d %H:%M')
y_mean.set_index('timestamp', inplace=True)
# merge the two
mean['year_aqi'] = y_mean['year_aqi']
@ -203,6 +208,10 @@ class MonthGenerator():
@staticmethod
def get_change(m_val, y_val):
""" helper function to get change on thresh """
# skip if nan
if y_val == 'nan':
return y_val
diff_avg = (m_val - y_val) / m_val
if diff_avg <= -0.15:
avg_change = 'down'
@ -223,10 +232,16 @@ class MonthGenerator():
m_avg = int(self.axis['y_1'].mean())
m_cat = self.get_aqi(m_avg)
# last
y_min = int(self.axis['y_2'].min())
y_max = int(self.axis['y_2'].max())
y_avg = int(self.axis['y_2'].mean())
y_cat = self.get_aqi(y_avg)
try:
y_min = int(self.axis['y_2'].min())
y_max = int(self.axis['y_2'].max())
y_avg = int(self.axis['y_2'].mean())
y_cat = self.get_aqi(y_avg)
except ValueError:
y_min = 'nan'
y_max = 'nan'
y_avg = 'nan'
y_cat = 'nan'
# build dict
monthly_dict = {
'data': [