Skip to content

Commit 49dc7b9

Browse files
authored
TST: Simplify more slow tests (#53862)
* Clean more slow tests * Closer scope
1 parent 4efe208 commit 49dc7b9

File tree

9 files changed

+85
-65
lines changed

9 files changed

+85
-65
lines changed

pandas/tests/arithmetic/test_numeric.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,24 @@ def test_compare_invalid(self):
104104
b.name = pd.Timestamp("2000-01-01")
105105
tm.assert_series_equal(a / b, 1 / (b / a))
106106

107-
def test_numeric_cmp_string_numexpr_path(self, box_with_array):
107+
def test_numeric_cmp_string_numexpr_path(self, box_with_array, monkeypatch):
108108
# GH#36377, GH#35700
109109
box = box_with_array
110110
xbox = box if box is not Index else np.ndarray
111111

112-
obj = Series(np.random.randn(10**5))
112+
obj = Series(np.random.randn(51))
113113
obj = tm.box_expected(obj, box, transpose=False)
114+
with monkeypatch.context() as m:
115+
m.setattr(expr, "_MIN_ELEMENTS", 50)
116+
result = obj == "a"
114117

115-
result = obj == "a"
116-
117-
expected = Series(np.zeros(10**5, dtype=bool))
118+
expected = Series(np.zeros(51, dtype=bool))
118119
expected = tm.box_expected(expected, xbox, transpose=False)
119120
tm.assert_equal(result, expected)
120121

121-
result = obj != "a"
122+
with monkeypatch.context() as m:
123+
m.setattr(expr, "_MIN_ELEMENTS", 50)
124+
result = obj != "a"
122125
tm.assert_equal(result, ~expected)
123126

124127
msg = "Invalid comparison between dtype=float64 and str"

pandas/tests/groupby/test_function.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def test_intercept_builtin_sum():
6868
@pytest.mark.parametrize("keys", ["jim", ["jim", "joe"]]) # Single key # Multi-key
6969
def test_builtins_apply(keys, f):
7070
# see gh-8155
71-
df = DataFrame(np.random.randint(1, 50, (1000, 2)), columns=["jim", "joe"])
72-
df["jolie"] = np.random.randn(1000)
71+
rs = np.random.RandomState(42)
72+
df = DataFrame(rs.randint(1, 7, (10, 2)), columns=["jim", "joe"])
73+
df["jolie"] = rs.randn(10)
7374

7475
gb = df.groupby(keys)
7576

pandas/tests/indexes/base_class/test_indexing.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ def test_get_loc_tuple_monotonic_above_size_cutoff(self, monkeypatch):
6464
# Go through the libindex path for which using
6565
# _bin_search vs ndarray.searchsorted makes a difference
6666

67-
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 100)
68-
lev = list("ABCD")
69-
dti = pd.date_range("2016-01-01", periods=10)
67+
with monkeypatch.context():
68+
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 100)
69+
lev = list("ABCD")
70+
dti = pd.date_range("2016-01-01", periods=10)
7071

71-
mi = pd.MultiIndex.from_product([lev, range(5), dti])
72-
oidx = mi.to_flat_index()
72+
mi = pd.MultiIndex.from_product([lev, range(5), dti])
73+
oidx = mi.to_flat_index()
7374

74-
loc = len(oidx) // 2
75-
tup = oidx[loc]
75+
loc = len(oidx) // 2
76+
tup = oidx[loc]
7677

77-
res = oidx.get_loc(tup)
78+
res = oidx.get_loc(tup)
7879
assert res == loc
7980

8081
def test_get_loc_nan_object_dtype_nonmonotonic_nonunique(self):

pandas/tests/indexes/multi/test_duplicates.py

+34-29
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import numpy as np
44
import pytest
55

6-
from pandas._libs import hashtable
6+
from pandas._libs import (
7+
hashtable,
8+
index as libindex,
9+
)
710

