Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import numpy, csv, sys
from itertools import groupby
from functools import partial
reader = csv.reader(open("data.csv", "r"), delimiter=";")
next(reader, None) # skip header
data = numpy.array([
(datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S"), int(valstr))
for [datestr, valstr, *_] in reader])
def bymin(mins, d):
date,val = d
return date - timedelta(minutes=date.minute%mins,seconds=date.second)
def average(averager, vals):
return averager([v for (k,v) in vals])
def make(name, grouper, averager):
grouped = numpy.array([(date, average(averager, vals)) for (date, vals) in groupby(data, grouper)])
dp = plt.plot([date for date,val in grouped], [val for date,val in grouped])
make("mean5", grouper=partial(bymin, 5), averager=numpy.mean)
make("mean1", grouper=partial(bymin, 1), averager=numpy.mean)
make("median5", grouper=partial(bymin, 5), averager=numpy.median)
make("median1", grouper=partial(bymin, 1), averager=numpy.median)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.