Here is another solution using numpy.
First we need to change the data so that it looks a bit like a matrix. we will use the default dict over the years as keys and lists of floats as values.
>>> pre_matrix = collections.defaultdict(lambda:[0]*12) >>> for row in tdata: ... pre_matrix[row[0].year][row[0].month - 1] += row[1] ...
Since we don’t want the array containing each year with Common Era, it allows us to examine pre-formatted data and extract the minimum and maximum years.
>>> r = range(min(pre_matrix.keys()),1+max(pre_matrix.keys()))
Finally, build a matrix, each row of which contains data for one year.
>>> matrix = numpy.array([pre_matrix[y] for y in r])
From there, just get the sum of rows and columns. we will use zip() to return interesting date values.
>>> zip((datetime.datetime(1970, i+1, 1).strftime("%b"), s) for i, s in enumerate(matrix.sum(0))) [(('Jan', 3000.0),), (('Feb', 14107.0),), (('Mar', 6000.0),), (('Apr', 14400.0),), (('May', 15960.0),), (('Jun', 3000.0),), (('Jul', 6000.0),), (('Aug', 10170.0),), (('Sep', 6000.0),), (('Oct', 6263.0),), (('Nov', 4170.0),), (('Dec', 18000.0),)]
Since we do not need to localize the years, this is a little easier.
>>> list(zip(r, matrix.sum(1))) [(2002, 12000.0), (2003, 0.0), (2004, 0.0), (2005, 6000.0), (2006, 0.0), (2007, 0.0), (2008, 12000.0), (2009, 15000.0), (2010, 27563.0), (2011, 34507.0)]