forked from matplotlib/interactive_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpddc_helpers.py
86 lines (66 loc) · 2.2 KB
/
pddc_helpers.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
from urllib.request import urlopen
import datetime
import numpy as np
import pandas as pd
import gzip
import os
def aggregate_by_month(df, col='T'):
gb = df.groupby(('year', 'month'))['T'].describe().unstack()
new_index = [datetime.date(*m, *(15, )) for m in gb.index]
gb.reset_index(inplace=True)
gb.index = new_index
return gb
def aggregate_by_day(df, col='T'):
gb = df.groupby(('year', 'month', 'day'))['T'].describe().unstack()
new_index = [datetime.date(*m) for m in gb.index]
gb.reset_index(inplace=True)
gb.index = new_index
return gb
def extract_month_of_daily(daily, year, month):
ix = (daily['month'] == month) & (daily['year'] == year)
df = daily[ix]
idx = [(m - df.index[0]).days for m in df.index]
df.reset_index(inplace=True)
df.index = idx
return df
def extract_day_of_hourly(hourly_df, year, month, day):
"""Given a data frame with hourly data, extract data for year-month-day
Parameters
----------
hourly_df : DataFrame
Must have columns 'year', 'month', 'day' with the expected semantics and
a time index
year, month, day : int
The day to extract the data for
"""
ix = ((hourly_df['month'] == month) &
(hourly_df['year'] == year) &
(hourly_df['day'] == day))
df = hourly_df[ix]
midnight = datetime.datetime(year, month, day, 0, 0)
df.index = [(m - midnight).seconds / 3600 for m in df.index]
return df
def load_bwi_data():
fname = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'bwi.h5')
return pd.read_hdf(fname)
def label_date(ax, label, date, df):
'''Helper function to annotate a date
``date`` is assumed to be in the index of ``df``
Parameters
----------
ax : Axes
The axes to draw to
label : str
The text of the label
date : object in index of df
The x coordinate
df : DataFrame
The data source
'''
y = df.loc[date]['mean']
return ax.annotate(label, (date, y),
ha='right',
xytext=(-10, -30),
textcoords='offset points',
arrowprops={'arrowstyle': '->'})