Skip to content

Commit d711b1d

Browse files
authored
TST/CLN: Test parametrizations (#56737)
1 parent c25ed5d commit d711b1d

22 files changed

+182
-258
lines changed

pandas/tests/arithmetic/test_datetime64.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ class TestDatetime64SeriesComparison:
182182
@pytest.mark.parametrize(
183183
"op, expected",
184184
[
185-
(operator.eq, Series([False, False, True])),
186-
(operator.ne, Series([True, True, False])),
187-
(operator.lt, Series([False, False, False])),
188-
(operator.gt, Series([False, False, False])),
189-
(operator.ge, Series([False, False, True])),
190-
(operator.le, Series([False, False, True])),
185+
(operator.eq, [False, False, True]),
186+
(operator.ne, [True, True, False]),
187+
(operator.lt, [False, False, False]),
188+
(operator.gt, [False, False, False]),
189+
(operator.ge, [False, False, True]),
190+
(operator.le, [False, False, True]),
191191
],
192192
)
193193
def test_nat_comparisons(
@@ -210,7 +210,7 @@ def test_nat_comparisons(
210210

211211
result = op(left, right)
212212

213-
tm.assert_series_equal(result, expected)
213+
tm.assert_series_equal(result, Series(expected))
214214

215215
@pytest.mark.parametrize(
216216
"data",
@@ -1485,11 +1485,10 @@ def test_dt64arr_add_sub_DateOffsets(
14851485
@pytest.mark.parametrize(
14861486
"other",
14871487
[
1488-
np.array([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]),
1489-
np.array([pd.offsets.DateOffset(years=1), pd.offsets.MonthEnd()]),
1490-
np.array( # matching offsets
1491-
[pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(years=1)]
1492-
),
1488+
[pd.offsets.MonthEnd(), pd.offsets.Day(n=2)],
1489+
[pd.offsets.DateOffset(years=1), pd.offsets.MonthEnd()],
1490+
# matching offsets
1491+
[pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(years=1)],
14931492
],
14941493
)
14951494
@pytest.mark.parametrize("op", [operator.add, roperator.radd, operator.sub])
@@ -1502,7 +1501,7 @@ def test_dt64arr_add_sub_offset_array(
15021501
tz = tz_naive_fixture
15031502
dti = date_range("2017-01-01", periods=2, tz=tz)
15041503
dtarr = tm.box_expected(dti, box_with_array)
1505-
1504+
other = np.array(other)
15061505
expected = DatetimeIndex([op(dti[n], other[n]) for n in range(len(dti))])
15071506
expected = tm.box_expected(expected, box_with_array).astype(object)
15081507

pandas/tests/arithmetic/test_timedelta64.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1960,19 +1960,20 @@ def test_td64arr_floordiv_numeric_scalar(self, box_with_array, two):
19601960
two // tdser
19611961

19621962
@pytest.mark.parametrize(
1963-
"vector",
1964-
[np.array([20, 30, 40]), Index([20, 30, 40]), Series([20, 30, 40])],
1965-
ids=lambda x: type(x).__name__,
1963+
"klass",
1964+
[np.array, Index, Series],
1965+
ids=lambda x: x.__name__,
19661966
)
19671967
def test_td64arr_rmul_numeric_array(
19681968
self,
19691969
box_with_array,
1970-
vector,
1970+
klass,
19711971
any_real_numpy_dtype,
19721972
):
19731973
# GH#4521
19741974
# divide/multiply by integers
19751975

1976+
vector = klass([20, 30, 40])
19761977
tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
19771978
vector = vector.astype(any_real_numpy_dtype)
19781979

@@ -1990,16 +1991,17 @@ def test_td64arr_rmul_numeric_array(
19901991
tm.assert_equal(result, expected)
19911992

19921993
@pytest.mark.parametrize(
1993-
"vector",
1994-
[np.array([20, 30, 40]), Index([20, 30, 40]), Series([20, 30, 40])],
1995-
ids=lambda x: type(x).__name__,
1994+
"klass",
1995+
[np.array, Index, Series],
1996+
ids=lambda x: x.__name__,
19961997
)
19971998
def test_td64arr_div_numeric_array(
1998-
self, box_with_array, vector, any_real_numpy_dtype
1999+
self, box_with_array, klass, any_real_numpy_dtype
19992000
):
20002001
# GH#4521
20012002
# divide/multiply by integers
20022003

2004+
vector = klass([20, 30, 40])
20032005
tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
20042006
vector = vector.astype(any_real_numpy_dtype)
20052007

pandas/tests/arrays/categorical/test_indexing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ def test_setitem(self):
5151

5252
tm.assert_categorical_equal(c, expected)
5353

54-
@pytest.mark.parametrize(
55-
"other",
56-
[Categorical(["b", "a"]), Categorical(["b", "a"], categories=["b", "a"])],
57-
)
58-
def test_setitem_same_but_unordered(self, other):
54+
@pytest.mark.parametrize("categories", [None, ["b", "a"]])
55+
def test_setitem_same_but_unordered(self, categories):
5956
# GH-24142
57+
other = Categorical(["b", "a"], categories=categories)
6058
target = Categorical(["a", "b"], categories=["a", "b"])
6159
mask = np.array([True, False])
6260
target[mask] = other[mask]

pandas/tests/arrays/categorical/test_operators.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -307,29 +307,23 @@ def test_comparisons(self, data, reverse, base):
307307
with pytest.raises(TypeError, match=msg):
308308
a < cat_rev
309309

310-
@pytest.mark.parametrize(
311-
"ctor",
312-
[
313-
lambda *args, **kwargs: Categorical(*args, **kwargs),
314-
lambda *args, **kwargs: Series(Categorical(*args, **kwargs)),
315-
],
316-
)
317-
def test_unordered_different_order_equal(self, ctor):
310+
@pytest.mark.parametrize("box", [lambda x: x, Series])
311+
def test_unordered_different_order_equal(self, box):
318312
# https://github.com/pandas-dev/pandas/issues/16014
319-
c1 = ctor(["a", "b"], categories=["a", "b"], ordered=False)
320-
c2 = ctor(["a", "b"], categories=["b", "a"], ordered=False)
313+
c1 = box(Categorical(["a", "b"], categories=["a", "b"], ordered=False))
314+
c2 = box(Categorical(["a", "b"], categories=["b", "a"], ordered=False))
321315
assert (c1 == c2).all()
322316

323-
c1 = ctor(["a", "b"], categories=["a", "b"], ordered=False)
324-
c2 = ctor(["b", "a"], categories=["b", "a"], ordered=False)
317+
c1 = box(Categorical(["a", "b"], categories=["a", "b"], ordered=False))
318+
c2 = box(Categorical(["b", "a"], categories=["b", "a"], ordered=False))
325319
assert (c1 != c2).all()
326320

327-
c1 = ctor(["a", "a"], categories=["a", "b"], ordered=False)
328-
c2 = ctor(["b", "b"], categories=["b", "a"], ordered=False)
321+
c1 = box(Categorical(["a", "a"], categories=["a", "b"], ordered=False))
322+
c2 = box(Categorical(["b", "b"], categories=["b", "a"], ordered=False))
329323
assert (c1 != c2).all()
330324

331-
c1 = ctor(["a", "a"], categories=["a", "b"], ordered=False)
332-
c2 = ctor(["a", "b"], categories=["b", "a"], ordered=False)
325+
c1 = box(Categorical(["a", "a"], categories=["a", "b"], ordered=False))
326+
c2 = box(Categorical(["a", "b"], categories=["b", "a"], ordered=False))
333327
result = c1 == c2
334328
tm.assert_numpy_array_equal(np.array(result), np.array([True, False]))
335329

pandas/tests/arrays/sparse/test_dtype.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ def test_construct_from_string_raises():
9999
@pytest.mark.parametrize(
100100
"dtype, expected",
101101
[
102-
(SparseDtype(int), True),
103-
(SparseDtype(float), True),
104-
(SparseDtype(bool), True),
105-
(SparseDtype(object), False),
106-
(SparseDtype(str), False),
102+
(int, True),
103+
(float, True),
104+
(bool, True),
105+
(object, False),
106+
(str, False),
107107
],
108108
)
109109
def test_is_numeric(dtype, expected):
110-
assert dtype._is_numeric is expected
110+
assert SparseDtype(dtype)._is_numeric is expected
111111

112112

113113
def test_str_uses_object():

pandas/tests/arrays/sparse/test_reductions.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ def test_sum(self):
126126

127127
@pytest.mark.parametrize(
128128
"arr",
129-
[np.array([0, 1, np.nan, 1]), np.array([0, 1, 1])],
129+
[[0, 1, np.nan, 1], [0, 1, 1]],
130130
)
131131
@pytest.mark.parametrize("fill_value", [0, 1, np.nan])
132132
@pytest.mark.parametrize("min_count, expected", [(3, 2), (4, np.nan)])
133133
def test_sum_min_count(self, arr, fill_value, min_count, expected):
134134
# GH#25777
135-
sparray = SparseArray(arr, fill_value=fill_value)
135+
sparray = SparseArray(np.array(arr), fill_value=fill_value)
136136
result = sparray.sum(min_count=min_count)
137137
if np.isnan(expected):
138138
assert np.isnan(result)
@@ -296,11 +296,9 @@ def test_argmax_argmin(self, arr, argmax_expected, argmin_expected):
296296
assert argmax_result == argmax_expected
297297
assert argmin_result == argmin_expected
298298

299-
@pytest.mark.parametrize(
300-
"arr,method",
301-
[(SparseArray([]), "argmax"), (SparseArray([]), "argmin")],
302-
)
303-
def test_empty_array(self, arr, method):
299+
@pytest.mark.parametrize("method", ["argmax", "argmin"])
300+
def test_empty_array(self, method):
304301
msg = f"attempt to get {method} of an empty sequence"
302+
arr = SparseArray([])
305303
with pytest.raises(ValueError, match=msg):
306-
arr.argmax() if method == "argmax" else arr.argmin()
304+
getattr(arr, method)()

pandas/tests/base/test_conversion.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -499,22 +499,23 @@ def test_to_numpy_dataframe_na_value(data, dtype, na_value):
499499

500500

501501
@pytest.mark.parametrize(
502-
"data, expected",
502+
"data, expected_data",
503503
[
504504
(
505505
{"a": pd.array([1, 2, None])},
506-
np.array([[1.0], [2.0], [np.nan]], dtype=float),
506+
[[1.0], [2.0], [np.nan]],
507507
),
508508
(
509509
{"a": [1, 2, 3], "b": [1, 2, 3]},
510-
np.array([[1, 1], [2, 2], [3, 3]], dtype=float),
510+
[[1, 1], [2, 2], [3, 3]],
511511
),
512512
],
513513
)
514-
def test_to_numpy_dataframe_single_block(data, expected):
514+
def test_to_numpy_dataframe_single_block(data, expected_data):
515515
# https://github.com/pandas-dev/pandas/issues/33820
516516
df = pd.DataFrame(data)
517517
result = df.to_numpy(dtype=float, na_value=np.nan)
518+
expected = np.array(expected_data, dtype=float)
518519
tm.assert_numpy_array_equal(result, expected)
519520

520521

pandas/tests/computation/test_eval.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,15 @@ def test_series_negate(self, engine, parser):
522522
"lhs",
523523
[
524524
# Float
525-
DataFrame(np.random.default_rng(2).standard_normal((5, 2))),
525+
np.random.default_rng(2).standard_normal((5, 2)),
526526
# Int
527-
DataFrame(np.random.default_rng(2).integers(5, size=(5, 2))),
527+
np.random.default_rng(2).integers(5, size=(5, 2)),
528528
# bool doesn't work with numexpr but works elsewhere
529-
DataFrame(np.random.default_rng(2).standard_normal((5, 2)) > 0.5),
529+
np.array([True, False, True, False, True], dtype=np.bool_),
530530
],
531531
)
532532
def test_frame_pos(self, lhs, engine, parser):
533+
lhs = DataFrame(lhs)
533534
expr = "+lhs"
534535
expect = lhs
535536

@@ -540,14 +541,15 @@ def test_frame_pos(self, lhs, engine, parser):
540541
"lhs",
541542
[
542543
# Float
543-
Series(np.random.default_rng(2).standard_normal(5)),
544+
np.random.default_rng(2).standard_normal(5),
544545
# Int
545-
Series(np.random.default_rng(2).integers(5, size=5)),
546+
np.random.default_rng(2).integers(5, size=5),
546547
# bool doesn't work with numexpr but works elsewhere
547-
Series(np.random.default_rng(2).standard_normal(5) > 0.5),
548+
np.array([True, False, True, False, True], dtype=np.bool_),
548549
],
549550
)
550551
def test_series_pos(self, lhs, engine, parser):
552+
lhs = Series(lhs)
551553
expr = "+lhs"
552554
expect = lhs
553555

pandas/tests/copy_view/index/test_datetimeindex.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@
1313
)
1414

1515

16-
@pytest.mark.parametrize(
17-
"cons",
18-
[
19-
lambda x: DatetimeIndex(x),
20-
lambda x: DatetimeIndex(DatetimeIndex(x)),
21-
],
22-
)
23-
def test_datetimeindex(using_copy_on_write, cons):
16+
@pytest.mark.parametrize("box", [lambda x: x, DatetimeIndex])
17+
def test_datetimeindex(using_copy_on_write, box):
2418
dt = date_range("2019-12-31", periods=3, freq="D")
2519
ser = Series(dt)
26-
idx = cons(ser)
20+
idx = box(DatetimeIndex(ser))
2721
expected = idx.copy(deep=True)
2822
ser.iloc[0] = Timestamp("2020-12-31")
2923
if using_copy_on_write:

pandas/tests/copy_view/index/test_periodindex.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@
1313
)
1414

1515

16-
@pytest.mark.parametrize(
17-
"cons",
18-
[
19-
lambda x: PeriodIndex(x),
20-
lambda x: PeriodIndex(PeriodIndex(x)),
21-
],
22-
)
23-
def test_periodindex(using_copy_on_write, cons):
16+
@pytest.mark.parametrize("box", [lambda x: x, PeriodIndex])
17+
def test_periodindex(using_copy_on_write, box):
2418
dt = period_range("2019-12-31", periods=3, freq="D")
2519
ser = Series(dt)
26-
idx = cons(ser)
20+
idx = box(PeriodIndex(ser))
2721
expected = idx.copy(deep=True)
2822
ser.iloc[0] = Period("2020-12-31")
2923
if using_copy_on_write:

pandas/tests/dtypes/test_inference.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -1078,15 +1078,15 @@ def test_integers(self):
10781078
@pytest.mark.parametrize(
10791079
"arr, skipna",
10801080
[
1081-
(np.array([1, 2, np.nan, np.nan, 3], dtype="O"), False),
1082-
(np.array([1, 2, np.nan, np.nan, 3], dtype="O"), True),
1083-
(np.array([1, 2, 3, np.int64(4), np.int32(5), np.nan], dtype="O"), False),
1084-
(np.array([1, 2, 3, np.int64(4), np.int32(5), np.nan], dtype="O"), True),
1081+
([1, 2, np.nan, np.nan, 3], False),
1082+
([1, 2, np.nan, np.nan, 3], True),
1083+
([1, 2, 3, np.int64(4), np.int32(5), np.nan], False),
1084+
([1, 2, 3, np.int64(4), np.int32(5), np.nan], True),
10851085
],
10861086
)
10871087
def test_integer_na(self, arr, skipna):
10881088
# GH 27392
1089-
result = lib.infer_dtype(arr, skipna=skipna)
1089+
result = lib.infer_dtype(np.array(arr, dtype="O"), skipna=skipna)
10901090
expected = "integer" if skipna else "integer-na"
10911091
assert result == expected
10921092

@@ -1287,13 +1287,13 @@ def test_infer_dtype_mixed_integer(self):
12871287
@pytest.mark.parametrize(
12881288
"arr",
12891289
[
1290-
np.array([Timestamp("2011-01-01"), Timestamp("2011-01-02")]),
1291-
np.array([datetime(2011, 1, 1), datetime(2012, 2, 1)]),
1292-
np.array([datetime(2011, 1, 1), Timestamp("2011-01-02")]),
1290+
[Timestamp("2011-01-01"), Timestamp("2011-01-02")],
1291+
[datetime(2011, 1, 1), datetime(2012, 2, 1)],
1292+
[datetime(2011, 1, 1), Timestamp("2011-01-02")],
12931293
],
12941294
)
12951295
def test_infer_dtype_datetime(self, arr):
1296-
assert lib.infer_dtype(arr, skipna=True) == "datetime"
1296+
assert lib.infer_dtype(np.array(arr), skipna=True) == "datetime"
12971297

12981298
@pytest.mark.parametrize("na_value", [pd.NaT, np.nan])
12991299
@pytest.mark.parametrize(
@@ -1902,14 +1902,15 @@ def test_is_scalar_numpy_array_scalars(self):
19021902
@pytest.mark.parametrize(
19031903
"zerodim",
19041904
[
1905-
np.array(1),
1906-
np.array("foobar"),
1907-
np.array(np.datetime64("2014-01-01")),
1908-
np.array(np.timedelta64(1, "h")),
1909-
np.array(np.datetime64("NaT")),
1905+
1,
1906+
"foobar",
1907+
np.datetime64("2014-01-01"),
1908+
np.timedelta64(1, "h"),
1909+
np.datetime64("NaT"),
19101910
],
19111911
)
19121912
def test_is_scalar_numpy_zerodim_arrays(self, zerodim):
1913+
zerodim = np.array(zerodim)
19131914
assert not is_scalar(zerodim)
19141915
assert is_scalar(lib.item_from_zerodim(zerodim))
19151916

pandas/tests/frame/indexing/test_get.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ def test_get(self, float_frame):
1515
)
1616

1717
@pytest.mark.parametrize(
18-
"df",
18+
"columns, index",
1919
[
20-
DataFrame(),
21-
DataFrame(columns=list("AB")),
22-
DataFrame(columns=list("AB"), index=range(3)),
20+
[None, None],
21+
[list("AB"), None],
22+
[list("AB"), range(3)],
2323
],
2424
)
25-
def test_get_none(self, df):
25+
def test_get_none(self, columns, index):
2626
# see gh-5652
27-
assert df.get(None) is None
27+
assert DataFrame(columns=columns, index=index).get(None) is None

0 commit comments

Comments
 (0)