<commit_before>
import pandas

from helpers import combine_data_frames

"""

Functions that act on data frame group by objects

"""

def filter_groups(dfgb, filter_function, on_index=False):
    """
    Filter the groups of a DataFrameGroupBy
    and return a DataFrameGroupBy containing
    only the groups that return true.
    In pseudocode:
    return [group for group in dfgb.groups
            if filter_function(group)]
    If on_index is true, the filter function is applied
    to the group value (the index)
    """

    return_groups = []

    for val, group in dfgb: #range(0, dfgb.ngroups):
        #group = dfgb.get_group(i)
        if on_index and filter_function(val):
            return_groups.append(group)
        if not on_index and filter_function(group):
            return_groups.append(group)

    return combine_data_frames(return_groups).groupby(dfgb.keys)


def sorted_groups(dfgb, key):
    """
    Return a sorted version of the
    grouped dataframe.  The required
    key function is a function of each
    group and determins the sort order
    """

    sort_list = []

    for name, group in dfgb:

        sort_val = key(group)
        sort_list.append((sort_val, group))

    sorted_groups = [group for val, group
                     in sorted(sort_list, key=lambda x: x[0])]

    return combine_data_frames(sorted_groups).groupby(dfgb.keys, sort=False)


def map_groups(grouped, func):
    """
    Take a DataFrameGroupBy and apply a function
    to the DataFrames, returning a seriesgroupby
    of the values
    """

    transformed = grouped.obj.apply(func, axis=1)
    #transformed = grouped.apply(func) #, axis=1)
    return transformed.groupby(grouped.grouper) #keys) #grouper)

    #return_groups = []

    #for name, group in grouped:


def take_groups(dfgb, n):<commit_msg>Fix map_groups<commit_after>
import pandas

from helpers import combine_data_frames

from types import *
import inspect

"""

Functions that act on data frame group by objects

"""

def filter_groups(dfgb, filter_function, on_index=False):
    """
    Filter the groups of a DataFrameGroupBy
    and return a DataFrameGroupBy containing
    only the groups that return true.
    In pseudocode:
    return [group for group in dfgb.groups
            if filter_function(group)]
    If on_index is true, the filter function is applied
    to the group value (the index)
    """

    return_groups = []

    for val, group in dfgb: #range(0, dfgb.ngroups):
        #group = dfgb.get_group(i)
        if on_index and filter_function(val):
            return_groups.append(group)
        if not on_index and filter_function(group):
            return_groups.append(group)

    return combine_data_frames(return_groups).groupby(dfgb.keys)


def sorted_groups(dfgb, key):
    """
    Return a sorted version of the
    grouped dataframe.  The required
    key function is a function of each
    group and determins the sort order
    """

    sort_list = []

    for name, group in dfgb:

        sort_val = key(group)
        sort_list.append((sort_val, group))

    sorted_groups = [group for val, group
                     in sorted(sort_list, key=lambda x: x[0])]

    return combine_data_frames(sorted_groups).groupby(dfgb.keys, sort=False)


def map_groups(grouped, func, name=None):
    """
    Take a DataFrameGroupBy and apply a function
    to the DataFrames, returning a seriesgroupby
    of the values
    """

    if name is None:
        name = func.__name__

    transformed = grouped.obj.apply(func, axis=1, reduce=False)
    return pandas.DataFrame({name: transformed}).groupby(grouped.grouper)


def take_groups(dfgb, n):
{'fraction_non_alphanumeric': 0.08326485894275541, 'fraction_numerical': 0.001917282936181868, 'mean_word_length': 2.986899563318777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6999574', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1095, 'n_words': 347}