@@ -374,6 +374,31 @@ def test_truncate_copy(self):
374
374
truncated .values [:] = 5.
375
375
assert not (self .tsframe .values [5 :11 ] == 5 ).any ()
376
376
377
+ def test_truncate_nonsortedindex (self ):
378
+ # GH 17935
379
+
380
+ df = pd .DataFrame ({'A' : ['a' , 'b' , 'c' , 'd' , 'e' ]},
381
+ index = [5 , 3 , 2 , 9 , 0 ])
382
+ with tm .assert_raises_regex (ValueError ,
383
+ 'truncate requires a sorted index' ):
384
+ df .truncate (before = 3 , after = 9 )
385
+
386
+ rng = pd .date_range ('2011-01-01' , '2012-01-01' , freq = 'W' )
387
+ ts = pd .Series (np .random .randn (len (rng )), index = rng )
388
+ with tm .assert_raises_regex (ValueError ,
389
+ 'truncate requires a sorted index' ):
390
+ ts .sort_values (ascending = False ).truncate (before = '2011-11' ,
391
+ after = '2011-12' )
392
+
393
+ df = pd .DataFrame ({3 : np .random .randn (5 ),
394
+ 20 : np .random .randn (5 ),
395
+ 2 : np .random .randn (5 ),
396
+ 0 : np .random .randn (5 )},
397
+ columns = [3 , 20 , 2 , 0 ])
398
+ with tm .assert_raises_regex (ValueError ,
399
+ 'truncate requires a sorted index' ):
400
+ df .truncate (before = 2 , after = 20 , axis = 1 )
401
+
377
402
def test_asfreq (self ):
378
403
offset_monthly = self .tsframe .asfreq (offsets .BMonthEnd ())
379
404
rule_monthly = self .tsframe .asfreq ('BM' )
0 commit comments