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