-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphite2pandas.py
55 lines (42 loc) · 1.47 KB
/
graphite2pandas.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
import pandas
import numpy
import requests
import json
"""
Script to convert `Graphite <http://graphite.readthedocs.org/en/latest/>`_ data
to a `Pandas <http://pandas.pydata.org/>`_ DataFrame.
:Author: Valentin Haenel <[email protected]>
:Licence: `WTFPL <http://www.wtfpl.net/>`_
Can be used to do fetch and load data for exploratory data analysis (EDA) and
for prototyping advanced algorithms on your local machine before hacking on
graphites ``functions.py``.
Example
-------
>>> from graphite2pandas import g2p
>>> url = 'http://grp.example.com/render/?target=metric.data.foo&format=json&from=-31d'
>>> df = g2p(url)
>>> df.plot()
Notes
-----
* You need to request data as JSON using `format=json`
* You can specify a timezone using the `localize` parameter
"""
def _localize(index, tz):
index = index.tz_localize('UTC')
index = index.tz_convert('CET')
return index
def g2p(url, localize='CET'):
resp = requests.get(url)
decoded_json = json.loads(resp.content)
times, values, labels = [], [], []
for element in decoded_json:
labels.append(element['target'])
datapoints = zip(*element['datapoints'])
values.append(datapoints[0])
times.append(datapoints[1])
index = pandas.DatetimeIndex((numpy.array(times[0],dtype='datetime64[s]')))
if localize:
index = _localize(index, localize)
return pandas.DataFrame(
dict(((labels[i], values[i]) for i in range(len(values)))),
index=index)