Skip to content

Commit 50a92fb

Browse files
committed
Merge branch 'issue37053-unify-numpy-related-imports-in-pandas/tests/plotting' of https://github.com/onshek/pandas into issue37053-unify-numpy-related-imports-in-pandas/tests/plotting
2 parents 5532ae8 + 93e8e8f commit 50a92fb

26 files changed

+5166
-779
lines changed

pandas/tests/computation/test_eval.py

+429-411
Large diffs are not rendered by default.

pandas/tests/indexing/multiindex/test_indexing_slow.py

+39-42
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,17 @@
77
from pandas import DataFrame, Series
88
import pandas._testing as tm
99

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-
3910

11+
@pytest.mark.slow
4012
@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
4614

4715
with warnings.catch_warnings(record=True):
4816

4917
# test indexing into a multi-index before & past the lexsort depth
5018

19+
cols = ["jim", "joe", "jolie", "joline", "jolia"]
20+
5121
def validate(mi, df, key):
5222
mask = np.ones(len(df)).astype("bool")
5323

@@ -80,11 +50,38 @@ def validate(mi, df, key):
8050
else: # multi hit
8151
tm.assert_frame_equal(mi.loc[key[: i + 1]], right)
8252

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)

pandas/tests/indexing/multiindex/test_insert.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from numpy.random import randn
1+
import numpy as np
22

33
from pandas import DataFrame, MultiIndex, Series
44
import pandas._testing as tm
@@ -14,7 +14,7 @@ def test_setitem_mixed_depth(self):
1414

1515
tuples = sorted(zip(*arrays))
1616
index = MultiIndex.from_tuples(tuples)
17-
df = DataFrame(randn(4, 6), columns=index)
17+
df = DataFrame(np.random.randn(4, 6), columns=index)
1818

1919
result = df.copy()
2020
expected = df.copy()

pandas/tests/indexing/multiindex/test_setitem.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy.random import randn
32
import pytest
43

54
import pandas as pd
@@ -311,7 +310,9 @@ def test_frame_getitem_setitem_multislice(self):
311310
tm.assert_frame_equal(df, result)
312311

313312
def test_frame_setitem_multi_column(self):
314-
df = DataFrame(randn(10, 4), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]])
313+
df = DataFrame(
314+
np.random.randn(10, 4), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]]
315+
)
315316

316317
cp = df.copy()
317318
cp["a"] = cp["b"]

pandas/tests/indexing/multiindex/test_sorted.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy.random import randn
32
import pytest
43

54
from pandas import DataFrame, MultiIndex, Series
@@ -115,7 +114,7 @@ def test_series_getitem_not_sorted(self):
115114
]
116115
tuples = zip(*arrays)
117116
index = MultiIndex.from_tuples(tuples)
118-
s = Series(randn(8), index=index)
117+
s = Series(np.random.randn(8), index=index)
119118

120119
arrays = [np.array(x) for x in zip(*index.values)]
121120

pandas/tests/io/test_clipboard.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from textwrap import dedent
22

33
import numpy as np
4-
from numpy.random import randint
54
import pytest
65

76
import pandas as pd
@@ -54,7 +53,7 @@ def df(request):
5453
return tm.makeCustomDataframe(
5554
max_rows + 1,
5655
3,
57-
data_gen_f=lambda *args: randint(2),
56+
data_gen_f=lambda *args: np.random.randint(2),
5857
c_idx_type="s",
5958
r_idx_type="i",
6059
c_idx_names=[None],
@@ -95,7 +94,7 @@ def df(request):
9594
return tm.makeCustomDataframe(
9695
5,
9796
3,
98-
data_gen_f=lambda *args: randint(2),
97+
data_gen_f=lambda *args: np.random.randint(2),
9998
c_idx_type="s",
10099
r_idx_type="i",
101100
c_idx_names=[None],

pandas/tests/io/test_html.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from urllib.error import URLError
88

99
import numpy as np
10-
from numpy.random import rand
1110
import pytest
1211

1312
from pandas.compat import is_platform_windows
@@ -110,7 +109,7 @@ def test_to_html_compat(self):
110109
tm.makeCustomDataframe(
111110
4,
112111
3,
113-
data_gen_f=lambda *args: rand(),
112+
data_gen_f=lambda *args: np.random.rand(),
114113
c_idx_names=False,
115114
r_idx_names=False,
116115
)

pandas/tests/plotting/common.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import warnings
33

44
import numpy as np
5-
from numpy import random
65

76
from pandas.util._decorators import cache_readonly
87
import pandas.util._test_decorators as td
@@ -50,11 +49,11 @@ def setup_method(self, method):
5049
{
5150
"gender": gender,
5251
"classroom": classroom,
53-
"height": random.normal(66, 4, size=n),
54-
"weight": random.normal(161, 32, size=n),
55-
"category": random.randint(4, size=n),
52+
"height": np.random.normal(66, 4, size=n),
53+
"weight": np.random.normal(161, 32, size=n),
54+
"category": np.random.randint(4, size=n),
5655
"datetime": to_datetime(
57-
random.randint(
56+
np.random.randint(
5857
self.start_date_to_int64,
5958
self.end_date_to_int64,
6059
size=n,

pandas/tests/plotting/test_boxplot_method.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import string
33

44
import numpy as np
5-
from numpy import random
65
import pytest
76

87
import pandas.util._test_decorators as td
@@ -186,7 +185,7 @@ def test_boxplot_numeric_data(self):
186185
)
187186
def test_color_kwd(self, colors_kwd, expected):
188187
# GH: 26214
189-
df = DataFrame(random.rand(10, 2))
188+
df = DataFrame(np.random.rand(10, 2))
190189
result = df.boxplot(color=colors_kwd, return_type="dict")
191190
for k, v in expected.items():
192191
assert result[k][0].get_color() == v
@@ -197,7 +196,7 @@ def test_color_kwd(self, colors_kwd, expected):
197196
)
198197
def test_color_kwd_errors(self, dict_colors, msg):
199198
# GH: 26214
200-
df = DataFrame(random.rand(10, 2))
199+
df = DataFrame(np.random.rand(10, 2))
201200
with pytest.raises(ValueError, match=msg):
202201
df.boxplot(color=dict_colors, return_type="dict")
203202

@@ -293,7 +292,7 @@ def test_grouped_box_return_type(self):
293292
self._check_box_return_type(result, "dict", expected_keys=["Male", "Female"])
294293

295294
columns2 = "X B C D A G Y N Q O".split()
296-
df2 = DataFrame(random.randn(50, 10), columns=columns2)
295+
df2 = DataFrame(np.random.randn(50, 10), columns=columns2)
297296
categories2 = "A B C D E F G H I J".split()
298297
df2["category"] = categories2 * 5
299298

0 commit comments

Comments
 (0)