I am running a query (postgres) which creates a temporary column grp dividing the time difference between event_time and start_date (a datetime object) into time periods of given length.
date = func.date_trunc('day', M.event_time)
session.query(..., ((date - start_date) / period_length).label('grp'))
.filter(..., date >= start_date)
.group_by('grp', ...)
This works fine as long as period_length = 1, but if it is 2, for example, the result is a timedelta which has half days datetime.timedelta(days=2, seconds=43200).
How do I cast this to integer before group_by to get my expected result?