Skip to content

TST/CLN: Test parametrizations 2 #56738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/tests/indexes/base_class/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ def test_intersection_str_dates(self, sort):

@pytest.mark.parametrize(
"index2,expected_arr",
[(Index(["B", "D"]), ["B"]), (Index(["B", "D", "A"]), ["A", "B"])],
[(["B", "D"], ["B"]), (["B", "D", "A"], ["A", "B"])],
)
def test_intersection_non_monotonic_non_unique(self, index2, expected_arr, sort):
# non-monotonic non-unique
index1 = Index(["A", "B", "A", "C"])
expected = Index(expected_arr)
result = index1.intersection(index2, sort=sort)
result = index1.intersection(Index(index2), sort=sort)
if sort is None:
expected = expected.sort_values()
tm.assert_index_equal(result, expected)
Expand Down
12 changes: 4 additions & 8 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,17 +526,13 @@ def test_range_tz_pytz(self):
@pytest.mark.parametrize(
"start, end",
[
[
Timestamp(datetime(2014, 3, 6), tz="US/Eastern"),
Timestamp(datetime(2014, 3, 12), tz="US/Eastern"),
],
[
Timestamp(datetime(2013, 11, 1), tz="US/Eastern"),
Timestamp(datetime(2013, 11, 6), tz="US/Eastern"),
],
[datetime(2014, 3, 6), datetime(2014, 3, 12)],
[datetime(2013, 11, 1), datetime(2013, 11, 6)],
],
)
def test_range_tz_dst_straddle_pytz(self, start, end):
start = Timestamp(start, tz="US/Eastern")
end = Timestamp(end, tz="US/Eastern")
dr = date_range(start, end, freq="D")
assert dr[0] == start
assert dr[-1] == end
Expand Down
19 changes: 8 additions & 11 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,23 +789,20 @@ def test_is_overlapping(self, start, shift, na_value, closed):
@pytest.mark.parametrize(
"tuples",
[
list(zip(range(10), range(1, 11))),
list(
zip(
date_range("20170101", periods=10),
date_range("20170101", periods=10),
)
zip(range(10), range(1, 11)),
zip(
date_range("20170101", periods=10),
date_range("20170101", periods=10),
),
list(
zip(
timedelta_range("0 days", periods=10),
timedelta_range("1 day", periods=10),
)
zip(
timedelta_range("0 days", periods=10),
timedelta_range("1 day", periods=10),
),
],
)
def test_to_tuples(self, tuples):
# GH 18756
tuples = list(tuples)
idx = IntervalIndex.from_tuples(tuples)
result = idx.to_tuples()
expected = Index(com.asarray_tuplesafe(tuples))
Expand Down
18 changes: 6 additions & 12 deletions pandas/tests/indexes/multi/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,14 @@ def f(a):
@pytest.mark.parametrize(
"keep, expected",
[
("first", np.array([False, False, False, True, True, False])),
("last", np.array([False, True, True, False, False, False])),
(False, np.array([False, True, True, True, True, False])),
("first", [False, False, False, True, True, False]),
("last", [False, True, True, False, False, False]),
(False, [False, True, True, True, True, False]),
],
)
def test_duplicated(idx_dup, keep, expected):
result = idx_dup.duplicated(keep=keep)
expected = np.array(expected)
tm.assert_numpy_array_equal(result, expected)


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


@pytest.mark.parametrize(
"dtype",
[
np.complex64,
np.complex128,
],
)
def test_duplicated_series_complex_numbers(dtype):
def test_duplicated_series_complex_numbers(complex_dtype):
# GH 17927
expected = Series(
[False, False, False, True, False, False, False, True, False, True],
Expand All @@ -345,7 +339,7 @@ def test_duplicated_series_complex_numbers(dtype):
np.nan,
np.nan + np.nan * 1j,
],
dtype=dtype,
dtype=complex_dtype,
).duplicated()
tm.assert_series_equal(result, expected)

Expand Down
17 changes: 8 additions & 9 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,27 +560,26 @@ def test_getitem_group_select(idx):
assert sorted_idx.get_loc("foo") == slice(0, 2)


@pytest.mark.parametrize("ind1", [[True] * 5, Index([True] * 5)])
@pytest.mark.parametrize(
"ind2",
[[True, False, True, False, False], Index([True, False, True, False, False])],
)
def test_getitem_bool_index_all(ind1, ind2):
@pytest.mark.parametrize("box", [list, Index])
def test_getitem_bool_index_all(box):
# GH#22533
ind1 = box([True] * 5)
idx = MultiIndex.from_tuples([(10, 1), (20, 2), (30, 3), (40, 4), (50, 5)])
tm.assert_index_equal(idx[ind1], idx)

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


@pytest.mark.parametrize("ind1", [[True], Index([True])])
@pytest.mark.parametrize("ind2", [[False], Index([False])])
def test_getitem_bool_index_single(ind1, ind2):
@pytest.mark.parametrize("box", [list, Index])
def test_getitem_bool_index_single(box):
# GH#22533
ind1 = box([True])
idx = MultiIndex.from_tuples([(10, 1)])
tm.assert_index_equal(idx[ind1], idx)

ind2 = box([False])
expected = MultiIndex(
levels=[np.array([], dtype=np.int64), np.array([], dtype=np.int64)],
codes=[[], []],
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/indexes/multi/test_isin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ def test_isin_level_kwarg():
@pytest.mark.parametrize(
"labels,expected,level",
[
([("b", np.nan)], np.array([False, False, True]), None),
([np.nan, "a"], np.array([True, True, False]), 0),
(["d", np.nan], np.array([False, True, True]), 1),
([("b", np.nan)], [False, False, True], None),
([np.nan, "a"], [True, True, False], 0),
(["d", np.nan], [False, True, True], 1),
],
)
def test_isin_multi_index_with_missing_value(labels, expected, level):
# GH 19132
midx = MultiIndex.from_arrays([[np.nan, "a", "b"], ["c", "d", np.nan]])
result = midx.isin(labels, level=level)
expected = np.array(expected)
tm.assert_numpy_array_equal(result, expected)


Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import pandas._testing as tm


@pytest.mark.parametrize(
"other", [Index(["three", "one", "two"]), Index(["one"]), Index(["one", "three"])]
)
@pytest.mark.parametrize("other", [["three", "one", "two"], ["one"], ["one", "three"]])
def test_join_level(idx, other, join_type):
other = Index(other)
join_index, lidx, ridx = other.join(
idx, how=join_type, level="second", return_indexers=True
)
Expand Down
12 changes: 3 additions & 9 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,17 +711,11 @@ def test_intersection_lexsort_depth(levels1, levels2, codes1, codes2, names):
"a",
[pd.Categorical(["a", "b"], categories=["a", "b"]), ["a", "b"]],
)
@pytest.mark.parametrize(
"b",
[
pd.Categorical(["a", "b"], categories=["b", "a"], ordered=True),
pd.Categorical(["a", "b"], categories=["b", "a"]),
],
)
def test_intersection_with_non_lex_sorted_categories(a, b):
@pytest.mark.parametrize("b_ordered", [True, False])
def test_intersection_with_non_lex_sorted_categories(a, b_ordered):
# GH#49974
other = ["1", "2"]

b = pd.Categorical(["a", "b"], categories=["b", "a"], ordered=b_ordered)
df1 = DataFrame({"x": a, "y": other})
df2 = DataFrame({"x": b, "y": other})

Expand Down
21 changes: 10 additions & 11 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ def test_get_indexer(self):
@pytest.mark.parametrize(
"expected,method",
[
(np.array([-1, 0, 0, 1, 1], dtype=np.intp), "pad"),
(np.array([-1, 0, 0, 1, 1], dtype=np.intp), "ffill"),
(np.array([0, 0, 1, 1, 2], dtype=np.intp), "backfill"),
(np.array([0, 0, 1, 1, 2], dtype=np.intp), "bfill"),
([-1, 0, 0, 1, 1], "pad"),
([-1, 0, 0, 1, 1], "ffill"),
([0, 0, 1, 1, 2], "backfill"),
([0, 0, 1, 1, 2], "bfill"),
],
)
def test_get_indexer_methods(self, reverse, expected, method):
index1 = Index([1, 2, 3, 4, 5])
index2 = Index([2, 4, 6])

expected = np.array(expected, dtype=np.intp)
if reverse:
index1 = index1[::-1]
expected = expected[::-1]
Expand Down Expand Up @@ -166,12 +166,11 @@ def test_get_indexer_nearest(self, method, tolerance, indexer, expected):
@pytest.mark.parametrize("listtype", [list, tuple, Series, np.array])
@pytest.mark.parametrize(
"tolerance, expected",
list(
zip(
[[0.3, 0.3, 0.1], [0.2, 0.1, 0.1], [0.1, 0.5, 0.5]],
[[0, 2, -1], [0, -1, -1], [-1, 2, 9]],
)
),
[
[[0.3, 0.3, 0.1], [0, 2, -1]],
[[0.2, 0.1, 0.1], [0, -1, -1]],
[[0.1, 0.5, 0.5], [-1, 2, 9]],
],
)
def test_get_indexer_nearest_listlike_tolerance(
self, tolerance, expected, listtype
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/indexes/numeric/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ def test_intersection_uint64_outside_int64_range(self):
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"index2,keeps_name",
"index2_name,keeps_name",
[
(Index([4, 7, 6, 5, 3], name="index"), True),
(Index([4, 7, 6, 5, 3], name="other"), False),
("index", True),
("other", False),
],
)
def test_intersection_monotonic(self, index2, keeps_name, sort):
def test_intersection_monotonic(self, index2_name, keeps_name, sort):
index2 = Index([4, 7, 6, 5, 3], name=index2_name)
index1 = Index([5, 3, 2, 4, 1], name="index")
expected = Index([5, 3, 4])

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/object/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class TestGetIndexer:
@pytest.mark.parametrize(
"method,expected",
[
("pad", np.array([-1, 0, 1, 1], dtype=np.intp)),
("backfill", np.array([0, 0, 1, -1], dtype=np.intp)),
("pad", [-1, 0, 1, 1]),
("backfill", [0, 0, 1, -1]),
],
)
def test_get_indexer_strings(self, method, expected):
expected = np.array(expected, dtype=np.intp)
index = Index(["b", "c"])
actual = index.get_indexer(["a", "b", "c", "d"], method=method)

Expand Down
24 changes: 12 additions & 12 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,12 +844,14 @@ def test_is_monotonic_incomparable(self, attr):
@pytest.mark.parametrize(
"index,expected",
[
(Index(["qux", "baz", "foo", "bar"]), np.array([False, False, True, True])),
(Index([]), np.array([], dtype=bool)), # empty
(["qux", "baz", "foo", "bar"], [False, False, True, True]),
([], []), # empty
],
)
def test_isin(self, values, index, expected):
index = Index(index)
result = index.isin(values)
expected = np.array(expected, dtype=bool)
tm.assert_numpy_array_equal(result, expected)

def test_isin_nan_common_object(
Expand Down Expand Up @@ -918,11 +920,12 @@ def test_isin_nan_common_float64(self, nulls_fixture, float_numpy_dtype):
@pytest.mark.parametrize(
"index",
[
Index(["qux", "baz", "foo", "bar"]),
Index([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
["qux", "baz", "foo", "bar"],
np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
],
)
def test_isin_level_kwarg(self, level, index):
index = Index(index)
values = index.tolist()[-2:] + ["nonexisting"]

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

@pytest.mark.parametrize(
"index,expected", [(Index(list("abcd")), True), (Index(range(4)), False)]
"index,expected", [(list("abcd"), True), (range(4), False)]
)
def test_tab_completion(self, index, expected):
# GH 9910
index = Index(index)
result = "str" in dir(index)
assert result == expected

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

@pytest.mark.parametrize(
"labels,dtype",
[
(DatetimeIndex([]), np.datetime64),
],
)
def test_reindex_doesnt_preserve_type_if_target_is_empty_index(self, labels, dtype):
def test_reindex_doesnt_preserve_type_if_target_is_empty_index(self):
# GH7774
index = Index(list("abc"))
labels = DatetimeIndex([])
dtype = np.datetime64
assert index.reindex(labels)[0].dtype.type == dtype

def test_reindex_doesnt_preserve_type_if_target_is_empty_index_numeric(
Expand Down
15 changes: 6 additions & 9 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,10 @@ def test_construction_list_tuples_nan(self, na_value, vtype):
expected = MultiIndex.from_tuples(values)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"dtype",
[int, "int64", "int32", "int16", "int8", "uint64", "uint32", "uint16", "uint8"],
)
def test_constructor_int_dtype_float(self, dtype):
def test_constructor_int_dtype_float(self, any_int_numpy_dtype):
# GH#18400
expected = Index([0, 1, 2, 3], dtype=dtype)
result = Index([0.0, 1.0, 2.0, 3.0], dtype=dtype)
expected = Index([0, 1, 2, 3], dtype=any_int_numpy_dtype)
result = Index([0.0, 1.0, 2.0, 3.0], dtype=any_int_numpy_dtype)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("cast_index", [True, False])
Expand Down Expand Up @@ -332,11 +328,12 @@ def test_constructor_dtypes_to_categorical(self, vals):
@pytest.mark.parametrize(
"vals",
[
Index(np.array([np.datetime64("2011-01-01"), np.datetime64("2011-01-02")])),
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)]),
np.array([np.datetime64("2011-01-01"), np.datetime64("2011-01-02")]),
[datetime(2011, 1, 1), datetime(2011, 1, 2)],
],
)
def test_constructor_dtypes_to_datetime(self, cast_index, vals):
vals = Index(vals)
if cast_index:
index = Index(vals, dtype=object)
assert isinstance(index, Index)
Expand Down
Loading