|
7 | 7 | from pandas import DataFrame, Series
|
8 | 8 | import pandas._testing as tm
|
9 | 9 |
|
10 |
| -m = 50 |
11 |
| -n = 1000 |
12 |
| -cols = ["jim", "joe", "jolie", "joline", "jolia"] |
13 |
| - |
14 |
| -vals = [ |
15 |
| - np.random.randint(0, 10, n), |
16 |
| - np.random.choice(list("abcdefghij"), n), |
17 |
| - np.random.choice(pd.date_range("20141009", periods=10).tolist(), n), |
18 |
| - np.random.choice(list("ZYXWVUTSRQ"), n), |
19 |
| - np.random.randn(n), |
20 |
| -] |
21 |
| -vals = list(map(tuple, zip(*vals))) |
22 |
| - |
23 |
| -# bunch of keys for testing |
24 |
| -keys = [ |
25 |
| - np.random.randint(0, 11, m), |
26 |
| - np.random.choice(list("abcdefghijk"), m), |
27 |
| - np.random.choice(pd.date_range("20141009", periods=11).tolist(), m), |
28 |
| - np.random.choice(list("ZYXWVUTSRQP"), m), |
29 |
| -] |
30 |
| -keys = list(map(tuple, zip(*keys))) |
31 |
| -keys += list(map(lambda t: t[:-1], vals[:: n // m])) |
32 |
| - |
33 |
| - |
34 |
| -# covers both unique index and non-unique index |
35 |
| -df = DataFrame(vals, columns=cols) |
36 |
| -a = pd.concat([df, df]) |
37 |
| -b = df.drop_duplicates(subset=cols[:-1]) |
38 |
| - |
39 | 10 |
|
| 11 | +@pytest.mark.slow |
40 | 12 | @pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning")
|
41 |
| -@pytest.mark.parametrize("lexsort_depth", list(range(5))) |
42 |
| -@pytest.mark.parametrize("key", keys) |
43 |
| -@pytest.mark.parametrize("frame", [a, b]) |
44 |
| -def test_multiindex_get_loc(lexsort_depth, key, frame): |
45 |
| - # GH7724, GH2646 |
| 13 | +def test_multiindex_get_loc(): # GH7724, GH2646 |
46 | 14 |
|
47 | 15 | with warnings.catch_warnings(record=True):
|
48 | 16 |
|
49 | 17 | # test indexing into a multi-index before & past the lexsort depth
|
50 | 18 |
|
| 19 | + cols = ["jim", "joe", "jolie", "joline", "jolia"] |
| 20 | + |
51 | 21 | def validate(mi, df, key):
|
52 | 22 | mask = np.ones(len(df)).astype("bool")
|
53 | 23 |
|
@@ -80,11 +50,38 @@ def validate(mi, df, key):
|
80 | 50 | else: # multi hit
|
81 | 51 | tm.assert_frame_equal(mi.loc[key[: i + 1]], right)
|
82 | 52 |
|
83 |
| - if lexsort_depth == 0: |
84 |
| - df = frame.copy() |
85 |
| - else: |
86 |
| - df = frame.sort_values(by=cols[:lexsort_depth]) |
87 |
| - |
88 |
| - mi = df.set_index(cols[:-1]) |
89 |
| - assert not mi.index.lexsort_depth < lexsort_depth |
90 |
| - validate(mi, df, key) |
| 53 | + def loop(mi, df, keys): |
| 54 | + for key in keys: |
| 55 | + validate(mi, df, key) |
| 56 | + |
| 57 | + n, m = 1000, 50 |
| 58 | + |
| 59 | + vals = [ |
| 60 | + np.random.randint(0, 10, n), |
| 61 | + np.random.choice(list("abcdefghij"), n), |
| 62 | + np.random.choice(pd.date_range("20141009", periods=10).tolist(), n), |
| 63 | + np.random.choice(list("ZYXWVUTSRQ"), n), |
| 64 | + np.random.randn(n), |
| 65 | + ] |
| 66 | + vals = list(map(tuple, zip(*vals))) |
| 67 | + |
| 68 | + # bunch of keys for testing |
| 69 | + keys = [ |
| 70 | + np.random.randint(0, 11, m), |
| 71 | + np.random.choice(list("abcdefghijk"), m), |
| 72 | + np.random.choice(pd.date_range("20141009", periods=11).tolist(), m), |
| 73 | + np.random.choice(list("ZYXWVUTSRQP"), m), |
| 74 | + ] |
| 75 | + keys = list(map(tuple, zip(*keys))) |
| 76 | + keys += list(map(lambda t: t[:-1], vals[:: n // m])) |
| 77 | + |
| 78 | + # covers both unique index and non-unique index |
| 79 | + df = DataFrame(vals, columns=cols) |
| 80 | + a, b = pd.concat([df, df]), df.drop_duplicates(subset=cols[:-1]) |
| 81 | + |
| 82 | + for frame in a, b: |
| 83 | + for i in range(5): # lexsort depth |
| 84 | + df = frame.copy() if i == 0 else frame.sort_values(by=cols[:i]) |
| 85 | + mi = df.set_index(cols[:-1]) |
| 86 | + assert not mi.index.lexsort_depth < i |
| 87 | + loop(mi, df, keys) |
0 commit comments