Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'yet.': 0.04; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'def': 0.12; 'random': 0.14; '0),': 0.16; '24,': 0.16; 'collections': 0.16; 'defaultdict': 0.16; 'interval.': 0.16; 'itertools': 0.16; 'namedtuple': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:[ 30': 0.16; 'skip:{ 30': 0.16; 'there!': 0.16; 'vars:': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'example': 0.22; 'import': 0.22; 'putting': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'group:': 0.31; 'values.': 0.31; 'subject:time': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'objects': 0.35; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'days': 0.60; 'email addr:gmail.com': 0.63; 'skip:n 10': 0.64; 'sum': 0.64; 'here': 0.66 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Group by interval time Date: Thu, 12 Feb 2015 14:29:59 +0100 Organization: None References: <6f6f567d-7e25-4a74-96ef-d20cb44a9646@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8940.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423747817 news.xs4all.nl 2912 [2001:888:2000:d::a6]:44826 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85582 charles.sartori@gmail.com wrote: > Hello there! > > I`m trying to group by a list of Row() objects in 12days interval and > sum(). values. Here is an example of the list > > [Row(time=datetime.datetime(2013, 1, 1, 0, 0), sum=4676557380615), > [Row(time=datetime.datetime(2013, 1, 2, 0, 0), sum=6549630855895), > [Row(time=datetime.datetime(2013, 1, 3, 0, 0), sum=6549630855895), ...] > > Row() objects has two vars: row.time and row.sum > > > The result that I`m looking for is: > [[datetime.datetime(2013, 1, 1, 0, 0), value], > [datetime.datetime(2013, 1, 12, 0, 0), value], > [datetime.datetime(2013, 1, 24, 0, 0), value] > ] > Where value is the sum() of all row.sum in that interval. > > I`m trying to use itertools.groupby by I could not get it to work yet. If the data is sorted by time then you can use groupby, otherwise consider putting it into a (default)dict. Here is an example for both methods: import datetime import random from itertools import groupby from collections import defaultdict, namedtuple BASEDATE = datetime.datetime(2015, 1, 1) INTERVAL = 12 # time interval in days DAYS = datetime.timedelta(days=INTERVAL) def make_sample_rows(): random.seed(42) Row = namedtuple("Row", "time sum") return [ Row(BASEDATE + datetime.timedelta(days=random.randrange(-20, 80)), random.randrange(300)) for i in range(30)] def get_key(row): offset = (row.time - BASEDATE).days // INTERVAL return BASEDATE + datetime.timedelta(days=offset) def format_time(time): return time.strftime("%Y-%m-%d") if __name__ == "__main__": rows = make_sample_rows() # with groupby() for key, group in groupby(sorted(rows), key=get_key): print("{} - {}".format(format_time(key), format_time(key+DAYS))) print("-" * 23) group = list(group) for row in group: print("{} {:4}".format(format_time(row.time), row.sum)) print("{:>15}".format("----")) print("{:15}".format(sum(row.sum for row in group))) print("") # with defaultdict d = defaultdict(int) for row in rows: d[get_key(row)] += row.sum for time, sum in sorted(d.items()): print("{} {:4}".format(format_time(time), sum))