1
+ import pytest
2
+
1
3
import pandas as pd
2
4
from pandas .util import testing as tm
3
5
@@ -20,20 +22,22 @@ def test_boolean_indexing(self):
20
22
dtype = 'int64' )
21
23
tm .assert_frame_equal (expected , result )
22
24
23
- def test_list_like_indexing (self ):
25
+ @pytest .mark .parametrize ("indexer, expected" ,
26
+ [(0 , [20 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]),
27
+ (slice (4 , 8 ), [0 , 1 , 2 , 3 , 20 , 20 , 20 , 20 , 8 , 9 ],),
28
+ ([3 , 5 ], [0 , 1 , 2 , 20 , 4 , 20 , 6 , 7 , 8 , 9 ])])
29
+ def test_list_like_indexing (self , indexer , expected ):
24
30
# GH 16637
25
31
df = pd .DataFrame ({'x' : range (10 )}, dtype = "int64" )
26
32
df .index = pd .to_timedelta (range (10 ), unit = 's' )
33
+ try :
34
+ df .loc [df .index [indexer ], 'x' ] = 20
35
+ except ValueError :
36
+ raise ValueError (indexer )
27
37
28
- conditions = [df .index [0 ], df .index [4 :8 ], df .index [[3 , 5 ]]]
29
- expected_data = [[20 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ],
30
- [0 , 1 , 2 , 3 , 20 , 20 , 20 , 20 , 8 , 9 ],
31
- [0 , 1 , 2 , 20 , 4 , 20 , 6 , 7 , 8 , 9 ]]
32
- for cond , data in zip (conditions , expected_data ):
33
- result = df .copy ()
34
- result .loc [cond , 'x' ] = 20
35
- expected = pd .DataFrame (data ,
36
- index = pd .to_timedelta (range (10 ), unit = 's' ),
37
- columns = ['x' ],
38
- dtype = "int64" )
39
- tm .assert_frame_equal (expected , result )
38
+ expected = pd .DataFrame (expected ,
39
+ index = pd .to_timedelta (range (10 ), unit = 's' ),
40
+ columns = ['x' ],
41
+ dtype = "int64" )
42
+
43
+ tm .assert_frame_equal (expected , df )
0 commit comments