@@ -350,7 +350,7 @@ def _check_output(result, values_col, index=['A', 'B'],
350
350
# no rows
351
351
rtable = self .data .pivot_table (columns = ['AA' , 'BB' ], margins = True ,
352
352
aggfunc = np .mean )
353
- tm .assertIsInstance (rtable , Series )
353
+ tm .assertIsInstance (rtable , DataFrame )
354
354
355
355
table = self .data .pivot_table (index = ['AA' , 'BB' ], margins = True ,
356
356
aggfunc = 'mean' )
@@ -485,7 +485,7 @@ def test_margins_no_values_no_cols(self):
485
485
# Regression test on pivot table: no values or cols passed.
486
486
result = self .data [['A' , 'B' ]].pivot_table (
487
487
index = ['A' , 'B' ], aggfunc = len , margins = True )
488
- result_list = result .tolist ()
488
+ result_list = result .values . flatten (). tolist ()
489
489
self .assertEqual (sum (result_list [:- 1 ]), result_list [- 1 ])
490
490
491
491
def test_margins_no_values_two_rows (self ):
@@ -854,6 +854,39 @@ def test_categorical_margins(self):
854
854
table = data .pivot_table ('x' , 'y' , 'z' , margins = True )
855
855
tm .assert_frame_equal (table , expected )
856
856
857
+ def test_always_return_dataframe (self ):
858
+ # GH 4386
859
+ df = DataFrame ({'col1' : [3 , 4 , 5 ],
860
+ 'col2' : ['C' , 'D' , 'E' ],
861
+ 'col3' : [1 , 3 , 9 ]})
862
+ result = df .pivot_table ('col1' , index = ['col3' , 'col2' ], aggfunc = np .sum )
863
+ m = MultiIndex .from_arrays ([[1 , 3 , 9 ],
864
+ ['C' , 'D' , 'E' ]],
865
+ names = ['col3' , 'col2' ])
866
+ expected = DataFrame ([3 , 4 , 5 ],
867
+ index = m , columns = ['col1' ])
868
+
869
+ tm .assert_frame_equal (result , expected )
870
+
871
+ result = df .pivot_table (
872
+ 'col1' , index = 'col3' , columns = 'col2' , aggfunc = np .sum
873
+ )
874
+ expected = DataFrame ([[3 , np .NaN , np .NaN ],
875
+ [np .NaN , 4 , np .NaN ],
876
+ [np .NaN , np .NaN , 5 ]],
877
+ index = Index ([1 , 3 , 9 ], name = 'col3' ),
878
+ columns = Index (['C' , 'D' , 'E' ], name = 'col2' ))
879
+
880
+ tm .assert_frame_equal (result , expected )
881
+
882
+ result = df .pivot_table ('col1' , index = 'col3' , aggfunc = [np .sum ])
883
+ m = MultiIndex .from_arrays ([['sum' ],
884
+ ['col1' ]])
885
+ expected = DataFrame ([3 , 4 , 5 ],
886
+ index = Index ([1 , 3 , 9 ], name = 'col3' ),
887
+ columns = m )
888
+ tm .assert_frame_equal (result , expected )
889
+
857
890
858
891
class TestCrosstab (tm .TestCase ):
859
892
0 commit comments