-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpm_stats_to_display.py
96 lines (73 loc) · 3.33 KB
/
pm_stats_to_display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import geo_plotting
# friendly note: units are in micrograms per cubic meter
def get_first_and_last_dates():
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
start_date = df['timestamp'].values[0]
end_date = df['timestamp'].values[-1:]
if type(end_date == np.ndarray):
end_date = end_date[0]
return [start_date, end_date]
def get_pm1_last10_avg():
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_ten = df.iloc[-10:]
pm1_last10_avg = last_ten['pm1'].mean()
print(pm1_last10_avg)
return pm1_last10_avg
def get_pm10_last10_avg():
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_ten = df.iloc[-10:]
pm10_last10_avg = last_ten['pm10'].mean()
print(pm10_last10_avg)
return pm10_last10_avg
def get_pm25_last10_avg():
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_ten = df.iloc[-10:]
pm25_last10_avg = last_ten['pm25'].mean()
print(pm25_last10_avg)
return pm25_last10_avg
def most_recent_pm_reading(pm_size):
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_row = df.iloc[-1]
return last_row[pm_size]
def pm_last_hr_avg(pm_size):
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_date = get_first_and_last_dates()[1]
last_date_dt = datetime.strptime(last_date[:-7], "%Y-%m-%d %H:%M:%S")
last_hour_date_time = last_date_dt - timedelta(hours = 1)
df['timestamp_dt'] = df['timestamp'].apply(lambda x: datetime.strptime(x[:-7], "%Y-%m-%d %H:%M:%S"))
mask = (df['timestamp_dt'] > last_hour_date_time) & (df['timestamp_dt'] <= df['timestamp_dt'])
df_last_hr = pd.DataFrame()
df_last_hr = df[mask]
avg_pm_last_hr = df_last_hr[pm_size].mean().round(2)
return avg_pm_last_hr
def pm_last_day_avg(pm_size):
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_date = get_first_and_last_dates()[1]
last_date_dt = datetime.strptime(last_date[:-7], "%Y-%m-%d %H:%M:%S")
last_day_date_time = last_date_dt - timedelta(days = 1)
df['timestamp_dt'] = df['timestamp'].apply(lambda x: datetime.strptime(x[:-7], "%Y-%m-%d %H:%M:%S"))
mask = (df['timestamp_dt'] > last_day_date_time) & (df['timestamp_dt'] <= df['timestamp_dt'])
df_last_day = pd.DataFrame()
df_last_day = df[mask]
avg_pm_last_day = df_last_day[pm_size].mean().round(2)
return avg_pm_last_day
def pm_last_week_avg(pm_size):
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
last_date = get_first_and_last_dates()[1]
last_date_dt = datetime.strptime(last_date[:-7], "%Y-%m-%d %H:%M:%S")
last_week_date_time = last_date_dt - timedelta(weeks = 1)
df['timestamp_dt'] = df['timestamp'].apply(lambda x: datetime.strptime(x[:-7], "%Y-%m-%d %H:%M:%S"))
mask = (df['timestamp_dt'] > last_week_date_time) & (df['timestamp_dt'] <= df['timestamp_dt'])
df_last_week = pd.DataFrame()
df_last_week = df[mask]
avg_pm_last_week = df_last_week[pm_size].mean().round(2)
return avg_pm_last_week
def pm_all_time_avg(pm_size):
df = pd.read_csv("data/processed/airquality_data_to_use.csv")
avg_pm_all_time = df[pm_size].mean().round(2)
return avg_pm_all_time
if __name__ == "__main__":
pm_all_time_avg('pm1')