Skip to content

Commit

Permalink
BUG: don't lose index names when adding row margin
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jan 16, 2012
1 parent 5f85f4b commit 9cd3e49
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion RELEASE.rst
Expand Up @@ -208,7 +208,8 @@ pandas 0.7.0
- Fix TypeError with tuple subclasses (e.g. namedtuple) in
DataFrame.from_records (GH #611)
- Catch misreported console size when running IPython within Emacs
- Fix minor bug in pivot table margins
- Fix minor bug in pivot table margins, loss of index names and length-1
'All' tuple in row labels

Thanks
------
Expand Down
3 changes: 3 additions & 0 deletions pandas/tools/pivot.py
Expand Up @@ -153,7 +153,10 @@ def _add_margins(table, data, values, rows=None, cols=None, aggfunc=np.mean):
row_margin[k] = grand_margin[k]

margin_dummy = DataFrame(row_margin, columns=[key]).T

row_names = result.index.names
result = result.append(margin_dummy)
result.index.names = row_names

return result

Expand Down
3 changes: 3 additions & 0 deletions pandas/tools/tests/test_pivot.py
Expand Up @@ -188,6 +188,9 @@ def test_crosstab_margins(self):
result = crosstab(a, [b, c], rownames=['a'], colnames=('b', 'c'),
margins=True)

self.assertEqual(result.index.names, ['a'])
self.assertEqual(result.columns.names, ['b', 'c'])

all_cols = result['All', '']
exp_cols = df.groupby(['a']).size()
exp_cols = exp_cols.append(Series([len(df)], index=['All']))
Expand Down

0 comments on commit 9cd3e49

Please sign in to comment.