简体   繁体   English

Django Trunc 数据到 15 分钟

[英]Django Trunc data to 15 minutes

I am currently using the Truc function of Django to aggregate some data within the day and hour.我目前正在使用 Django 的Truc函数来聚合一天和一小时内的一些数据。

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.我想做同样的事情,但超过 15 分钟,而不是一个小时或一分钟,但我无法弄清楚。

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 .问题在于可以传递给Trunc'minutes'参数。 As far as I get it, there is no choice inbetween 'hour' and 'minutes' .据我所知,在'hour''minutes'没有选择。

How can I group my data over 15 minutes and have a larger time span for the data grouping ?如何将我的数据分组超过 15 分钟,并为数据分组提供更大的时间跨度?

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.我应该指定我不希望使用原始 SQL 来执行此操作并坚持使用 Django ORM 或本机 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.由于我不想走使用 Postgres 函数的道路并坚持使用 ORM,因此我最终手动截断了迭代所有项目的数据。

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.这可能不是最快的方法,但它有效并且不需要原始 SQL。


Edit : Add d in the else part of the code so that the value that get in the new slice isn't forgotten编辑:在代码的else部分添加d以便不会忘记在新切片中获得的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM