From 098a5558dc7bf474a22f9fd8074188523c71034b Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 21 Oct 2020 19:56:24 -0700 Subject: [PATCH 1/2] TST: parametrize --- .../indexing/multiindex/test_indexing_slow.py | 81 ++++++++++--------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/pandas/tests/indexing/multiindex/test_indexing_slow.py b/pandas/tests/indexing/multiindex/test_indexing_slow.py index d8e56661b7d61..7b67f3e265807 100644 --- a/pandas/tests/indexing/multiindex/test_indexing_slow.py +++ b/pandas/tests/indexing/multiindex/test_indexing_slow.py @@ -7,17 +7,47 @@ from pandas import DataFrame, Series import pandas._testing as tm +m = 50 +n = 1000 +cols = ["jim", "joe", "jolie", "joline", "jolia"] + +vals = [ + np.random.randint(0, 10, n), + np.random.choice(list("abcdefghij"), n), + np.random.choice(pd.date_range("20141009", periods=10).tolist(), n), + np.random.choice(list("ZYXWVUTSRQ"), n), + np.random.randn(n), +] +vals = list(map(tuple, zip(*vals))) + +# bunch of keys for testing +keys = [ + np.random.randint(0, 11, m), + np.random.choice(list("abcdefghijk"), m), + np.random.choice(pd.date_range("20141009", periods=11).tolist(), m), + np.random.choice(list("ZYXWVUTSRQP"), m), +] +keys = list(map(tuple, zip(*keys))) +keys += list(map(lambda t: t[:-1], vals[:: n // m])) + + +# covers both unique index and non-unique index +df = DataFrame(vals, columns=cols) +a = pd.concat([df, df]) +b = df.drop_duplicates(subset=cols[:-1]) + @pytest.mark.slow @pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") -def test_multiindex_get_loc(): # GH7724, GH2646 +@pytest.mark.parametrize("lexsort_depth", list(range(5))) +@pytest.mark.parametrize("key", keys) +@pytest.mark.parametrize("frame", [a, b]) +def test_multiindex_get_loc(lexsort_depth, key, frame): + # GH7724, GH2646 with warnings.catch_warnings(record=True): # test indexing into a multi-index before & past the lexsort depth - from numpy.random import choice, randint, randn - - cols = ["jim", "joe", "jolie", "joline", "jolia"] def validate(mi, df, key): mask = np.ones(len(df)).astype("bool") @@ -51,38 +81,11 @@ def validate(mi, df, key): else: # multi hit tm.assert_frame_equal(mi.loc[key[: i + 1]], right) - def loop(mi, df, keys): - for key in keys: - validate(mi, df, key) - - n, m = 1000, 50 - - vals = [ - randint(0, 10, n), - choice(list("abcdefghij"), n), - choice(pd.date_range("20141009", periods=10).tolist(), n), - choice(list("ZYXWVUTSRQ"), n), - randn(n), - ] - vals = list(map(tuple, zip(*vals))) - - # bunch of keys for testing - keys = [ - randint(0, 11, m), - choice(list("abcdefghijk"), m), - choice(pd.date_range("20141009", periods=11).tolist(), m), - choice(list("ZYXWVUTSRQP"), m), - ] - keys = list(map(tuple, zip(*keys))) - keys += list(map(lambda t: t[:-1], vals[:: n // m])) - - # covers both unique index and non-unique index - df = DataFrame(vals, columns=cols) - a, b = pd.concat([df, df]), df.drop_duplicates(subset=cols[:-1]) - - for frame in a, b: - for i in range(5): # lexsort depth - df = frame.copy() if i == 0 else frame.sort_values(by=cols[:i]) - mi = df.set_index(cols[:-1]) - assert not mi.index.lexsort_depth < i - loop(mi, df, keys) + if lexsort_depth == 0: + df = frame.copy() + else: + df = frame.sort_values(by=cols[:lexsort_depth]) + + mi = df.set_index(cols[:-1]) + assert not mi.index.lexsort_depth < lexsort_depth + validate(mi, df, key) From ee37b3d128b7975169a219d6c568be7e7da7f9ce Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 22 Oct 2020 08:20:28 -0700 Subject: [PATCH 2/2] remove slow mark --- pandas/tests/indexing/multiindex/test_indexing_slow.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexing/multiindex/test_indexing_slow.py b/pandas/tests/indexing/multiindex/test_indexing_slow.py index 7b67f3e265807..efe1e0f0d75b5 100644 --- a/pandas/tests/indexing/multiindex/test_indexing_slow.py +++ b/pandas/tests/indexing/multiindex/test_indexing_slow.py @@ -37,7 +37,6 @@ b = df.drop_duplicates(subset=cols[:-1]) -@pytest.mark.slow @pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") @pytest.mark.parametrize("lexsort_depth", list(range(5))) @pytest.mark.parametrize("key", keys)