@@ -171,6 +171,53 @@ def test_pivot_no_level_overlap(self):
171
171
expected = grouped .unstack ('b' ).unstack ('c' ).dropna (axis = 1 , how = 'all' )
172
172
tm .assert_frame_equal (table , expected )
173
173
174
+ def test_pivot_columns_lexsorted (self ):
175
+ import datetime
176
+ import numpy as np
177
+ import pandas
178
+
179
+ n = 10000
180
+
181
+ dtype = np .dtype ([
182
+ ("Index" , object ),
183
+ ("Symbol" , object ),
184
+ ("Year" , int ),
185
+ ("Month" , int ),
186
+ ("Day" , int ),
187
+ ("Quantity" , int ),
188
+ ("Price" , float ),
189
+ ])
190
+
191
+ products = np .array ([
192
+ ('SP500' , 'ADBE' ),
193
+ ('SP500' , 'NVDA' ),
194
+ ('SP500' , 'ORCL' ),
195
+ ('NDQ100' , 'AAPL' ),
196
+ ('NDQ100' , 'MSFT' ),
197
+ ('NDQ100' , 'GOOG' ),
198
+ ('FTSE' , 'DGE.L' ),
199
+ ('FTSE' , 'TSCO.L' ),
200
+ ('FTSE' , 'GSK.L' ),
201
+ ], dtype = [('Index' , object ), ('Symbol' , object )])
202
+ items = np .empty (n , dtype = dtype )
203
+ iproduct = np .random .randint (0 , len (products ), n )
204
+ items ['Index' ] = products ['Index' ][iproduct ]
205
+ items ['Symbol' ] = products ['Symbol' ][iproduct ]
206
+ dr = pandas .date_range (datetime .date (2000 , 1 , 1 ),
207
+ datetime .date (2010 , 12 , 31 ))
208
+ dates = dr [np .random .randint (0 , len (dr ), n )]
209
+ items ['Year' ] = dates .year
210
+ items ['Month' ] = dates .month
211
+ items ['Day' ] = dates .day
212
+ items ['Price' ] = np .random .lognormal (4.0 , 2.0 , n )
213
+
214
+ df = DataFrame (items )
215
+
216
+ pivoted = df .pivot_table ('Price' , rows = ['Month' , 'Day' ],
217
+ cols = ['Index' , 'Symbol' , 'Year' ],
218
+ aggfunc = 'mean' )
219
+
220
+ self .assert_ (pivoted .columns .is_monotonic )
174
221
175
222
class TestCrosstab (unittest .TestCase ):
176
223
0 commit comments