0

I am currently using the Truc function of Django to aggregate some data within the day and hour.

I would like to do the same but over 15 minutes, instead of an hour or just a minute but I can't figure it out.

Here is an example of my current query:

data_trunc = data_qs.annotate(
   start_day=Trunc('date_created', 'minute', output_field=DateTimeField()))
   .values('start_day', 'content', 'device')

The problem lies with the 'minutes' argument that can be passed to Trunc. As far as I get it, there is no choice inbetween 'hour' and 'minutes'.

How can I group my data over 15 minutes and have a larger time span for the data grouping ?

I know that I could do this by hand afterward but I'd really like to have the database do this for me since there is a large dataset to compute and this way is the most efficient one I have yet.
If this is the only way though I am opened to suggestions to the most efficient ways to get around this.

Thanks for your help


Edit
I should have specified I did not wish to use raw SQL to do this and stick with Django ORM or native Python.

d6bels
  • 1,432
  • 2
  • 18
  • 30
  • Possible duplicate of [What is the fastest way to truncate timestamps to 5 minutes in Postgres?](https://stackoverflow.com/questions/7299342/what-is-the-fastest-way-to-truncate-timestamps-to-5-minutes-in-postgres) – solarissmoke Oct 28 '17 at 03:21

1 Answers1

0

Well,

Since I do not want to go along the path of using Postgres functions and stick with the ORM, I ended up manually trunc-ing the data iterating all items.

def trunc_minutes(raw_data, groupping=15):

truncated_arranged = []
current_slice = []
end_time = raw_data[0]['start_day'] + timedelta(minutes=groupping)

for d in raw_data:
    if d['start_day'] <= end_time:
        current_slice.append(d)
    else:
        truncated_arranged.append(current_slice)
        current_slice = [d]
        end_time = d['start_day'] + timedelta(minutes=groupping)

return truncated_arranged

This might not be the fastest way but it works and does not require raw SQL.


Edit: Add d in the else part of the code so that the value that get in the new slice isn't forgotten

d6bels
  • 1,432
  • 2
  • 18
  • 30
  • I think there is small problem with the loop. When ever the loop falls in the else condition it will not store the current value in d anywhere and move over to the next loop. – hasan najeeb Oct 16 '20 at 20:11
  • Long time I didn't use that code, maybe the 2nd line of the 'else' part should be `current_slice = [d]` so that this value is not forgotten? Have you tried ? – d6bels Oct 26 '20 at 10:14
  • Seems like I had added an append in my code at the time, I'll just edit – d6bels Oct 29 '20 at 05:56