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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.