Skip to content

Commit 35bb1a1

Browse files
cmazzullojreback
authored andcommitted
BUG: df.pivot_table: margins_name ignored when aggfunc is a list
closes #13354 Author: cmazzullo <[email protected]> Closes #13435 from cmazzullo/pivot_table and squashes the following commits: 6017e25 [cmazzullo] Fixed up whitespace for pep8 b4b09bf [cmazzullo] Compared whole dataframes in test_pivot_table_margins_name_with_aggfunc_list 98ab8be [cmazzullo] Added doublequotes to variable names 669931f [cmazzullo] Added unit test for pivot_table margins_name when aggfunc is a list 0dcb78f [cmazzullo] Switched documentation to v0.18.2 d6a6036 [cmazzullo] BUG: df.pivot_table: margins_name is ignored when there aggfunc is list #13354
1 parent e24ab24 commit 35bb1a1

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ Bug Fixes
497497
- Bug in ``pd.read_csv()`` that prevents ``usecols`` from being an empty set (:issue:`13402`)
498498
- Bug in ``pd.read_csv()`` with ``engine=='c'`` in which null ``quotechar`` was not accepted even though ``quoting`` was specified as ``None`` (:issue:`13411`)
499499
- Bug in ``pd.read_csv()`` with ``engine=='c'`` in which fields were not properly cast to float when quoting was specified as non-numeric (:issue:`13411`)
500+
- Bug in ``pd.pivot_table()`` where ``margins_name`` is ignored when ``aggfunc`` is a list (:issue:`13354`)
500501

501502

502503

pandas/tools/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
8686
table = pivot_table(data, values=values, index=index,
8787
columns=columns,
8888
fill_value=fill_value, aggfunc=func,
89-
margins=margins)
89+
margins=margins, margins_name=margins_name)
9090
pieces.append(table)
9191
keys.append(func.__name__)
9292
return concat(pieces, keys=keys, axis=1)

pandas/tools/tests/test_pivot.py

+22
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,28 @@ def test_pivot_table_with_iterator_values(self):
779779
)
780780
tm.assert_frame_equal(pivot_values_gen, pivot_values_list)
781781

782+
def test_pivot_table_margins_name_with_aggfunc_list(self):
783+
# GH 13354
784+
margins_name = 'Weekly'
785+
costs = pd.DataFrame(
786+
{'item': ['bacon', 'cheese', 'bacon', 'cheese'],
787+
'cost': [2.5, 4.5, 3.2, 3.3],
788+
'day': ['M', 'M', 'T', 'T']}
789+
)
790+
table = costs.pivot_table(
791+
index="item", columns="day", margins=True,
792+
margins_name=margins_name, aggfunc=[np.mean, max]
793+
)
794+
ix = pd.Index(
795+
['bacon', 'cheese', margins_name], dtype='object', name='item'
796+
)
797+
tups = [('mean', 'cost', 'M'), ('mean', 'cost', 'T'),
798+
('mean', 'cost', margins_name), ('max', 'cost', 'M'),
799+
('max', 'cost', 'T'), ('max', 'cost', margins_name)]
800+
cols = pd.MultiIndex.from_tuples(tups, names=[None, None, 'day'])
801+
expected = pd.DataFrame(table.values, index=ix, columns=cols)
802+
tm.assert_frame_equal(table, expected)
803+
782804

783805
class TestCrosstab(tm.TestCase):
784806

0 commit comments

Comments
 (0)