Skip to content

Commit 3b13e8a

Browse files
authored
TST/CLN: Test parametrizations 2 (#56738)
* TST/CLN: Test parametrizations * Fix typos * Fix warnings
1 parent 17cdcd9 commit 3b13e8a

40 files changed

+270
-353
lines changed

pandas/tests/indexes/base_class/test_setops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ def test_intersection_str_dates(self, sort):
149149

150150
@pytest.mark.parametrize(
151151
"index2,expected_arr",
152-
[(Index(["B", "D"]), ["B"]), (Index(["B", "D", "A"]), ["A", "B"])],
152+
[(["B", "D"], ["B"]), (["B", "D", "A"], ["A", "B"])],
153153
)
154154
def test_intersection_non_monotonic_non_unique(self, index2, expected_arr, sort):
155155
# non-monotonic non-unique
156156
index1 = Index(["A", "B", "A", "C"])
157157
expected = Index(expected_arr)
158-
result = index1.intersection(index2, sort=sort)
158+
result = index1.intersection(Index(index2), sort=sort)
159159
if sort is None:
160160
expected = expected.sort_values()
161161
tm.assert_index_equal(result, expected)

pandas/tests/indexes/datetimes/test_date_range.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,13 @@ def test_range_tz_pytz(self):
526526
@pytest.mark.parametrize(
527527
"start, end",
528528
[
529-
[
530-
Timestamp(datetime(2014, 3, 6), tz="US/Eastern"),
531-
Timestamp(datetime(2014, 3, 12), tz="US/Eastern"),
532-
],
533-
[
534-
Timestamp(datetime(2013, 11, 1), tz="US/Eastern"),
535-
Timestamp(datetime(2013, 11, 6), tz="US/Eastern"),
536-
],
529+
[datetime(2014, 3, 6), datetime(2014, 3, 12)],
530+
[datetime(2013, 11, 1), datetime(2013, 11, 6)],
537531
],
538532
)
539533
def test_range_tz_dst_straddle_pytz(self, start, end):
534+
start = Timestamp(start, tz="US/Eastern")
535+
end = Timestamp(end, tz="US/Eastern")
540536
dr = date_range(start, end, freq="D")
541537
assert dr[0] == start
542538
assert dr[-1] == end

pandas/tests/indexes/interval/test_interval.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -789,23 +789,20 @@ def test_is_overlapping(self, start, shift, na_value, closed):
789789
@pytest.mark.parametrize(
790790
"tuples",
791791
[
792-
list(zip(range(10), range(1, 11))),
793-
list(
794-
zip(
795-
date_range("20170101", periods=10),
796-
date_range("20170101", periods=10),
797-
)
792+
zip(range(10), range(1, 11)),
793+
zip(
794+
date_range("20170101", periods=10),
795+
date_range("20170101", periods=10),
798796
),
799-
list(
800-
zip(
801-
timedelta_range("0 days", periods=10),
802-
timedelta_range("1 day", periods=10),
803-
)
797+
zip(
798+
timedelta_range("0 days", periods=10),
799+
timedelta_range("1 day", periods=10),
804800
),
805801
],
806802
)
807803
def test_to_tuples(self, tuples):
808804
# GH 18756
805+
tuples = list(tuples)
809806
idx = IntervalIndex.from_tuples(tuples)
810807
result = idx.to_tuples()
811808
expected = Index(com.asarray_tuplesafe(tuples))

pandas/tests/indexes/multi/test_duplicates.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,14 @@ def f(a):
243243
@pytest.mark.parametrize(
244244
"keep, expected",
245245
[
246-
("first", np.array([False, False, False, True, True, False])),
247-
("last", np.array([False, True, True, False, False, False])),
248-
(False, np.array([False, True, True, True, True, False])),
246+
("first", [False, False, False, True, True, False]),
247+
("last", [False, True, True, False, False, False]),
248+
(False, [False, True, True, True, True, False]),
249249
],
250250
)
251251
def test_duplicated(idx_dup, keep, expected):
252252
result = idx_dup.duplicated(keep=keep)
253+
expected = np.array(expected)
253254
tm.assert_numpy_array_equal(result, expected)
254255

255256

@@ -319,14 +320,7 @@ def test_duplicated_drop_duplicates():
319320
tm.assert_index_equal(idx.drop_duplicates(keep=False), expected)
320321

321322

322-
@pytest.mark.parametrize(
323-
"dtype",
324-
[
325-
np.complex64,
326-
np.complex128,
327-
],
328-
)
329-
def test_duplicated_series_complex_numbers(dtype):
323+
def test_duplicated_series_complex_numbers(complex_dtype):
330324
# GH 17927
331325
expected = Series(
332326
[False, False, False, True, False, False, False, True, False, True],
@@ -345,7 +339,7 @@ def test_duplicated_series_complex_numbers(dtype):
345339
np.nan,
346340
np.nan + np.nan * 1j,
347341
],
348-
dtype=dtype,
342+
dtype=complex_dtype,
349343
).duplicated()
350344
tm.assert_series_equal(result, expected)
351345

pandas/tests/indexes/multi/test_indexing.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -560,27 +560,26 @@ def test_getitem_group_select(idx):
560560
assert sorted_idx.get_loc("foo") == slice(0, 2)
561561

562562

563-
@pytest.mark.parametrize("ind1", [[True] * 5, Index([True] * 5)])
564-
@pytest.mark.parametrize(
565-
"ind2",
566-
[[True, False, True, False, False], Index([True, False, True, False, False])],
567-
)
568-
def test_getitem_bool_index_all(ind1, ind2):
563+
@pytest.mark.parametrize("box", [list, Index])
564+
def test_getitem_bool_index_all(box):
569565
# GH#22533
566+
ind1 = box([True] * 5)
570567
idx = MultiIndex.from_tuples([(10, 1), (20, 2), (30, 3), (40, 4), (50, 5)])
571568
tm.assert_index_equal(idx[ind1], idx)
572569

570+
ind2 = box([True, False, True, False, False])
573571
expected = MultiIndex.from_tuples([(10, 1), (30, 3)])
574572
tm.assert_index_equal(idx[ind2], expected)
575573

576574

577-
@pytest.mark.parametrize("ind1", [[True], Index([True])])
578-
@pytest.mark.parametrize("ind2", [[False], Index([False])])
579-
def test_getitem_bool_index_single(ind1, ind2):
575+
@pytest.mark.parametrize("box", [list, Index])
576+
def test_getitem_bool_index_single(box):
580577
# GH#22533
578+
ind1 = box([True])
581579
idx = MultiIndex.from_tuples([(10, 1)])
582580
tm.assert_index_equal(idx[ind1], idx)
583581

582+
ind2 = box([False])
584583
expected = MultiIndex(
585584
levels=[np.array([], dtype=np.int64), np.array([], dtype=np.int64)],
586585
codes=[[], []],

pandas/tests/indexes/multi/test_isin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ def test_isin_level_kwarg():
7575
@pytest.mark.parametrize(
7676
"labels,expected,level",
7777
[
78-
([("b", np.nan)], np.array([False, False, True]), None),
79-
([np.nan, "a"], np.array([True, True, False]), 0),
80-
(["d", np.nan], np.array([False, True, True]), 1),
78+
([("b", np.nan)], [False, False, True], None),
79+
([np.nan, "a"], [True, True, False], 0),
80+
(["d", np.nan], [False, True, True], 1),
8181
],
8282
)
8383
def test_isin_multi_index_with_missing_value(labels, expected, level):
8484
# GH 19132
8585
midx = MultiIndex.from_arrays([[np.nan, "a", "b"], ["c", "d", np.nan]])
8686
result = midx.isin(labels, level=level)
87+
expected = np.array(expected)
8788
tm.assert_numpy_array_equal(result, expected)
8889

8990

pandas/tests/indexes/multi/test_join.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
import pandas._testing as tm
1313

1414

15-
@pytest.mark.parametrize(
16-
"other", [Index(["three", "one", "two"]), Index(["one"]), Index(["one", "three"])]
17-
)
15+
@pytest.mark.parametrize("other", [["three", "one", "two"], ["one"], ["one", "three"]])
1816
def test_join_level(idx, other, join_type):
17+
other = Index(other)
1918
join_index, lidx, ridx = other.join(
2019
idx, how=join_type, level="second", return_indexers=True
2120
)

pandas/tests/indexes/multi/test_setops.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -711,17 +711,11 @@ def test_intersection_lexsort_depth(levels1, levels2, codes1, codes2, names):
711711
"a",
712712
[pd.Categorical(["a", "b"], categories=["a", "b"]), ["a", "b"]],
713713
)
714-
@pytest.mark.parametrize(
715-
"b",
716-
[
717-
pd.Categorical(["a", "b"], categories=["b", "a"], ordered=True),
718-
pd.Categorical(["a", "b"], categories=["b", "a"]),
719-
],
720-
)
721-
def test_intersection_with_non_lex_sorted_categories(a, b):
714+
@pytest.mark.parametrize("b_ordered", [True, False])
715+
def test_intersection_with_non_lex_sorted_categories(a, b_ordered):
722716
# GH#49974
723717
other = ["1", "2"]
724-
718+
b = pd.Categorical(["a", "b"], categories=["b", "a"], ordered=b_ordered)
725719
df1 = DataFrame({"x": a, "y": other})
726720
df2 = DataFrame({"x": b, "y": other})
727721

pandas/tests/indexes/numeric/test_indexing.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ def test_get_indexer(self):
110110
@pytest.mark.parametrize(
111111
"expected,method",
112112
[
113-
(np.array([-1, 0, 0, 1, 1], dtype=np.intp), "pad"),
114-
(np.array([-1, 0, 0, 1, 1], dtype=np.intp), "ffill"),
115-
(np.array([0, 0, 1, 1, 2], dtype=np.intp), "backfill"),
116-
(np.array([0, 0, 1, 1, 2], dtype=np.intp), "bfill"),
113+
([-1, 0, 0, 1, 1], "pad"),
114+
([-1, 0, 0, 1, 1], "ffill"),
115+
([0, 0, 1, 1, 2], "backfill"),
116+
([0, 0, 1, 1, 2], "bfill"),
117117
],
118118
)
119119
def test_get_indexer_methods(self, reverse, expected, method):
120120
index1 = Index([1, 2, 3, 4, 5])
121121
index2 = Index([2, 4, 6])
122-
122+
expected = np.array(expected, dtype=np.intp)
123123
if reverse:
124124
index1 = index1[::-1]
125125
expected = expected[::-1]
@@ -166,12 +166,11 @@ def test_get_indexer_nearest(self, method, tolerance, indexer, expected):
166166
@pytest.mark.parametrize("listtype", [list, tuple, Series, np.array])
167167
@pytest.mark.parametrize(
168168
"tolerance, expected",
169-
list(
170-
zip(
171-
[[0.3, 0.3, 0.1], [0.2, 0.1, 0.1], [0.1, 0.5, 0.5]],
172-
[[0, 2, -1], [0, -1, -1], [-1, 2, 9]],
173-
)
174-
),
169+
[
170+
[[0.3, 0.3, 0.1], [0, 2, -1]],
171+
[[0.2, 0.1, 0.1], [0, -1, -1]],
172+
[[0.1, 0.5, 0.5], [-1, 2, 9]],
173+
],
175174
)
176175
def test_get_indexer_nearest_listlike_tolerance(
177176
self, tolerance, expected, listtype

pandas/tests/indexes/numeric/test_setops.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ def test_intersection_uint64_outside_int64_range(self):
113113
tm.assert_index_equal(result, expected)
114114

115115
@pytest.mark.parametrize(
116-
"index2,keeps_name",
116+
"index2_name,keeps_name",
117117
[
118-
(Index([4, 7, 6, 5, 3], name="index"), True),
119-
(Index([4, 7, 6, 5, 3], name="other"), False),
118+
("index", True),
119+
("other", False),
120120
],
121121
)
122-
def test_intersection_monotonic(self, index2, keeps_name, sort):
122+
def test_intersection_monotonic(self, index2_name, keeps_name, sort):
123+
index2 = Index([4, 7, 6, 5, 3], name=index2_name)
123124
index1 = Index([5, 3, 2, 4, 1], name="index")
124125
expected = Index([5, 3, 4])
125126

pandas/tests/indexes/object/test_indexing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ class TestGetIndexer:
1818
@pytest.mark.parametrize(
1919
"method,expected",
2020
[
21-
("pad", np.array([-1, 0, 1, 1], dtype=np.intp)),
22-
("backfill", np.array([0, 0, 1, -1], dtype=np.intp)),
21+
("pad", [-1, 0, 1, 1]),
22+
("backfill", [0, 0, 1, -1]),
2323
],
2424
)
2525
def test_get_indexer_strings(self, method, expected):
26+
expected = np.array(expected, dtype=np.intp)
2627
index = Index(["b", "c"])
2728
actual = index.get_indexer(["a", "b", "c", "d"], method=method)
2829

pandas/tests/indexes/test_base.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,14 @@ def test_is_monotonic_incomparable(self, attr):
844844
@pytest.mark.parametrize(
845845
"index,expected",
846846
[
847-
(Index(["qux", "baz", "foo", "bar"]), np.array([False, False, True, True])),
848-
(Index([]), np.array([], dtype=bool)), # empty
847+
(["qux", "baz", "foo", "bar"], [False, False, True, True]),
848+
([], []), # empty
849849
],
850850
)
851851
def test_isin(self, values, index, expected):
852+
index = Index(index)
852853
result = index.isin(values)
854+
expected = np.array(expected, dtype=bool)
853855
tm.assert_numpy_array_equal(result, expected)
854856

855857
def test_isin_nan_common_object(
@@ -918,11 +920,12 @@ def test_isin_nan_common_float64(self, nulls_fixture, float_numpy_dtype):
918920
@pytest.mark.parametrize(
919921
"index",
920922
[
921-
Index(["qux", "baz", "foo", "bar"]),
922-
Index([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
923+
["qux", "baz", "foo", "bar"],
924+
np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
923925
],
924926
)
925927
def test_isin_level_kwarg(self, level, index):
928+
index = Index(index)
926929
values = index.tolist()[-2:] + ["nonexisting"]
927930

928931
expected = np.array([False, False, True, True])
@@ -1078,10 +1081,11 @@ def test_str_bool_series_indexing(self):
10781081
tm.assert_series_equal(result, expected)
10791082

10801083
@pytest.mark.parametrize(
1081-
"index,expected", [(Index(list("abcd")), True), (Index(range(4)), False)]
1084+
"index,expected", [(list("abcd"), True), (range(4), False)]
10821085
)
10831086
def test_tab_completion(self, index, expected):
10841087
# GH 9910
1088+
index = Index(index)
10851089
result = "str" in dir(index)
10861090
assert result == expected
10871091

@@ -1164,15 +1168,11 @@ def test_reindex_preserves_type_if_target_is_empty_list_or_array(self, labels):
11641168
index = Index(list("abc"))
11651169
assert index.reindex(labels)[0].dtype.type == index.dtype.type
11661170

1167-
@pytest.mark.parametrize(
1168-
"labels,dtype",
1169-
[
1170-
(DatetimeIndex([]), np.datetime64),
1171-
],
1172-
)
1173-
def test_reindex_doesnt_preserve_type_if_target_is_empty_index(self, labels, dtype):
1171+
def test_reindex_doesnt_preserve_type_if_target_is_empty_index(self):
11741172
# GH7774
11751173
index = Index(list("abc"))
1174+
labels = DatetimeIndex([])
1175+
dtype = np.datetime64
11761176
assert index.reindex(labels)[0].dtype.type == dtype
11771177

11781178
def test_reindex_doesnt_preserve_type_if_target_is_empty_index_numeric(

pandas/tests/indexes/test_index_new.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,10 @@ def test_construction_list_tuples_nan(self, na_value, vtype):
8080
expected = MultiIndex.from_tuples(values)
8181
tm.assert_index_equal(result, expected)
8282

83-
@pytest.mark.parametrize(
84-
"dtype",
85-
[int, "int64", "int32", "int16", "int8", "uint64", "uint32", "uint16", "uint8"],
86-
)
87-
def test_constructor_int_dtype_float(self, dtype):
83+
def test_constructor_int_dtype_float(self, any_int_numpy_dtype):
8884
# GH#18400
89-
expected = Index([0, 1, 2, 3], dtype=dtype)
90-
result = Index([0.0, 1.0, 2.0, 3.0], dtype=dtype)
85+
expected = Index([0, 1, 2, 3], dtype=any_int_numpy_dtype)
86+
result = Index([0.0, 1.0, 2.0, 3.0], dtype=any_int_numpy_dtype)
9187
tm.assert_index_equal(result, expected)
9288

9389
@pytest.mark.parametrize("cast_index", [True, False])
@@ -332,11 +328,12 @@ def test_constructor_dtypes_to_categorical(self, vals):
332328
@pytest.mark.parametrize(
333329
"vals",
334330
[
335-
Index(np.array([np.datetime64("2011-01-01"), np.datetime64("2011-01-02")])),
336-
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)]),
331+
np.array([np.datetime64("2011-01-01"), np.datetime64("2011-01-02")]),
332+
[datetime(2011, 1, 1), datetime(2011, 1, 2)],
337333
],
338334
)
339335
def test_constructor_dtypes_to_datetime(self, cast_index, vals):
336+
vals = Index(vals)
340337
if cast_index:
341338
index = Index(vals, dtype=object)
342339
assert isinstance(index, Index)

0 commit comments

Comments
 (0)