|
| 1 | +""" |
| 2 | +Provide basic components for groupby. These defintiions |
| 3 | +hold the whitelist of methods that are exposed on the |
| 4 | +SeriesGroupBy and the DataFrameGroupBy objects. |
| 5 | +""" |
| 6 | + |
| 7 | +import types |
| 8 | +from pandas.util._decorators import make_signature |
| 9 | +from pandas.core.dtypes.common import is_scalar, is_list_like |
| 10 | + |
| 11 | + |
| 12 | +class GroupByMixin(object): |
| 13 | + """ provide the groupby facilities to the mixed object """ |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def _dispatch(name, *args, **kwargs): |
| 17 | + """ dispatch to apply """ |
| 18 | + |
| 19 | + def outer(self, *args, **kwargs): |
| 20 | + def f(x): |
| 21 | + x = self._shallow_copy(x, groupby=self._groupby) |
| 22 | + return getattr(x, name)(*args, **kwargs) |
| 23 | + return self._groupby.apply(f) |
| 24 | + outer.__name__ = name |
| 25 | + return outer |
| 26 | + |
| 27 | + def _gotitem(self, key, ndim, subset=None): |
| 28 | + """ |
| 29 | + sub-classes to define |
| 30 | + return a sliced object |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + key : string / list of selections |
| 35 | + ndim : 1,2 |
| 36 | + requested ndim of result |
| 37 | + subset : object, default None |
| 38 | + subset to act on |
| 39 | + """ |
| 40 | + # create a new object to prevent aliasing |
| 41 | + if subset is None: |
| 42 | + subset = self.obj |
| 43 | + |
| 44 | + # we need to make a shallow copy of ourselves |
| 45 | + # with the same groupby |
| 46 | + kwargs = dict([(attr, getattr(self, attr)) |
| 47 | + for attr in self._attributes]) |
| 48 | + self = self.__class__(subset, |
| 49 | + groupby=self._groupby[key], |
| 50 | + parent=self, |
| 51 | + **kwargs) |
| 52 | + self._reset_cache() |
| 53 | + if subset.ndim == 2: |
| 54 | + if is_scalar(key) and key in subset or is_list_like(key): |
| 55 | + self._selection = key |
| 56 | + return self |
| 57 | + |
| 58 | + |
| 59 | +# special case to prevent duplicate plots when catching exceptions when |
| 60 | +# forwarding methods from NDFrames |
| 61 | +plotting_methods = frozenset(['plot', 'boxplot', 'hist']) |
| 62 | + |
| 63 | +common_apply_whitelist = frozenset([ |
| 64 | + 'last', 'first', |
| 65 | + 'head', 'tail', 'median', |
| 66 | + 'mean', 'sum', 'min', 'max', |
| 67 | + 'cumcount', 'ngroup', |
| 68 | + 'resample', |
| 69 | + 'rank', 'quantile', |
| 70 | + 'fillna', |
| 71 | + 'mad', |
| 72 | + 'any', 'all', |
| 73 | + 'take', |
| 74 | + 'idxmax', 'idxmin', |
| 75 | + 'shift', 'tshift', |
| 76 | + 'ffill', 'bfill', |
| 77 | + 'pct_change', 'skew', |
| 78 | + 'corr', 'cov', 'diff', |
| 79 | +]) | plotting_methods |
| 80 | + |
| 81 | +series_apply_whitelist = ((common_apply_whitelist | |
| 82 | + {'nlargest', 'nsmallest', |
| 83 | + 'is_monotonic_increasing', |
| 84 | + 'is_monotonic_decreasing'}) - |
| 85 | + {'boxplot'}) | frozenset(['dtype', 'unique']) |
| 86 | + |
| 87 | +dataframe_apply_whitelist = ((common_apply_whitelist | |
| 88 | + frozenset(['dtypes', 'corrwith'])) - |
| 89 | + {'boxplot'}) |
| 90 | + |
| 91 | +cython_transforms = frozenset(['cumprod', 'cumsum', 'shift', |
| 92 | + 'cummin', 'cummax']) |
| 93 | + |
| 94 | +cython_cast_blacklist = frozenset(['rank', 'count', 'size']) |
| 95 | + |
| 96 | + |
| 97 | +def whitelist_method_generator(base, klass, whitelist): |
| 98 | + """ |
| 99 | + Yields all GroupBy member defs for DataFrame/Series names in whitelist. |
| 100 | +
|
| 101 | + Parameters |
| 102 | + ---------- |
| 103 | + base : class |
| 104 | + base class |
| 105 | + klass : class |
| 106 | + class where members are defined. |
| 107 | + Should be Series or DataFrame |
| 108 | + whitelist : list |
| 109 | + list of names of klass methods to be constructed |
| 110 | +
|
| 111 | + Returns |
| 112 | + ------- |
| 113 | + The generator yields a sequence of strings, each suitable for exec'ing, |
| 114 | + that define implementations of the named methods for DataFrameGroupBy |
| 115 | + or SeriesGroupBy. |
| 116 | +
|
| 117 | + Since we don't want to override methods explicitly defined in the |
| 118 | + base class, any such name is skipped. |
| 119 | + """ |
| 120 | + |
| 121 | + method_wrapper_template = \ |
| 122 | + """def %(name)s(%(sig)s) : |
| 123 | + \""" |
| 124 | + %(doc)s |
| 125 | + \""" |
| 126 | + f = %(self)s.__getattr__('%(name)s') |
| 127 | + return f(%(args)s)""" |
| 128 | + property_wrapper_template = \ |
| 129 | + """@property |
| 130 | +def %(name)s(self) : |
| 131 | + \""" |
| 132 | + %(doc)s |
| 133 | + \""" |
| 134 | + return self.__getattr__('%(name)s')""" |
| 135 | + |
| 136 | + for name in whitelist: |
| 137 | + # don't override anything that was explicitly defined |
| 138 | + # in the base class |
| 139 | + if hasattr(base, name): |
| 140 | + continue |
| 141 | + # ugly, but we need the name string itself in the method. |
| 142 | + f = getattr(klass, name) |
| 143 | + doc = f.__doc__ |
| 144 | + doc = doc if type(doc) == str else '' |
| 145 | + if isinstance(f, types.MethodType): |
| 146 | + wrapper_template = method_wrapper_template |
| 147 | + decl, args = make_signature(f) |
| 148 | + # pass args by name to f because otherwise |
| 149 | + # GroupBy._make_wrapper won't know whether |
| 150 | + # we passed in an axis parameter. |
| 151 | + args_by_name = ['{0}={0}'.format(arg) for arg in args[1:]] |
| 152 | + params = {'name': name, |
| 153 | + 'doc': doc, |
| 154 | + 'sig': ','.join(decl), |
| 155 | + 'self': args[0], |
| 156 | + 'args': ','.join(args_by_name)} |
| 157 | + else: |
| 158 | + wrapper_template = property_wrapper_template |
| 159 | + params = {'name': name, 'doc': doc} |
| 160 | + yield wrapper_template % params |
0 commit comments