def month_floor(dt, n):
    """Round datetime down to nearest date that falls evenly on an
    n-month boundary. (E.g., valid intervals for a 3-month
    boundary are 1/1, 4/1, 7/1, and 10/1)"""
    year = dt.replace(month=1, day=1, hour=0,
                      minute=0, second=0, microsecond=0)
    next = year
    curr = None
    if next == dt:
        return dt
    else:
        while next < dt:
            curr = next
            if next.month + n > 12:
                break
            next = next.replace(month=next.month + n)
        return curr