811
from pandas import (
912
NA,
@@ -232,41 +235,43 @@ def test_duplicated(idx_dup, keep, expected):
232235

233236

234237
@pytest.mark.arm_slow
235-
def test_duplicated_large(keep):
238+
def test_duplicated_hashtable_impl(keep, monkeypatch):
236239
# GH 9125
237-
n, k = 200, 5000
240+
n, k = 6, 10
238241
levels = [np.arange(n), tm.makeStringIndex(n), 1000 + np.arange(n)]
239-
codes = [np.random.choice(n, k * n) for lev in levels]
240-
mi = MultiIndex(levels=levels, codes=codes)
242+
codes = [np.random.choice(n, k * n) for _ in levels]
243+
with monkeypatch.context() as m:
244+
m.setattr(libindex, "_SIZE_CUTOFF", 50)
245+
mi = MultiIndex(levels=levels, codes=codes)
241246

242-
result = mi.duplicated(keep=keep)
243-
expected = hashtable.duplicated(mi.values, keep=keep)
247+
result = mi.duplicated(keep=keep)
248+
expected = hashtable.duplicated(mi.values, keep=keep)
244249
tm.assert_numpy_array_equal(result, expected)
245250

246251

247-
def test_duplicated2():
248-
# TODO: more informative test name
252+
@pytest.mark.parametrize("val", [101, 102])
253+
def test_duplicated_with_nan(val):
254+
# GH5873
255+
mi = MultiIndex.from_arrays([[101, val], [3.5, np.nan]])
256+
assert not mi.has_duplicates
257+
258+
tm.assert_numpy_array_equal(mi.duplicated(), np.zeros(2, dtype="bool"))
259+
260+
261+
@pytest.mark.parametrize("n", range(1, 6))
262+
@pytest.mark.parametrize("m", range(1, 5))
263+
def test_duplicated_with_nan_multi_shape(n, m):
249264
# GH5873
250-
for a in [101, 102]:
251-
mi = MultiIndex.from_arrays([[101, a], [3.5, np.nan]])
252-
assert not mi.has_duplicates
253-
254-
tm.assert_numpy_array_equal(mi.duplicated(), np.zeros(2, dtype="bool"))
255-
256-
for n in range(1, 6): # 1st level shape
257-
for m in range(1, 5): # 2nd level shape
258-
# all possible unique combinations, including nan
259-
codes = product(range(-1, n), range(-1, m))
260-
mi = MultiIndex(
261-
levels=[list("abcde")[:n], list("WXYZ")[:m]],
262-
codes=np.random.permutation(list(codes)).T,
263-
)
264-
assert len(mi) == (n + 1) * (m + 1)
265-
assert not mi.has_duplicates
266-
267-
tm.assert_numpy_array_equal(
268-
mi.duplicated(), np.zeros(len(mi), dtype="bool")
269-
)
265+
# all possible unique combinations, including nan
266+
codes = product(range(-1, n), range(-1, m))
267+
mi = MultiIndex(
268+
levels=[list("abcde")[:n], list("WXYZ")[:m]],
269+
codes=np.random.permutation(list(codes)).T,
270+
)
271+
assert len(mi) == (n + 1) * (m + 1)
272+
assert not mi.has_duplicates
273+
274+
tm.assert_numpy_array_equal(mi.duplicated(), np.zeros(len(mi), dtype="bool"))
270275

271276

272277
def test_duplicated_drop_duplicates():

pandas/tests/indexes/multi/test_integrity.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44
import pytest
55

6+
from pandas._libs import index as libindex
7+
68
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
79

810
import pandas as pd
@@ -186,12 +188,11 @@ def test_large_multiindex_error():
186188
df_above_1000000.loc[(3, 0), "dest"]
187189

188190

189-
def test_million_record_attribute_error():
191+
def test_mi_hashtable_populated_attribute_error(monkeypatch):
190192
# GH 18165
191-
r = list(range(1000000))
192-
df = pd.DataFrame(
193-
{"a": r, "b": r}, index=MultiIndex.from_tuples([(x, x) for x in r])
194-
)
193+
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 50)
194+
r = range(50)
195+
df = pd.DataFrame({"a": r, "b": r}, index=MultiIndex.from_arrays([r, r]))
195196

196197
msg = "'Series' object has no attribute 'foo'"
197198
with pytest.raises(AttributeError, match=msg):

