@@ -1075,77 +1075,57 @@ def test_get_indexer_invalid(self):
1075
1075
with tm .assert_raises_regex (ValueError , 'limit argument' ):
1076
1076
idx .get_indexer ([1 , 0 ], limit = 1 )
1077
1077
1078
- with pytest .raises (ValueError , match = 'tolerance size must match' ):
1079
- idx .get_indexer ([1 , 0 ], method = 'nearest' ,
1080
- tolerance = [1 , 2 , 3 ])
1081
-
1082
- def test_get_indexer_nearest (self ):
1078
+ @pytest .mark .parametrize (
1079
+ 'method, tolerance, indexer, expected' ,
1080
+ [
1081
+ ('pad' , None , [0 , 5 , 9 ], [0 , 5 , 9 ]),
1082
+ ('backfill' , None , [0 , 5 , 9 ], [0 , 5 , 9 ]),
1083
+ ('nearest' , None , [0 , 5 , 9 ], [0 , 5 , 9 ]),
1084
+ ('pad' , 0 , [0 , 5 , 9 ], [0 , 5 , 9 ]),
1085
+ ('backfill' , 0 , [0 , 5 , 9 ], [0 , 5 , 9 ]),
1086
+ ('nearest' , 0 , [0 , 5 , 9 ], [0 , 5 , 9 ]),
1087
+
1088
+ ('pad' , None , [0.2 , 1.8 , 8.5 ], [0 , 1 , 8 ]),
1089
+ ('backfill' , None , [0.2 , 1.8 , 8.5 ], [1 , 2 , 9 ]),
1090
+ ('nearest' , None , [0.2 , 1.8 , 8.5 ], [0 , 2 , 9 ]),
1091
+ ('pad' , 1 , [0.2 , 1.8 , 8.5 ], [0 , 1 , 8 ]),
1092
+ ('backfill' , 1 , [0.2 , 1.8 , 8.5 ], [1 , 2 , 9 ]),
1093
+ ('nearest' , 1 , [0.2 , 1.8 , 8.5 ], [0 , 2 , 9 ]),
1094
+
1095
+ ('pad' , 0.2 , [0.2 , 1.8 , 8.5 ], [0 , - 1 , - 1 ]),
1096
+ ('backfill' , 0.2 , [0.2 , 1.8 , 8.5 ], [- 1 , 2 , - 1 ]),
1097
+ ('nearest' , 0.2 , [0.2 , 1.8 , 8.5 ], [0 , 2 , - 1 ])])
1098
+ def test_get_indexer_nearest (self , method , tolerance , indexer , expected ):
1083
1099
idx = Index (np .arange (10 ))
1084
1100
1085
- all_methods = ['pad' , 'backfill' , 'nearest' ]
1086
- for method in all_methods :
1087
- actual = idx .get_indexer ([0 , 5 , 9 ], method = method )
1088
- tm .assert_numpy_array_equal (actual , np .array ([0 , 5 , 9 ],
1089
- dtype = np .intp ))
1090
-
1091
- actual = idx .get_indexer ([0 , 5 , 9 ], method = method , tolerance = 0 )
1092
- tm .assert_numpy_array_equal (actual , np .array ([0 , 5 , 9 ],
1093
- dtype = np .intp ))
1101
+ actual = idx .get_indexer (indexer , method = method , tolerance = tolerance )
1102
+ tm .assert_numpy_array_equal (actual , np .array (expected ,
1103
+ dtype = np .intp ))
1104
+
1105
+ @pytest .mark .parametrize ('listtype' , [list , tuple , Series , np .array ])
1106
+ @pytest .mark .parametrize (
1107
+ 'tolerance, expected' ,
1108
+ list (zip ([[0.3 , 0.3 , 0.1 ], [0.2 , 0.1 , 0.1 ],
1109
+ [0.1 , 0.5 , 0.5 ]],
1110
+ [[0 , 2 , - 1 ], [0 , - 1 , - 1 ],
1111
+ [- 1 , 2 , 9 ]])))
1112
+ def test_get_indexer_nearest_listlike_tolerance (self , tolerance ,
1113
+ expected , listtype ):
1114
+ idx = Index (np .arange (10 ))
1094
1115
1095
- for method , expected in zip (all_methods , [[0 , 1 , 8 ], [1 , 2 , 9 ],
1096
- [0 , 2 , 9 ]]):
1097
- actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = method )
1098
- tm .assert_numpy_array_equal (actual , np .array (expected ,
1099
- dtype = np .intp ))
1100
-
1101
- actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = method ,
1102
- tolerance = 1 )
1103
- tm .assert_numpy_array_equal (actual , np .array (expected ,
1104
- dtype = np .intp ))
1105
-
1106
- for method , expected in zip (all_methods , [[0 , - 1 , - 1 ], [- 1 , 2 , - 1 ],
1107
- [0 , 2 , - 1 ]]):
1108
- actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = method ,
1109
- tolerance = 0.2 )
1110
- tm .assert_numpy_array_equal (actual , np .array (expected ,
1111
- dtype = np .intp ))
1116
+ actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = 'nearest' ,
1117
+ tolerance = listtype (tolerance ))
1118
+ tm .assert_numpy_array_equal (actual , np .array (expected ,
1119
+ dtype = np .intp ))
1112
1120
1121
+ def test_get_indexer_nearest_error (self ):
1122
+ idx = Index (np .arange (10 ))
1113
1123
with tm .assert_raises_regex (ValueError , 'limit argument' ):
1114
1124
idx .get_indexer ([1 , 0 ], method = 'nearest' , limit = 1 )
1115
1125
1116
- def test_get_indexer_nearest_listtolerance (self ):
1117
- idx = Index (np .arange (10 ))
1118
-
1119
- all_methods = ['pad' , 'backfill' , 'nearest' ]
1120
- for method in all_methods :
1121
- actual = idx .get_indexer ([0 , 5 , 9 ], method = method ,
1122
- tolerance = [0 , 0 , 0 ])
1123
- tm .assert_numpy_array_equal (actual , np .array ([0 , 5 , 9 ],
1124
- dtype = np .intp ))
1125
-
1126
- for method , expected in zip (all_methods , [[0 , 1 , 8 ], [1 , 2 , 9 ],
1127
- [0 , 2 , 9 ]]):
1128
- actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = method ,
1129
- tolerance = [1.5 , 1 , 1 ])
1130
- tm .assert_numpy_array_equal (actual , np .array (expected ,
1131
- dtype = np .intp ))
1132
-
1133
- for method , expected in zip (all_methods , [[0 , - 1 , - 1 ], [- 1 , 2 , - 1 ],
1134
- [0 , 2 , - 1 ]]):
1135
- actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = method ,
1136
- tolerance = [0.5 , 0.2 , 0.2 ])
1137
- tm .assert_numpy_array_equal (actual , np .array (expected ,
1138
- dtype = np .intp ))
1139
- for tolerance , expected , listtype in \
1140
- zip ([[0.3 , 0.3 , 0.1 ], [0.2 , 0.1 , 0.1 ],
1141
- [0.1 , 0.5 , 0.5 ]],
1142
- [[0 , 2 , - 1 ], [0 , - 1 , - 1 ],
1143
- [- 1 , 2 , 9 ]],
1144
- (list , tuple , Series , np .array )):
1145
- actual = idx .get_indexer ([0.2 , 1.8 , 8.5 ], method = 'nearest' ,
1146
- tolerance = listtype (tolerance ))
1147
- tm .assert_numpy_array_equal (actual , np .array (expected ,
1148
- dtype = np .intp ))
1126
+ with pytest .raises (ValueError , match = 'tolerance size must match' ):
1127
+ idx .get_indexer ([1 , 0 ], method = 'nearest' ,
1128
+ tolerance = [1 , 2 , 3 ])
1149
1129
1150
1130
def test_get_indexer_nearest_decreasing (self ):
1151
1131
idx = Index (np .arange (10 ))[::- 1 ]
0 commit comments