@@ -964,6 +964,86 @@ def test_stack_unstack_multiple(self):
964
964
expected = self .ymd .unstack (2 ).unstack (1 ).dropna (axis = 1 , how = 'all' )
965
965
assert_frame_equal (unstacked , expected .ix [:, unstacked .columns ])
966
966
967
+ def test_unstack_period_series (self ):
968
+ # GH 4342
969
+ idx1 = pd .PeriodIndex (['2013-01' , '2013-01' , '2013-02' , '2013-02' ,
970
+ '2013-03' , '2013-03' ], freq = 'M' , name = 'period' )
971
+ idx2 = Index (['A' , 'B' ] * 3 , name = 'str' )
972
+ value = [1 , 2 , 3 , 4 , 5 , 6 ]
973
+
974
+ idx = MultiIndex .from_arrays ([idx1 , idx2 ])
975
+ s = Series (value , index = idx )
976
+
977
+ result1 = s .unstack ()
978
+ result2 = s .unstack (level = 1 )
979
+ result3 = s .unstack (level = 0 )
980
+
981
+ e_idx = pd .PeriodIndex (['2013-01' , '2013-02' , '2013-03' ], freq = 'M' , name = 'period' )
982
+ expected = DataFrame ({'A' : [1 , 3 , 5 ], 'B' : [2 , 4 , 6 ]}, index = e_idx ,
983
+ columns = ['A' , 'B' ])
984
+ expected .columns .name = 'str'
985
+
986
+ assert_frame_equal (result1 , expected )
987
+ assert_frame_equal (result2 , expected )
988
+ assert_frame_equal (result3 , expected .T )
989
+
990
+ idx1 = pd .PeriodIndex (['2013-01' , '2013-01' , '2013-02' , '2013-02' ,
991
+ '2013-03' , '2013-03' ], freq = 'M' , name = 'period1' )
992
+
993
+ idx2 = pd .PeriodIndex (['2013-12' , '2013-11' , '2013-10' , '2013-09' ,
994
+ '2013-08' , '2013-07' ], freq = 'M' , name = 'period2' )
995
+ idx = pd .MultiIndex .from_arrays ([idx1 , idx2 ])
996
+ s = Series (value , index = idx )
997
+
998
+ result1 = s .unstack ()
999
+ result2 = s .unstack (level = 1 )
1000
+ result3 = s .unstack (level = 0 )
1001
+
1002
+ e_idx = pd .PeriodIndex (['2013-01' , '2013-02' , '2013-03' ], freq = 'M' , name = 'period1' )
1003
+ e_cols = pd .PeriodIndex (['2013-07' , '2013-08' , '2013-09' , '2013-10' ,
1004
+ '2013-11' , '2013-12' ], freq = 'M' , name = 'period2' )
1005
+ expected = DataFrame ([[np .nan , np .nan , np .nan , np .nan , 2 , 1 ],
1006
+ [np .nan , np .nan , 4 , 3 , np .nan , np .nan ],
1007
+ [6 , 5 , np .nan , np .nan , np .nan , np .nan ]],
1008
+ index = e_idx , columns = e_cols )
1009
+
1010
+ assert_frame_equal (result1 , expected )
1011
+ assert_frame_equal (result2 , expected )
1012
+ assert_frame_equal (result3 , expected .T )
1013
+
1014
+ def test_unstack_period_frame (self ):
1015
+ # GH 4342
1016
+ idx1 = pd .PeriodIndex (['2014-01' , '2014-02' , '2014-02' , '2014-02' , '2014-01' , '2014-01' ],
1017
+ freq = 'M' , name = 'period1' )
1018
+ idx2 = pd .PeriodIndex (['2013-12' , '2013-12' , '2014-02' , '2013-10' , '2013-10' , '2014-02' ],
1019
+ freq = 'M' , name = 'period2' )
1020
+ value = {'A' : [1 , 2 , 3 , 4 , 5 , 6 ], 'B' : [6 , 5 , 4 , 3 , 2 , 1 ]}
1021
+ idx = pd .MultiIndex .from_arrays ([idx1 , idx2 ])
1022
+ df = pd .DataFrame (value , index = idx )
1023
+
1024
+ result1 = df .unstack ()
1025
+ result2 = df .unstack (level = 1 )
1026
+ result3 = df .unstack (level = 0 )
1027
+
1028
+ e_1 = pd .PeriodIndex (['2014-01' , '2014-02' ], freq = 'M' , name = 'period1' )
1029
+ e_2 = pd .PeriodIndex (['2013-10' , '2013-12' , '2014-02' , '2013-10' ,
1030
+ '2013-12' , '2014-02' ], freq = 'M' , name = 'period2' )
1031
+ e_cols = pd .MultiIndex .from_arrays (['A A A B B B' .split (), e_2 ])
1032
+ expected = DataFrame ([[5 , 1 , 6 , 2 , 6 , 1 ], [4 , 2 , 3 , 3 , 5 , 4 ]],
1033
+ index = e_1 , columns = e_cols )
1034
+
1035
+ assert_frame_equal (result1 , expected )
1036
+ assert_frame_equal (result2 , expected )
1037
+
1038
+ e_1 = pd .PeriodIndex (['2014-01' , '2014-02' , '2014-01' ,
1039
+ '2014-02' ], freq = 'M' , name = 'period1' )
1040
+ e_2 = pd .PeriodIndex (['2013-10' , '2013-12' , '2014-02' ], freq = 'M' , name = 'period2' )
1041
+ e_cols = pd .MultiIndex .from_arrays (['A A B B' .split (), e_1 ])
1042
+ expected = DataFrame ([[5 , 4 , 2 , 3 ], [1 , 2 , 6 , 5 ], [6 , 3 , 1 , 4 ]],
1043
+ index = e_2 , columns = e_cols )
1044
+
1045
+ assert_frame_equal (result3 , expected )
1046
+
967
1047
def test_stack_multiple_bug (self ):
968
1048
""" bug when some uniques are not present in the data #3170"""
969
1049
id_col = ([1 ] * 3 ) + ([2 ] * 3 )
0 commit comments