@@ -2365,6 +2365,28 @@ def test_fails_on_no_datetime_index(self):
2365
2365
"got an instance of 'PeriodIndex'" ):
2366
2366
df .groupby (TimeGrouper ('D' ))
2367
2367
2368
+ def test_aaa_group_order (self ):
2369
+ # GH 12840
2370
+ # check TimeGrouper perform stable sorts
2371
+ n = 20
2372
+ data = np .random .randn (n , 4 )
2373
+ df = DataFrame (data , columns = ['A' , 'B' , 'C' , 'D' ])
2374
+ df ['key' ] = [datetime (2013 , 1 , 1 ), datetime (2013 , 1 , 2 ),
2375
+ datetime (2013 , 1 , 3 ), datetime (2013 , 1 , 4 ),
2376
+ datetime (2013 , 1 , 5 )] * 4
2377
+ grouped = df .groupby (TimeGrouper (key = 'key' , freq = 'D' ))
2378
+
2379
+ tm .assert_frame_equal (grouped .get_group (datetime (2013 , 1 , 1 )),
2380
+ df [::5 ])
2381
+ tm .assert_frame_equal (grouped .get_group (datetime (2013 , 1 , 2 )),
2382
+ df [1 ::5 ])
2383
+ tm .assert_frame_equal (grouped .get_group (datetime (2013 , 1 , 3 )),
2384
+ df [2 ::5 ])
2385
+ tm .assert_frame_equal (grouped .get_group (datetime (2013 , 1 , 4 )),
2386
+ df [3 ::5 ])
2387
+ tm .assert_frame_equal (grouped .get_group (datetime (2013 , 1 , 5 )),
2388
+ df [4 ::5 ])
2389
+
2368
2390
def test_aggregate_normal (self ):
2369
2391
# check TimeGrouper's aggregation is identical as normal groupby
2370
2392
@@ -2402,23 +2424,25 @@ def test_aggregate_normal(self):
2402
2424
periods = 5 , name = 'key' )
2403
2425
dt_result = getattr (dt_grouped , func )()
2404
2426
assert_series_equal (expected , dt_result )
2405
- """
2427
+
2428
+ # GH 7453
2406
2429
for func in ['first' , 'last' ]:
2407
2430
expected = getattr (normal_grouped , func )()
2408
2431
expected .index = date_range (start = '2013-01-01' , freq = 'D' ,
2409
2432
periods = 5 , name = 'key' )
2410
2433
dt_result = getattr (dt_grouped , func )()
2411
2434
assert_frame_equal (expected , dt_result )
2412
2435
2436
+ # if TimeGrouper is used included, 'nth' doesn't work yet
2437
+
2438
+ """
2413
2439
for func in ['nth']:
2414
2440
expected = getattr(normal_grouped, func)(3)
2415
2441
expected.index = date_range(start='2013-01-01',
2416
2442
freq='D', periods=5, name='key')
2417
2443
dt_result = getattr(dt_grouped, func)(3)
2418
2444
assert_frame_equal(expected, dt_result)
2419
2445
"""
2420
- # if TimeGrouper is used included, 'first','last' and 'nth' doesn't
2421
- # work yet
2422
2446
2423
2447
def test_aggregate_with_nat (self ):
2424
2448
# check TimeGrouper's aggregation is identical as normal groupby
0 commit comments