pandas/tests/io/test_compression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def test_gzip_reproducibility_file_name():
177177
with tm.ensure_clean() as path:
178178
path = Path(path)
179179
df.to_csv(path, compression=compression_options)
180-
time.sleep(2)
180+
time.sleep(0.1)
181181
output = path.read_bytes()
182182
df.to_csv(path, compression=compression_options)
183183
assert output == path.read_bytes()
@@ -196,7 +196,7 @@ def test_gzip_reproducibility_file_object():
196196
buffer = io.BytesIO()
197197
df.to_csv(buffer, compression=compression_options, mode="wb")
198198
output = buffer.getvalue()
199-
time.sleep(2)
199+
time.sleep(0.1)
200200
buffer = io.BytesIO()
201201
df.to_csv(buffer, compression=compression_options, mode="wb")
202202
assert output == buffer.getvalue()

pandas/tests/plotting/frame/test_frame_subplots.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def test_bar_log_subplots(self):
404404
tm.assert_numpy_array_equal(ax[0].yaxis.get_ticklocs(), expected)
405405
tm.assert_numpy_array_equal(ax[1].yaxis.get_ticklocs(), expected)
406406

407-
def test_boxplot_subplots_return_type(self, hist_df):
407+
def test_boxplot_subplots_return_type_default(self, hist_df):
408408
df = hist_df
409409

410410
# normal style: return_type=None
@@ -414,14 +414,16 @@ def test_boxplot_subplots_return_type(self, hist_df):
414414
result, None, expected_keys=["height", "weight", "category"]
415415
)
416416

417-
for t in ["dict", "axes", "both"]:
418-
returned = df.plot.box(return_type=t, subplots=True)
419-
_check_box_return_type(
420-
returned,
421-
t,
422-
expected_keys=["height", "weight", "category"],
423-
check_ax_title=False,
424-
)
417+
@pytest.mark.parametrize("rt", ["dict", "axes", "both"])
418+
def test_boxplot_subplots_return_type(self, hist_df, rt):
419+
df = hist_df
420+
returned = df.plot.box(return_type=rt, subplots=True)
421+
_check_box_return_type(
422+
returned,
423+
rt,
424+
expected_keys=["height", "weight", "category"],
425+
check_ax_title=False,
426+
)
425427

426428
def test_df_subplots_patterns_minorticks(self):
427429
# GH 10657

pandas/tests/tslibs/test_conversion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def test_tz_convert_single_matches_tz_convert_hourly(tz_aware_fixture):
7676
@pytest.mark.parametrize("freq", ["D", "A"])
7777
def test_tz_convert_single_matches_tz_convert(tz_aware_fixture, freq):
7878
tz = tz_aware_fixture
79-
tz_didx = date_range("2000-01-01", "2020-01-01", freq=freq, tz=tz)
80-
naive_didx = date_range("2000-01-01", "2020-01-01", freq=freq)
79+
tz_didx = date_range("2018-01-01", "2020-01-01", freq=freq, tz=tz)
80+
naive_didx = date_range("2018-01-01", "2020-01-01", freq=freq)
8181

8282
_compare_utc_to_local(tz_didx)
8383
_compare_local_to_utc(tz_didx, naive_didx)

pandas/tests/window/conftest.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,22 @@ def numeric_only(request):
9090
return request.param
9191

9292

93-
@pytest.fixture(params=[pytest.param("numba", marks=td.skip_if_no("numba")), "cython"])
93+
@pytest.fixture(
94+
params=[
95+
pytest.param("numba", marks=[td.skip_if_no("numba"), pytest.mark.single_cpu]),
96+
"cython",
97+
]
98+
)
9499
def engine(request):
95100
"""engine keyword argument for rolling.apply"""
96101
return request.param
97102

98103

99104
@pytest.fixture(
100105
params=[
101-
pytest.param(("numba", True), marks=td.skip_if_no("numba")),
106+
pytest.param(
107+
("numba", True), marks=[td.skip_if_no("numba"), pytest.mark.single_cpu]
108+
),
102109
("cython", True),
103110
("cython", False),
104111
]

0 commit comments

Comments
 (0)