-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharvarddatasciencehw0.py
More file actions
139 lines (108 loc) · 4.13 KB
/
harvarddatasciencehw0.py
File metadata and controls
139 lines (108 loc) · 4.13 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from fnmatch import fnmatch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import requests
from pattern import web
# set some nicer defaults for matplotlib
from matplotlib import rcParams
#these colors come from colorbrewer2.org. Each is an RGB triplet
dark2_colors = [(0.10588235294117647, 0.6196078431372549, 0.4666666666666667),
(0.8509803921568627, 0.37254901960784315, 0.00784313725490196),
(0.4588235294117647, 0.4392156862745098, 0.7019607843137254),
(0.9058823529411765, 0.1607843137254902, 0.5411764705882353),
(0.4, 0.6509803921568628, 0.11764705882352941),
(0.9019607843137255, 0.6705882352941176, 0.00784313725490196),
(0.6509803921568628, 0.4627450980392157, 0.11372549019607843),
(0.4, 0.4, 0.4)]
rcParams['figure.figsize'] = (10, 6)
rcParams['figure.dpi'] = 150
rcParams['axes.color_cycle'] = dark2_colors
rcParams['lines.linewidth'] = 2
rcParams['axes.grid'] = True
rcParams['axes.facecolor'] = '#eeeeee'
rcParams['font.size'] = 14
rcParams['patch.edgecolor'] = 'none'
# Problem 1
def get_poll_xml(poll_id):
link = "http://charts.realclearpolitics.com/charts/%i.xml" % int(poll_id)
return requests.get(link).text
import re
def _strip(s):
"""This function removes non-letter characters from a word
for example _strip('Hi there!') == 'Hi there'
"""
return re.sub(r'[\W_]+', '', s)
def plot_colors(xml):
"""
Given an XML document like the link above, returns a python dictionary
that maps a graph title to a graph color.
Both the title and color are parsed from attributes of the <graph> tag:
<graph title="the title", color="#ff0000"> -> {'the title': '#ff0000'}
These colors are in "hex string" format. This page explains them:
http://coding.smashingmagazine.com/2012/10/04/the-code-side-of-color/
Example
-------
>>> plot_colors(get_poll_xml(1044))
{u'Approve': u'#000000', u'Disapprove': u'#FF0000'}
"""
dom = web.Element(xml)
result = {}
for graph in dom.by_tag('graph'):
title = _strip(graph.attributes['title'])
result[title] = graph.attributes['color']
return result
"""
Function
---------
rcp_poll_data
Extract poll information from an XML string, and convert to a DataFrame
Parameters
----------
xml : str
A string, containing the XML data from a page like
get_poll_xml(1044)
Returns
-------
A pandas DataFrame with the following columns:
date: The date for each entry
title_n: The data value for the gid=n graph (take the column name from the `title` tag)
This DataFrame should be sorted by date
Example
-------
Consider the following simple xml page:
<chart>
<series>
<value xid="0">1/27/2009</value>
<value xid="1">1/28/2009</value>
</series>
<graphs>
<graph gid="1" color="#000000" balloon_color="#000000" title="Approve">
<value xid="0">63.3</value>
<value xid="1">63.3</value>
</graph>
<graph gid="2" color="#FF0000" balloon_color="#FF0000" title="Disapprove">
<value xid="0">20.0</value>
<value xid="1">20.0</value>
</graph>
</graphs>
</chart>
Given this string, rcp_poll_data should return
result = pd.DataFrame({'date': pd.to_datetime(['1/27/2009', '1/28/2009']),
'Approve': [63.3, 63.3], 'Disapprove': [20.0, 20.0]})
"""
def rcp_poll_data(xml):
dom = web.Element(xml)
result = {}
dates = dom.by_tag('series')[0]
dates = {n.attributes['xid']: str(n.content) for n in dates.by_tag('value')}
keys = dates.keys()
result['date'] = pd.to_datetime([dates[k] for k in keys])
for graph in dom.by_tag('graph'):
name = graph.attributes['title']
data = {n.attributes['xid']: float(n.content)
if n.content else np.nan for n in graph.by_tag('value')}
result[name] = [data[k] for k in keys]
result = pd.DataFrame(result)
result = result.sort(columns=['date'])
return result