-
Notifications
You must be signed in to change notification settings - Fork 3
/
Class14_InClassDemoPandas.py
51 lines (40 loc) · 1.36 KB
/
Class14_InClassDemoPandas.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple demo of using Pandas to visualize time series data
"""
import pymysql
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#edit for your specific ODM database
conn = pymysql.connect(host='localhost', port=3306, user='root', \
passwd='', db='LBRODM_small')
cursor = conn.cursor()
#hard coded to plot water temperature observations at SiteID = 2
sql_statement = "SELECT LocalDateTime, DataValue FROM DataValues \
WHERE SiteID = 2 AND VariableID = 36 ORDER BY LocalDateTime"
cursor.execute(sql_statement)
rows = cursor.fetchall()
localDateTimes, dataValues = zip(*rows)
ts = pd.Series(dataValues, index=localDateTimes)
fig, axes = plt.subplots(nrows=2, ncols=2)
#time series plot
ts.plot(ax=axes[0,0])
axes[0,0].set_title('Temperature at SiteID=2')
#weekly stats plot
ts.resample('W', how=['mean', np.min, np.max]).plot(ax=axes[0,1])
axes[0,1].set_title('Weekly Resample')
#montly stats plot
ts.resample('M', how=['mean', np.min, np.max]).plot(ax=axes[1,0])
axes[1,0].set_title('Monthly Resample')
#montly boxplot
df = pd.DataFrame(ts, columns=['tmp'])
df['mon'] = df.index.month
df.boxplot(column = 'tmp', by='mon', ax=axes[1,1])
axes[1,1].set_title('Monthly Boxplot')
fig = axes[1][1].get_figure()
fig.suptitle('')
plt.tight_layout()
plt.savefig('Class14_InClassDemoPandas.png')
plt.show()