Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85578 > unrolled thread
| Started by | charles.sartori@gmail.com |
|---|---|
| First post | 2015-02-12 04:34 -0800 |
| Last post | 2015-02-12 06:54 -0800 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
Group by interval time charles.sartori@gmail.com - 2015-02-12 04:34 -0800
Re: Group by interval time MRAB <python@mrabarnett.plus.com> - 2015-02-12 13:17 +0000
Re: Group by interval time alister <alister.nospam.ware@ntlworld.com> - 2015-02-12 13:29 +0000
Re: Group by interval time Peter Otten <__peter__@web.de> - 2015-02-12 14:29 +0100
Re: Group by interval time charles.sartori@gmail.com - 2015-02-12 06:54 -0800
| From | charles.sartori@gmail.com |
|---|---|
| Date | 2015-02-12 04:34 -0800 |
| Subject | Group by interval time |
| Message-ID | <6f6f567d-7e25-4a74-96ef-d20cb44a9646@googlegroups.com> |
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. Thnak you.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-02-12 13:17 +0000 |
| Message-ID | <mailman.18689.1423747073.18130.python-list@python.org> |
| In reply to | #85578 |
On 2015-02-12 12:34, 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. > Try grouping by the number of days since datetime.datetime(2013, 1, 1, 0, 0), integer-divided by 12.
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2015-02-12 13:29 +0000 |
| Message-ID | <mbi9rt$o7c$1@speranza.aioe.org> |
| In reply to | #85578 |
On Thu, 12 Feb 2015 04:34:03 -0800, charles.sartori 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. > > Thnak you. what is the code you have tried & what errors are you getting? -- life, n.: A whim of several billion cells to be you for a while.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-02-12 14:29 +0100 |
| Message-ID | <mailman.18690.1423747817.18130.python-list@python.org> |
| In reply to | #85578 |
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))
[toc] | [prev] | [next] | [standalone]
| From | charles.sartori@gmail.com |
|---|---|
| Date | 2015-02-12 06:54 -0800 |
| Message-ID | <07d750f9-04be-457c-9f29-bd4db700aff7@googlegroups.com> |
| In reply to | #85582 |
Thank you Peter, I was doing wrong at get_key function... Thnak you so much!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web