Ejemplo n.º 1
0
 def agg(self, agg_types):
     df = DataFrame()
     by = []
     if self.level is not None:
         if isinstance(self.source_series.index, MultiIndex):
             # Add index columns specified by multiindex into _df
             # Record the index column names for the groupby
             for col in self.source_series.index.codes:
                 df[self.group_name + col] = self.source_series.index.codes[
                         col]
                 by.append(self.group_name + col)
     else:
         if isinstance(self.group_keys, Series):
             df[self.group_name] = self.group_keys
             by = self.group_name
         else:
             df = self.group_keys
             by = self._by
     df[self.source_name] = self.source_series
     groupby = df.groupby(by).agg(agg_types)
     idx = groupby.index
     if len(groupby.columns) == 1:
         result = groupby[self.source_name]
         result.name = self.source_series.name
         idx.name = None
         result = result.set_index(idx)
     else:
         idx.name = self.group_name
         result = groupby.set_index(idx)
     if len(result) == 0 and self._by is not None:
         empties = [[] for x in range(len(self._by))]
         mi = MultiIndex(empties, empties, names=self._by)
         result = result.set_index(mi)
     return result
Ejemplo n.º 2
0
    def __getattr__(self, attr):
        df = DataFrame()
        df[self.source_name] = self.source_series
        by = []
        if self.level is not None:
            if isinstance(self.source_series.index, MultiIndex):
                # Add index columns specified by multiindex into _df
                # Record the index column names for the groupby
                for col in self.source_series.index.codes:
                    df[self.group_name +
                       col] = self.source_series.index.codes[col]
                    by.append(self.group_name + col)
        else:
            df[self.group_name] = self.group_series
            by = self.group_name
        groupby = df.groupby(by, level=self.level, sort=self.sort)
        result_df = getattr(groupby, attr)()

        def get_result():
            result_series = result_df[self.source_name]
            result_series.name = self.source_name if self.source_name !=\
                '_x' else None
            idx = result_df.index
            idx.name = self.group_name if self.group_name != '_y' else None
            result_series.set_index(idx)
            return result_series

        return get_result
Ejemplo n.º 3
0
 def agg(self, agg_types):
     df = DataFrame()
     df['x'] = self.source_series
     if self.level is not None:
         df['y'] = self.source_series.index
     else:
         df['y'] = self.group_series
     groupby = df.groupby('y').agg(agg_types)
     idx = groupby.index
     idx.name = None
     groupby.set_index(idx)
     return groupby
Ejemplo n.º 4
0
    def __getattr__(self, attr):
        df = DataFrame()
        df['x'] = self.source_series
        if self.level is not None:
            df['y'] = self.source_series.index
        else:
            df['y'] = self.group_series
        groupby = df.groupby('y', level=self.level, sort=self.sort)
        result_df = getattr(groupby, attr)()

        def get_result():
            result_series = result_df['x']
            result_series.name = None
            idx = result_df.index
            idx.name = None
            result_series.set_index(idx)
            return result_series
        return get_result
Ejemplo n.º 5
0
    def __getattr__(self, attr):
        df = DataFrame()
        by = []
        if self.level is not None:
            if isinstance(self.source_series.index, MultiIndex):
                # Add index columns specified by multiindex into _df
                # Record the index column names for the groupby
                for col in self.source_series.index.codes:
                    df[self.group_name + col] = self.source_series.index.codes[
                            col]
                    by.append(self.group_name + col)
        else:
            if isinstance(self.group_keys, Series):
                df[self.group_name] = self.group_keys
                by = self.group_name
            else:
                df = self.group_keys
                by = self._by
        df[self.source_name] = self.source_series
        groupby = df.groupby(by,
                             level=self.level,
                             sort=self.sort)
        if attr in ['sum', 'min', 'max', 'mean', 'count', 'agg']:
            result_df = getattr(groupby, attr)()
        else:
            return getattr(groupby, attr)

        def get_result():
            result_series = result_df[self.source_name]
            result_series.name = self.source_name if self.source_name !=\
                _LEVEL_0_DATA_NAME else None
            if len(result_df) == 0 and self._by is not None:
                empties = [[] for x in range(len(self._by))]
                mi = MultiIndex(empties, empties, names=self._by)
                result_series = result_series.set_index(mi)
            else:
                idx = result_df.index
                if self.group_name == _LEVEL_0_INDEX_NAME:
                    idx.name = None
                result_series = result_series.set_index(idx)
            return result_series
        return get_result