(frame)
return fix_gap_in_list(hours), threshold, frame_count
Return a list of the hours with the most activity within a frame.

    Args:
        frame: a pandas DataFrame object that contains a datetime column.

    Returns:
        A tuple that contains three items:
            1. list of hours where the most activity within the DataFrame
            occurs.
            2. the threshold used to determine what is considered to be
            an active hour.
            3. a DataFrame object containing the aggregation.
Return a list of the hours with the most activity within a frame.
Return a list of the hours with the most activity within a frame .
def get_active_hours(frame):
    """Return a list of the hours with the most activity within a frame.

    Args:
        frame: a pandas DataFrame object that contains a datetime column.

    Returns:
        A tuple that contains three items:
            1. list of hours where the most activity within the DataFrame
            occurs.
            2. the threshold used to determine what is considered to be
            an active hour.
            3. a DataFrame object containing the aggregation.

    """
    frame_count = frame[['datetime', 'hour']].groupby(
        'hour', as_index=False).count()
    frame_count['count'] = frame_count['datetime']
    del frame_count['datetime']

    stats = frame_count['count'].describe()

    # Define few different options for the threshold value of what constitutes
    # an active hour. Then we choose the method that has the highest active
    # hour count, as long as it is between 3 and 12 hours.
    thresholds = {
        stats['75%'] - stats['mean']: 0,
        stats['75%']: 0,
        stats['50%']: 0,
        stats['25%']: 0
    }
    for threshold in thresholds:
        threshold_filter = frame_count['count'] >= threshold
        hours = list(frame_count[threshold_filter].hour.values)
        hours = sorted(hours)

        hour_len = len(hours)
        if 3 <= hour_len <= 12:
            thresholds[threshold] = hour_len

    threshold_counter = collections.Counter(thresholds)
    threshold, _ = threshold_counter.most_common(1)[0]

    threshold_filter = frame_count['count'] >= threshold
    hours = list(frame_count[threshold_filter].hour.values)
    hours = sorted(hours)
    runs = get_list_of_consecutive_sequences(hours)

    # There should either be a single run or at most two.
    number_runs = len(runs)
    if number_runs == 1:
        return hours, threshold, frame_count

    if number_runs == 2 and runs[0][0] == 0:
        # Two runs, first one starts at hour zero.
        return hours, threshold, frame_count

    return fix_gap_in_list(hours), threshold, frame_count
def get_active_hours ( frame ) : frame_count = frame [ [ 'datetime' , 'hour' ] ] . groupby ( 'hour' , as_index = False ) . count ( ) frame_count [ 'count' ] = frame_count [ 'datetime' ] del frame_count [ 'datetime' ] stats = frame_count [ 'count' ] . describe ( ) # Define few different options for the threshold value of what constitutes # an active hour. Then we choose the method that has the highest active # hour count, as long as it is between 3 and 12 hours. thresholds = { stats [ '75%' ] - stats [ 'mean' ] : 0 , stats [ '75%' ] : 0 , stats [ '50%' ] : 0 , stats [ '25%' ] : 0 } for threshold in thresholds : threshold_filter = frame_count [ 'count' ] >= threshold hours = list ( frame_count [ threshold_filter ] . hour . values ) hours = sorted ( hours ) hour_len = len ( hours ) if 3 <= hour_len <= 12 : thresholds [ threshold ] = hour_len threshold_counter = collections . Counter ( thresholds ) threshold , _ = threshold_counter . most_common ( 1 ) [ 0 ] threshold_filter = frame_count [ 'count' ] >= threshold hours = list ( frame_count [ threshold_filter ] . hour . values ) hours = sorted ( hours ) runs = get_list_of_consecutive_sequences ( hours ) # There should either be a single run or at most two. number_runs = len ( runs ) if number_runs == 1 : return hours , threshold , frame_count if number_runs == 2 and runs [ 0 ] [ 0 ] == 0 : # Two runs, first one starts at hour zero. return hours , threshold , frame_count return fix_gap_in_list ( hours ) , threshold , frame_count