Skip to content

Commit 3b8ad3e

Browse files
committed
BUG: handling of grand margin when no columns passed, GH #114
1 parent e874b4c commit 3b8ad3e

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pandas 0.6.1
5656
columns (GH #302)
5757
- Add rolling_corr_pairwise function for computing Panel of correlation
5858
matrices (GH #189)
59+
- Add `margins` option to `pivot_table` for computing subgroup aggregates (GH
60+
#114)
5961

6062
**Improvements to existing features**
6163

pandas/tools/pivot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def _add_margins(table, data, values, rows=None, cols=None, aggfunc=np.mean):
139139
row_margin = row_margin.reindex(result.columns)
140140
# populate grand margin
141141
for k in margin_keys:
142-
row_margin[k] = grand_margin[k[0]]
142+
if len(cols) > 0:
143+
row_margin[k] = grand_margin[k[0]]
144+
else:
145+
row_margin[k] = grand_margin[k]
143146

144147
margin_dummy = DataFrame(row_margin, columns=[key]).T
145148
result = result.append(margin_dummy)

pandas/tools/tests/test_pivot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ def _check_output(res, col, rows=['A', 'B'], cols=['C']):
8686
_check_output(table[valcol], valcol)
8787

8888
# no col
89-
table = self.data.pivot_table(rows=['A', 'B'], margins=True,
89+
90+
# to help with a buglet
91+
self.data.columns = [k * 2 for k in self.data.columns]
92+
table = self.data.pivot_table(rows=['AA', 'BB'], margins=True,
9093
aggfunc=np.mean)
9194
for valcol in table.columns:
9295
gmarg = table[valcol]['All', '']

0 commit comments

Comments
 (0)