|
2 | 2 |
|
3 | 3 | from pandas import Series, DataFrame
|
4 | 4 | from pandas.core.index import MultiIndex
|
5 |
| -from pandas.core.reshape import _unstack_multiple |
6 | 5 | from pandas.tools.merge import concat
|
7 | 6 | from pandas.tools.util import cartesian_product
|
8 |
| -from pandas.compat import range, lrange, zip |
9 |
| -from pandas import compat |
10 | 7 | import pandas.core.common as com
|
11 | 8 | import numpy as np
|
12 | 9 |
|
@@ -149,17 +146,64 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc='mean',
|
149 | 146 | DataFrame.pivot_table = pivot_table
|
150 | 147 |
|
151 | 148 |
|
152 |
| -def _add_margins(table, data, values, rows=None, cols=None, aggfunc=np.mean): |
153 |
| - grand_margin = {} |
154 |
| - for k, v in compat.iteritems(data[values]): |
155 |
| - try: |
156 |
| - if isinstance(aggfunc, compat.string_types): |
157 |
| - grand_margin[k] = getattr(v, aggfunc)() |
158 |
| - else: |
159 |
| - grand_margin[k] = aggfunc(v) |
160 |
| - except TypeError: |
161 |
| - pass |
| 149 | +def _add_margins(table, data, values, rows, cols, aggfunc): |
| 150 | + |
| 151 | + grand_margin = _compute_grand_margin(data, values, aggfunc) |
| 152 | + |
| 153 | + if not values and isinstance(table, Series): |
| 154 | + # If there are no values and the table is a series, then there is only |
| 155 | + # one column in the data. Compute grand margin and return it. |
| 156 | + row_key = ('All',) + ('',) * (len(rows) - 1) if len(rows) > 1 else 'All' |
| 157 | + return table.append(Series({row_key: grand_margin['All']})) |
| 158 | + |
| 159 | + if values: |
| 160 | + marginal_result_set = _generate_marginal_results(table, data, values, rows, cols, aggfunc, grand_margin) |
| 161 | + if not isinstance(marginal_result_set, tuple): |
| 162 | + return marginal_result_set |
| 163 | + result, margin_keys, row_margin = marginal_result_set |
| 164 | + else: |
| 165 | + marginal_result_set = _generate_marginal_results_without_values(table, data, rows, cols, aggfunc) |
| 166 | + if not isinstance(marginal_result_set, tuple): |
| 167 | + return marginal_result_set |
| 168 | + result, margin_keys, row_margin = marginal_result_set |
| 169 | + |
| 170 | + key = ('All',) + ('',) * (len(rows) - 1) if len(rows) > 1 else 'All' |
| 171 | + |
| 172 | + row_margin = row_margin.reindex(result.columns) |
| 173 | + # populate grand margin |
| 174 | + for k in margin_keys: |
| 175 | + if isinstance(k, basestring): |
| 176 | + row_margin[k] = grand_margin[k] |
| 177 | + else: |
| 178 | + row_margin[k] = grand_margin[k[0]] |
162 | 179 |
|
| 180 | + margin_dummy = DataFrame(row_margin, columns=[key]).T |
| 181 | + |
| 182 | + row_names = result.index.names |
| 183 | + result = result.append(margin_dummy) |
| 184 | + result.index.names = row_names |
| 185 | + |
| 186 | + return result |
| 187 | + |
| 188 | + |
| 189 | +def _compute_grand_margin(data, values, aggfunc): |
| 190 | + |
| 191 | + if values: |
| 192 | + grand_margin = {} |
| 193 | + for k, v in data[values].iteritems(): |
| 194 | + try: |
| 195 | + if isinstance(aggfunc, basestring): |
| 196 | + grand_margin[k] = getattr(v, aggfunc)() |
| 197 | + else: |
| 198 | + grand_margin[k] = aggfunc(v) |
| 199 | + except TypeError: |
| 200 | + pass |
| 201 | + return grand_margin |
| 202 | + else: |
| 203 | + return {'All': aggfunc(data.index)} |
| 204 | + |
| 205 | + |
| 206 | +def _generate_marginal_results(table, data, values, rows, cols, aggfunc, grand_margin): |
163 | 207 | if len(cols) > 0:
|
164 | 208 | # need to "interleave" the margins
|
165 | 209 | table_pieces = []
|
@@ -198,28 +242,48 @@ def _all_key(key):
|
198 | 242 | row_margin = row_margin.stack()
|
199 | 243 |
|
200 | 244 | # slight hack
|
201 |
| - new_order = [len(cols)] + lrange(len(cols)) |
| 245 | + new_order = [len(cols)] + range(len(cols)) |
202 | 246 | row_margin.index = row_margin.index.reorder_levels(new_order)
|
203 | 247 | else:
|
204 | 248 | row_margin = Series(np.nan, index=result.columns)
|
205 | 249 |
|
206 |
| - key = ('All',) + ('',) * (len(rows) - 1) if len(rows) > 1 else 'All' |
| 250 | + return result, margin_keys, row_margin |
207 | 251 |
|
208 |
| - row_margin = row_margin.reindex(result.columns) |
209 |
| - # populate grand margin |
210 |
| - for k in margin_keys: |
211 |
| - if len(cols) > 0: |
212 |
| - row_margin[k] = grand_margin[k[0]] |
213 |
| - else: |
214 |
| - row_margin[k] = grand_margin[k] |
215 | 252 |
|
216 |
| - margin_dummy = DataFrame(row_margin, columns=[key]).T |
| 253 | +def _generate_marginal_results_without_values(table, data, rows, cols, aggfunc): |
| 254 | + if len(cols) > 0: |
| 255 | + # need to "interleave" the margins |
| 256 | + margin_keys = [] |
217 | 257 |
|
218 |
| - row_names = result.index.names |
219 |
| - result = result.append(margin_dummy) |
220 |
| - result.index.names = row_names |
| 258 | + def _all_key(): |
| 259 | + if len(cols) == 1: |
| 260 | + return 'All' |
| 261 | + return ('All', ) + ('', ) * (len(cols) - 1) |
221 | 262 |
|
222 |
| - return result |
| 263 | + if len(rows) > 0: |
| 264 | + margin = data[rows].groupby(rows).apply(aggfunc) |
| 265 | + all_key = _all_key() |
| 266 | + table[all_key] = margin |
| 267 | + result = table |
| 268 | + margin_keys.append(all_key) |
| 269 | + |
| 270 | + else: |
| 271 | + margin = data.groupby(level=0, axis=0).apply(aggfunc) |
| 272 | + all_key = _all_key() |
| 273 | + table[all_key] = margin |
| 274 | + result = table |
| 275 | + margin_keys.append(all_key) |
| 276 | + return result |
| 277 | + else: |
| 278 | + result = table |
| 279 | + margin_keys = table.columns |
| 280 | + |
| 281 | + if len(cols): |
| 282 | + row_margin = data[cols].groupby(cols).apply(aggfunc) |
| 283 | + else: |
| 284 | + row_margin = Series(np.nan, index=result.columns) |
| 285 | + |
| 286 | + return result, margin_keys, row_margin |
223 | 287 |
|
224 | 288 |
|
225 | 289 | def _convert_by(by):
|
|
0 commit comments