Skip to content

Commit 9cd3e49

Browse files
committed
BUG: don't lose index names when adding row margin
1 parent 5f85f4b commit 9cd3e49

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

RELEASE.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ pandas 0.7.0
208208
- Fix TypeError with tuple subclasses (e.g. namedtuple) in
209209
DataFrame.from_records (GH #611)
210210
- Catch misreported console size when running IPython within Emacs
211-
- Fix minor bug in pivot table margins
211+
- Fix minor bug in pivot table margins, loss of index names and length-1
212+
'All' tuple in row labels
212213

213214
Thanks
214215
------

pandas/tools/pivot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def _add_margins(table, data, values, rows=None, cols=None, aggfunc=np.mean):
153153
row_margin[k] = grand_margin[k]
154154

155155
margin_dummy = DataFrame(row_margin, columns=[key]).T
156+
157+
row_names = result.index.names
156158
result = result.append(margin_dummy)
159+
result.index.names = row_names
157160

158161
return result
159162

pandas/tools/tests/test_pivot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def test_crosstab_margins(self):
188188
result = crosstab(a, [b, c], rownames=['a'], colnames=('b', 'c'),
189189
margins=True)
190190

191+
self.assertEqual(result.index.names, ['a'])
192+
self.assertEqual(result.columns.names, ['b', 'c'])
193+
191194
all_cols = result['All', '']
192195
exp_cols = df.groupby(['a']).size()
193196
exp_cols = exp_cols.append(Series([len(df)], index=['All']))

0 commit comments

Comments
 (0)