Skip to content

Commit a1c9907

Browse files
authored
TST: Parameterize test_algos (#44760)
1 parent 9198f35 commit a1c9907

File tree

1 file changed

+65
-77
lines changed

1 file changed

+65
-77
lines changed

pandas/tests/test_algos.py

+65-77
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,8 @@ def test_different_nans(self):
779779
expected = np.array([np.nan])
780780
tm.assert_numpy_array_equal(result, expected)
781781

782-
def test_first_nan_kept(self):
782+
@pytest.mark.parametrize("el_type", [np.float64, object])
783+
def test_first_nan_kept(self, el_type):
783784
# GH 22295
784785
# create different nans from bit-patterns:
785786
bits_for_nan1 = 0xFFF8000000000001
@@ -788,13 +789,12 @@ def test_first_nan_kept(self):
788789
NAN2 = struct.unpack("d", struct.pack("=Q", bits_for_nan2))[0]
789790
assert NAN1 != NAN1
790791
assert NAN2 != NAN2
791-
for el_type in [np.float64, object]:
792-
a = np.array([NAN1, NAN2], dtype=el_type)
793-
result = pd.unique(a)
794-
assert result.size == 1
795-
# use bit patterns to identify which nan was kept:
796-
result_nan_bits = struct.unpack("=Q", struct.pack("d", result[0]))[0]
797-
assert result_nan_bits == bits_for_nan1
792+
a = np.array([NAN1, NAN2], dtype=el_type)
793+
result = pd.unique(a)
794+
assert result.size == 1
795+
# use bit patterns to identify which nan was kept:
796+
result_nan_bits = struct.unpack("=Q", struct.pack("d", result[0]))[0]
797+
assert result_nan_bits == bits_for_nan1
798798

799799
def test_do_not_mangle_na_values(self, unique_nulls_fixture, unique_nulls_fixture2):
800800
# GH 22295
@@ -1261,21 +1261,20 @@ def test_dropna(self):
12611261
expected = Series([3, 2, 1], index=[5.0, 10.3, np.nan])
12621262
tm.assert_series_equal(result, expected)
12631263

1264-
def test_value_counts_normalized(self):
1264+
@pytest.mark.parametrize("dtype", (np.float64, object, "M8[ns]"))
1265+
def test_value_counts_normalized(self, dtype):
12651266
# GH12558
12661267
s = Series([1] * 2 + [2] * 3 + [np.nan] * 5)
1267-
dtypes = (np.float64, object, "M8[ns]")
1268-
for t in dtypes:
1269-
s_typed = s.astype(t)
1270-
result = s_typed.value_counts(normalize=True, dropna=False)
1271-
expected = Series(
1272-
[0.5, 0.3, 0.2], index=Series([np.nan, 2.0, 1.0], dtype=t)
1273-
)
1274-
tm.assert_series_equal(result, expected)
1268+
s_typed = s.astype(dtype)
1269+
result = s_typed.value_counts(normalize=True, dropna=False)
1270+
expected = Series(
1271+
[0.5, 0.3, 0.2], index=Series([np.nan, 2.0, 1.0], dtype=dtype)
1272+
)
1273+
tm.assert_series_equal(result, expected)
12751274

1276-
result = s_typed.value_counts(normalize=True, dropna=True)
1277-
expected = Series([0.6, 0.4], index=Series([2.0, 1.0], dtype=t))
1278-
tm.assert_series_equal(result, expected)
1275+
result = s_typed.value_counts(normalize=True, dropna=True)
1276+
expected = Series([0.6, 0.4], index=Series([2.0, 1.0], dtype=dtype))
1277+
tm.assert_series_equal(result, expected)
12791278

12801279
def test_value_counts_uint64(self):
12811280
arr = np.array([2 ** 63], dtype=np.uint64)
@@ -1479,13 +1478,10 @@ def test_datetime_likes(self):
14791478
res_false = s.duplicated(keep=False)
14801479
tm.assert_series_equal(res_false, Series(exp_false))
14811480

1482-
def test_unique_index(self):
1483-
cases = [Index([1, 2, 3]), pd.RangeIndex(0, 3)]
1484-
for case in cases:
1485-
assert case.is_unique is True
1486-
tm.assert_numpy_array_equal(
1487-
case.duplicated(), np.array([False, False, False])
1488-
)
1481+
@pytest.mark.parametrize("case", [Index([1, 2, 3]), pd.RangeIndex(0, 3)])
1482+
def test_unique_index(self, case):
1483+
assert case.is_unique is True
1484+
tm.assert_numpy_array_equal(case.duplicated(), np.array([False, False, False]))
14891485

14901486
@pytest.mark.parametrize(
14911487
"arr, uniques",
@@ -1744,20 +1740,25 @@ def test_unique_label_indices():
17441740

17451741
class TestRank:
17461742
@td.skip_if_no_scipy
1747-
def test_scipy_compat(self):
1743+
@pytest.mark.parametrize(
1744+
"arr",
1745+
[
1746+
[np.nan, np.nan, 5.0, 5.0, 5.0, np.nan, 1, 2, 3, np.nan],
1747+
[4.0, np.nan, 5.0, 5.0, 5.0, np.nan, 1, 2, 4.0, np.nan],
1748+
],
1749+
)
1750+
def test_scipy_compat(self, arr):
17481751
from scipy.stats import rankdata
17491752

1750-
def _check(arr):
1751-
mask = ~np.isfinite(arr)
1752-
arr = arr.copy()
1753-
result = libalgos.rank_1d(arr)
1754-
arr[mask] = np.inf
1755-
exp = rankdata(arr)
1756-
exp[mask] = np.nan
1757-
tm.assert_almost_equal(result, exp)
1753+
arr = np.array(arr)
17581754

1759-
_check(np.array([np.nan, np.nan, 5.0, 5.0, 5.0, np.nan, 1, 2, 3, np.nan]))
1760-
_check(np.array([4.0, np.nan, 5.0, 5.0, 5.0, np.nan, 1, 2, 4.0, np.nan]))
1755+
mask = ~np.isfinite(arr)
1756+
arr = arr.copy()
1757+
result = libalgos.rank_1d(arr)
1758+
arr[mask] = np.inf
1759+
exp = rankdata(arr)
1760+
exp[mask] = np.nan
1761+
tm.assert_almost_equal(result, exp)
17611762

17621763
@pytest.mark.parametrize("dtype", np.typecodes["AllInteger"])
17631764
def test_basic(self, writable, dtype):
@@ -1769,12 +1770,12 @@ def test_basic(self, writable, dtype):
17691770
result = algos.rank(ser)
17701771
tm.assert_numpy_array_equal(result, exp)
17711772

1772-
def test_uint64_overflow(self):
1773+
@pytest.mark.parametrize("dtype", [np.float64, np.uint64])
1774+
def test_uint64_overflow(self, dtype):
17731775
exp = np.array([1, 2], dtype=np.float64)
17741776

1775-
for dtype in [np.float64, np.uint64]:
1776-
s = Series([1, 2 ** 63], dtype=dtype)
1777-
tm.assert_numpy_array_equal(algos.rank(s), exp)
1777+
s = Series([1, 2 ** 63], dtype=dtype)
1778+
tm.assert_numpy_array_equal(algos.rank(s), exp)
17781779

17791780
def test_too_many_ndims(self):
17801781
arr = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
@@ -1819,21 +1820,6 @@ def test_pad_backfill_object_segfault():
18191820

18201821

18211822
class TestTseriesUtil:
1822-
def test_combineFunc(self):
1823-
pass
1824-
1825-
def test_reindex(self):
1826-
pass
1827-
1828-
def test_isna(self):
1829-
pass
1830-
1831-
def test_groupby(self):
1832-
pass
1833-
1834-
def test_groupby_withnull(self):
1835-
pass
1836-
18371823
def test_backfill(self):
18381824
old = Index([1, 5, 10])
18391825
new = Index(list(range(12)))
@@ -2274,44 +2260,45 @@ def test_no_mode(self):
22742260
exp = Series([], dtype=np.float64, index=Index([], dtype=int))
22752261
tm.assert_series_equal(algos.mode([]), exp)
22762262

2277-
def test_mode_single(self):
2263+
@pytest.mark.parametrize("dt", np.typecodes["AllInteger"] + np.typecodes["Float"])
2264+
def test_mode_single(self, dt):
22782265
# GH 15714
22792266
exp_single = [1]
22802267
data_single = [1]
22812268

22822269
exp_multi = [1]
22832270
data_multi = [1, 1]
22842271

2285-
for dt in np.typecodes["AllInteger"] + np.typecodes["Float"]:
2286-
s = Series(data_single, dtype=dt)
2287-
exp = Series(exp_single, dtype=dt)
2288-
tm.assert_series_equal(algos.mode(s), exp)
2272+
s = Series(data_single, dtype=dt)
2273+
exp = Series(exp_single, dtype=dt)
2274+
tm.assert_series_equal(algos.mode(s), exp)
22892275

2290-
s = Series(data_multi, dtype=dt)
2291-
exp = Series(exp_multi, dtype=dt)
2292-
tm.assert_series_equal(algos.mode(s), exp)
2276+
s = Series(data_multi, dtype=dt)
2277+
exp = Series(exp_multi, dtype=dt)
2278+
tm.assert_series_equal(algos.mode(s), exp)
22932279

2280+
def test_mode_obj_int(self):
22942281
exp = Series([1], dtype=int)
22952282
tm.assert_series_equal(algos.mode([1]), exp)
22962283

22972284
exp = Series(["a", "b", "c"], dtype=object)
22982285
tm.assert_series_equal(algos.mode(["a", "b", "c"]), exp)
22992286

2300-
def test_number_mode(self):
2287+
@pytest.mark.parametrize("dt", np.typecodes["AllInteger"] + np.typecodes["Float"])
2288+
def test_number_mode(self, dt):
23012289
exp_single = [1]
23022290
data_single = [1] * 5 + [2] * 3
23032291

23042292
exp_multi = [1, 3]
23052293
data_multi = [1] * 5 + [2] * 3 + [3] * 5
23062294

2307-
for dt in np.typecodes["AllInteger"] + np.typecodes["Float"]:
2308-
s = Series(data_single, dtype=dt)
2309-
exp = Series(exp_single, dtype=dt)
2310-
tm.assert_series_equal(algos.mode(s), exp)
2295+
s = Series(data_single, dtype=dt)
2296+
exp = Series(exp_single, dtype=dt)
2297+
tm.assert_series_equal(algos.mode(s), exp)
23112298

2312-
s = Series(data_multi, dtype=dt)
2313-
exp = Series(exp_multi, dtype=dt)
2314-
tm.assert_series_equal(algos.mode(s), exp)
2299+
s = Series(data_multi, dtype=dt)
2300+
exp = Series(exp_multi, dtype=dt)
2301+
tm.assert_series_equal(algos.mode(s), exp)
23152302

23162303
def test_strobj_mode(self):
23172304
exp = ["b"]
@@ -2321,13 +2308,14 @@ def test_strobj_mode(self):
23212308
exp = Series(exp, dtype="c")
23222309
tm.assert_series_equal(algos.mode(s), exp)
23232310

2311+
@pytest.mark.parametrize("dt", [str, object])
2312+
def test_strobj_multi_char(self, dt):
23242313
exp = ["bar"]
23252314
data = ["foo"] * 2 + ["bar"] * 3
23262315

2327-
for dt in [str, object]:
2328-
s = Series(data, dtype=dt)
2329-
exp = Series(exp, dtype=dt)
2330-
tm.assert_series_equal(algos.mode(s), exp)
2316+
s = Series(data, dtype=dt)
2317+
exp = Series(exp, dtype=dt)
2318+
tm.assert_series_equal(algos.mode(s), exp)
23312319

23322320
def test_datelike_mode(self):
23332321
exp = Series(["1900-05-03", "2011-01-03", "2013-01-02"], dtype="M8[ns]")

0 commit comments

Comments
 (0)