import datetime


def group_datetime(d, interval):
    """Group datetime in bins."""
    seconds = d.second + d.hour * 3600 + d.minute * 60 + d.microsecond / 1000
    k = d - datetime.timedelta(seconds=seconds % interval)
    return datetime.datetime(k.year, k.month, k.day, k.hour, k.minute, k.second)