Skip to content

Commit 074790b

Browse files
committed
change to use parametrize
1 parent 9b42a9c commit 074790b

File tree

2 files changed

+45
-65
lines changed

2 files changed

+45
-65
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Other Enhancements
234234
- :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names. (:issue:`14207`)
235235
- Improved the import time of pandas by about 2.25x. (:issue:`16764`)
236236
- :func:`read_json` and :func:`to_json` now accept a ``compression`` argument which allows them to transparently handle compressed files. (:issue:`17798`)
237-
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (PR #17367)
237+
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (:issue:`17367`)
238238

239239
.. _whatsnew_0210.api_breaking:
240240

pandas/tests/indexes/test_base.py

+44-64
Original file line numberDiff line numberDiff line change
@@ -1075,77 +1075,57 @@ def test_get_indexer_invalid(self):
10751075
with tm.assert_raises_regex(ValueError, 'limit argument'):
10761076
idx.get_indexer([1, 0], limit=1)
10771077

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):
10831099
idx = Index(np.arange(10))
10841100

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))
10941115

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))
11121120

1121+
def test_get_indexer_nearest_error(self):
1122+
idx = Index(np.arange(10))
11131123
with tm.assert_raises_regex(ValueError, 'limit argument'):
11141124
idx.get_indexer([1, 0], method='nearest', limit=1)
11151125

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])
11491129

11501130
def test_get_indexer_nearest_decreasing(self):
11511131
idx = Index(np.arange(10))[::-1]

0 commit comments

Comments
 (0)