Skip to content

Commit 2542674

Browse files
authored
TST/CLN: Reuse more existing fixtures (pandas-dev#56709)
1 parent cc2f1a6 commit 2542674

File tree

7 files changed

+57
-71
lines changed

7 files changed

+57
-71
lines changed

pandas/tests/arrays/test_timedeltas.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,17 @@ def test_add_timedeltaarraylike(self, tda):
194194

195195

196196
class TestTimedeltaArray:
197-
@pytest.mark.parametrize("dtype", [int, np.int32, np.int64, "uint32", "uint64"])
198-
def test_astype_int(self, dtype):
197+
def test_astype_int(self, any_int_numpy_dtype):
199198
arr = TimedeltaArray._from_sequence(
200199
[Timedelta("1h"), Timedelta("2h")], dtype="m8[ns]"
201200
)
202201

203-
if np.dtype(dtype) != np.int64:
202+
if np.dtype(any_int_numpy_dtype) != np.int64:
204203
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
205-
arr.astype(dtype)
204+
arr.astype(any_int_numpy_dtype)
206205
return
207206

208-
result = arr.astype(dtype)
207+
result = arr.astype(any_int_numpy_dtype)
209208
expected = arr._ndarray.view("i8")
210209
tm.assert_numpy_array_equal(result, expected)
211210

pandas/tests/computation/test_eval.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,10 @@ def test_unary_in_array(self):
606606
)
607607
tm.assert_numpy_array_equal(result, expected)
608608

609-
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
610609
@pytest.mark.parametrize("expr", ["x < -0.1", "-5 > x"])
611-
def test_float_comparison_bin_op(self, dtype, expr):
610+
def test_float_comparison_bin_op(self, float_numpy_dtype, expr):
612611
# GH 16363
613-
df = DataFrame({"x": np.array([0], dtype=dtype)})
612+
df = DataFrame({"x": np.array([0], dtype=float_numpy_dtype)})
614613
res = df.eval(expr)
615614
assert res.values == np.array([False])
616615

@@ -747,15 +746,16 @@ class TestTypeCasting:
747746
@pytest.mark.parametrize("op", ["+", "-", "*", "**", "/"])
748747
# maybe someday... numexpr has too many upcasting rules now
749748
# chain(*(np.core.sctypes[x] for x in ['uint', 'int', 'float']))
750-
@pytest.mark.parametrize("dt", [np.float32, np.float64])
751749
@pytest.mark.parametrize("left_right", [("df", "3"), ("3", "df")])
752-
def test_binop_typecasting(self, engine, parser, op, dt, left_right):
753-
df = DataFrame(np.random.default_rng(2).standard_normal((5, 3)), dtype=dt)
750+
def test_binop_typecasting(self, engine, parser, op, float_numpy_dtype, left_right):
751+
df = DataFrame(
752+
np.random.default_rng(2).standard_normal((5, 3)), dtype=float_numpy_dtype
753+
)
754754
left, right = left_right
755755
s = f"{left} {op} {right}"
756756
res = pd.eval(s, engine=engine, parser=parser)
757-
assert df.values.dtype == dt
758-
assert res.values.dtype == dt
757+
assert df.values.dtype == float_numpy_dtype
758+
assert res.values.dtype == float_numpy_dtype
759759
tm.assert_frame_equal(res, eval(s))
760760

761761

pandas/tests/frame/indexing/test_indexing.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -1682,16 +1682,15 @@ def exp_single_cats_value(self):
16821682
)
16831683
return exp_single_cats_value
16841684

1685-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
1686-
def test_loc_iloc_setitem_list_of_lists(self, orig, indexer):
1685+
def test_loc_iloc_setitem_list_of_lists(self, orig, indexer_li):
16871686
# - assign multiple rows (mixed values) -> exp_multi_row
16881687
df = orig.copy()
16891688

16901689
key = slice(2, 4)
1691-
if indexer is tm.loc:
1690+
if indexer_li is tm.loc:
16921691
key = slice("j", "k")
16931692

1694-
indexer(df)[key, :] = [["b", 2], ["b", 2]]
1693+
indexer_li(df)[key, :] = [["b", 2], ["b", 2]]
16951694

16961695
cats2 = Categorical(["a", "a", "b", "b", "a", "a", "a"], categories=["a", "b"])
16971696
idx2 = Index(["h", "i", "j", "k", "l", "m", "n"])
@@ -1701,7 +1700,7 @@ def test_loc_iloc_setitem_list_of_lists(self, orig, indexer):
17011700

17021701
df = orig.copy()
17031702
with pytest.raises(TypeError, match=msg1):
1704-
indexer(df)[key, :] = [["c", 2], ["c", 2]]
1703+
indexer_li(df)[key, :] = [["c", 2], ["c", 2]]
17051704

17061705
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc, tm.at, tm.iat])
17071706
def test_loc_iloc_at_iat_setitem_single_value_in_categories(
@@ -1722,32 +1721,30 @@ def test_loc_iloc_at_iat_setitem_single_value_in_categories(
17221721
with pytest.raises(TypeError, match=msg1):
17231722
indexer(df)[key] = "c"
17241723

1725-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
17261724
def test_loc_iloc_setitem_mask_single_value_in_categories(
1727-
self, orig, exp_single_cats_value, indexer
1725+
self, orig, exp_single_cats_value, indexer_li
17281726
):
17291727
# mask with single True
17301728
df = orig.copy()
17311729

17321730
mask = df.index == "j"
17331731
key = 0
1734-
if indexer is tm.loc:
1732+
if indexer_li is tm.loc:
17351733
key = df.columns[key]
17361734

1737-
indexer(df)[mask, key] = "b"
1735+
indexer_li(df)[mask, key] = "b"
17381736
tm.assert_frame_equal(df, exp_single_cats_value)
17391737

1740-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
1741-
def test_loc_iloc_setitem_full_row_non_categorical_rhs(self, orig, indexer):
1738+
def test_loc_iloc_setitem_full_row_non_categorical_rhs(self, orig, indexer_li):
17421739
# - assign a complete row (mixed values) -> exp_single_row
17431740
df = orig.copy()
17441741

17451742
key = 2
1746-
if indexer is tm.loc:
1743+
if indexer_li is tm.loc:
17471744
key = df.index[2]
17481745

17491746
# not categorical dtype, but "b" _is_ among the categories for df["cat"]
1750-
indexer(df)[key, :] = ["b", 2]
1747+
indexer_li(df)[key, :] = ["b", 2]
17511748
cats1 = Categorical(["a", "a", "b", "a", "a", "a", "a"], categories=["a", "b"])
17521749
idx1 = Index(["h", "i", "j", "k", "l", "m", "n"])
17531750
values1 = [1, 1, 2, 1, 1, 1, 1]
@@ -1756,56 +1753,54 @@ def test_loc_iloc_setitem_full_row_non_categorical_rhs(self, orig, indexer):
17561753

17571754
# "c" is not among the categories for df["cat"]
17581755
with pytest.raises(TypeError, match=msg1):
1759-
indexer(df)[key, :] = ["c", 2]
1756+
indexer_li(df)[key, :] = ["c", 2]
17601757

1761-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
17621758
def test_loc_iloc_setitem_partial_col_categorical_rhs(
1763-
self, orig, exp_parts_cats_col, indexer
1759+
self, orig, exp_parts_cats_col, indexer_li
17641760
):
17651761
# assign a part of a column with dtype == categorical ->
17661762
# exp_parts_cats_col
17671763
df = orig.copy()
17681764

17691765
key = (slice(2, 4), 0)
1770-
if indexer is tm.loc:
1766+
if indexer_li is tm.loc:
17711767
key = (slice("j", "k"), df.columns[0])
17721768

17731769
# same categories as we currently have in df["cats"]
17741770
compat = Categorical(["b", "b"], categories=["a", "b"])
1775-
indexer(df)[key] = compat
1771+
indexer_li(df)[key] = compat
17761772
tm.assert_frame_equal(df, exp_parts_cats_col)
17771773

17781774
# categories do not match df["cat"]'s, but "b" is among them
17791775
semi_compat = Categorical(list("bb"), categories=list("abc"))
17801776
with pytest.raises(TypeError, match=msg2):
17811777
# different categories but holdable values
17821778
# -> not sure if this should fail or pass
1783-
indexer(df)[key] = semi_compat
1779+
indexer_li(df)[key] = semi_compat
17841780

17851781
# categories do not match df["cat"]'s, and "c" is not among them
17861782
incompat = Categorical(list("cc"), categories=list("abc"))
17871783
with pytest.raises(TypeError, match=msg2):
17881784
# different values
1789-
indexer(df)[key] = incompat
1785+
indexer_li(df)[key] = incompat
17901786

1791-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
17921787
def test_loc_iloc_setitem_non_categorical_rhs(
1793-
self, orig, exp_parts_cats_col, indexer
1788+
self, orig, exp_parts_cats_col, indexer_li
17941789
):
17951790
# assign a part of a column with dtype != categorical -> exp_parts_cats_col
17961791
df = orig.copy()
17971792

17981793
key = (slice(2, 4), 0)
1799-
if indexer is tm.loc:
1794+
if indexer_li is tm.loc:
18001795
key = (slice("j", "k"), df.columns[0])
18011796

18021797
# "b" is among the categories for df["cat"]
1803-
indexer(df)[key] = ["b", "b"]
1798+
indexer_li(df)[key] = ["b", "b"]
18041799
tm.assert_frame_equal(df, exp_parts_cats_col)
18051800

18061801
# "c" not part of the categories
18071802
with pytest.raises(TypeError, match=msg1):
1808-
indexer(df)[key] = ["c", "c"]
1803+
indexer_li(df)[key] = ["c", "c"]
18091804

18101805
@pytest.mark.parametrize("indexer", [tm.getitem, tm.loc, tm.iloc])
18111806
def test_getitem_preserve_object_index_with_dates(self, indexer):

pandas/tests/frame/indexing/test_setitem.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -1000,13 +1000,12 @@ def test_setitem_slice_position(self):
10001000
expected = DataFrame(arr)
10011001
tm.assert_frame_equal(df, expected)
10021002

1003-
@pytest.mark.parametrize("indexer", [tm.setitem, tm.iloc])
10041003
@pytest.mark.parametrize("box", [Series, np.array, list, pd.array])
10051004
@pytest.mark.parametrize("n", [1, 2, 3])
1006-
def test_setitem_slice_indexer_broadcasting_rhs(self, n, box, indexer):
1005+
def test_setitem_slice_indexer_broadcasting_rhs(self, n, box, indexer_si):
10071006
# GH#40440
10081007
df = DataFrame([[1, 3, 5]] + [[2, 4, 6]] * n, columns=["a", "b", "c"])
1009-
indexer(df)[1:] = box([10, 11, 12])
1008+
indexer_si(df)[1:] = box([10, 11, 12])
10101009
expected = DataFrame([[1, 3, 5]] + [[10, 11, 12]] * n, columns=["a", "b", "c"])
10111010
tm.assert_frame_equal(df, expected)
10121011

@@ -1019,15 +1018,14 @@ def test_setitem_list_indexer_broadcasting_rhs(self, n, box):
10191018
expected = DataFrame([[1, 3, 5]] + [[10, 11, 12]] * n, columns=["a", "b", "c"])
10201019
tm.assert_frame_equal(df, expected)
10211020

1022-
@pytest.mark.parametrize("indexer", [tm.setitem, tm.iloc])
10231021
@pytest.mark.parametrize("box", [Series, np.array, list, pd.array])
10241022
@pytest.mark.parametrize("n", [1, 2, 3])
1025-
def test_setitem_slice_broadcasting_rhs_mixed_dtypes(self, n, box, indexer):
1023+
def test_setitem_slice_broadcasting_rhs_mixed_dtypes(self, n, box, indexer_si):
10261024
# GH#40440
10271025
df = DataFrame(
10281026
[[1, 3, 5], ["x", "y", "z"]] + [[2, 4, 6]] * n, columns=["a", "b", "c"]
10291027
)
1030-
indexer(df)[1:] = box([10, 11, 12])
1028+
indexer_si(df)[1:] = box([10, 11, 12])
10311029
expected = DataFrame(
10321030
[[1, 3, 5]] + [[10, 11, 12]] * (n + 1),
10331031
columns=["a", "b", "c"],
@@ -1105,13 +1103,12 @@ def test_setitem_loc_only_false_indexer_dtype_changed(self, box):
11051103
df.loc[indexer, ["b"]] = 9
11061104
tm.assert_frame_equal(df, expected)
11071105

1108-
@pytest.mark.parametrize("indexer", [tm.setitem, tm.loc])
1109-
def test_setitem_boolean_mask_aligning(self, indexer):
1106+
def test_setitem_boolean_mask_aligning(self, indexer_sl):
11101107
# GH#39931
11111108
df = DataFrame({"a": [1, 4, 2, 3], "b": [5, 6, 7, 8]})
11121109
expected = df.copy()
11131110
mask = df["a"] >= 3
1114-
indexer(df)[mask] = indexer(df)[mask].sort_values("a")
1111+
indexer_sl(df)[mask] = indexer_sl(df)[mask].sort_values("a")
11151112
tm.assert_frame_equal(df, expected)
11161113

11171114
def test_setitem_mask_categorical(self):

pandas/tests/frame/methods/test_astype.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,16 @@ def test_astype_with_view_mixed_float(self, mixed_float_frame):
134134
tf.astype(np.int64)
135135
tf.astype(np.float32)
136136

137-
@pytest.mark.parametrize("dtype", [np.int32, np.int64])
138137
@pytest.mark.parametrize("val", [np.nan, np.inf])
139-
def test_astype_cast_nan_inf_int(self, val, dtype):
138+
def test_astype_cast_nan_inf_int(self, val, any_int_numpy_dtype):
140139
# see GH#14265
141140
#
142141
# Check NaN and inf --> raise error when converting to int.
143142
msg = "Cannot convert non-finite values \\(NA or inf\\) to integer"
144143
df = DataFrame([val])
145144

146145
with pytest.raises(ValueError, match=msg):
147-
df.astype(dtype)
146+
df.astype(any_int_numpy_dtype)
148147

149148
def test_astype_str(self):
150149
# see GH#9757
@@ -323,9 +322,9 @@ def test_astype_categoricaldtype_class_raises(self, cls):
323322
with pytest.raises(TypeError, match=xpr):
324323
df["A"].astype(cls)
325324

326-
@pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"])
327-
def test_astype_extension_dtypes(self, dtype):
325+
def test_astype_extension_dtypes(self, any_int_ea_dtype):
328326
# GH#22578
327+
dtype = any_int_ea_dtype
329328
df = DataFrame([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], columns=["a", "b"])
330329

331330
expected1 = DataFrame(
@@ -348,9 +347,9 @@ def test_astype_extension_dtypes(self, dtype):
348347
tm.assert_frame_equal(df.astype(dtype), expected1)
349348
tm.assert_frame_equal(df.astype("int64").astype(dtype), expected1)
350349

351-
@pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"])
352-
def test_astype_extension_dtypes_1d(self, dtype):
350+
def test_astype_extension_dtypes_1d(self, any_int_ea_dtype):
353351
# GH#22578
352+
dtype = any_int_ea_dtype
354353
df = DataFrame({"a": [1.0, 2.0, 3.0]})
355354

356355
expected1 = DataFrame({"a": pd.array([1, 2, 3], dtype=dtype)})
@@ -433,14 +432,13 @@ def test_astype_from_datetimelike_to_object(self, dtype, unit):
433432
else:
434433
assert result.iloc[0, 0] == Timedelta(1, unit=unit)
435434

436-
@pytest.mark.parametrize("arr_dtype", [np.int64, np.float64])
437435
@pytest.mark.parametrize("dtype", ["M8", "m8"])
438436
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "h", "m", "D"])
439-
def test_astype_to_datetimelike_unit(self, arr_dtype, dtype, unit):
437+
def test_astype_to_datetimelike_unit(self, any_real_numpy_dtype, dtype, unit):
440438
# tests all units from numeric origination
441439
# GH#19223 / GH#12425
442440
dtype = f"{dtype}[{unit}]"
443-
arr = np.array([[1, 2, 3]], dtype=arr_dtype)
441+
arr = np.array([[1, 2, 3]], dtype=any_real_numpy_dtype)
444442
df = DataFrame(arr)
445443
result = df.astype(dtype)
446444
expected = DataFrame(arr.astype(dtype))

pandas/tests/frame/test_arithmetic.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ def test_df_string_comparison(self):
304304

305305
class TestFrameFlexComparisons:
306306
# TODO: test_bool_flex_frame needs a better name
307-
@pytest.mark.parametrize("op", ["eq", "ne", "gt", "lt", "ge", "le"])
308-
def test_bool_flex_frame(self, op):
307+
def test_bool_flex_frame(self, comparison_op):
309308
data = np.random.default_rng(2).standard_normal((5, 3))
310309
other_data = np.random.default_rng(2).standard_normal((5, 3))
311310
df = DataFrame(data)
@@ -315,8 +314,8 @@ def test_bool_flex_frame(self, op):
315314
# DataFrame
316315
assert df.eq(df).values.all()
317316
assert not df.ne(df).values.any()
318-
f = getattr(df, op)
319-
o = getattr(operator, op)
317+
f = getattr(df, comparison_op.__name__)
318+
o = comparison_op
320319
# No NAs
321320
tm.assert_frame_equal(f(other), o(df, other))
322321
# Unaligned
@@ -459,25 +458,23 @@ def test_flex_comparison_nat(self):
459458
result = df.ne(pd.NaT)
460459
assert result.iloc[0, 0].item() is True
461460

462-
@pytest.mark.parametrize("opname", ["eq", "ne", "gt", "lt", "ge", "le"])
463-
def test_df_flex_cmp_constant_return_types(self, opname):
461+
def test_df_flex_cmp_constant_return_types(self, comparison_op):
464462
# GH 15077, non-empty DataFrame
465463
df = DataFrame({"x": [1, 2, 3], "y": [1.0, 2.0, 3.0]})
466464
const = 2
467465

468-
result = getattr(df, opname)(const).dtypes.value_counts()
466+
result = getattr(df, comparison_op.__name__)(const).dtypes.value_counts()
469467
tm.assert_series_equal(
470468
result, Series([2], index=[np.dtype(bool)], name="count")
471469
)
472470

473-
@pytest.mark.parametrize("opname", ["eq", "ne", "gt", "lt", "ge", "le"])
474-
def test_df_flex_cmp_constant_return_types_empty(self, opname):
471+
def test_df_flex_cmp_constant_return_types_empty(self, comparison_op):
475472
# GH 15077 empty DataFrame
476473
df = DataFrame({"x": [1, 2, 3], "y": [1.0, 2.0, 3.0]})
477474
const = 2
478475

479476
empty = df.iloc[:0]
480-
result = getattr(empty, opname)(const).dtypes.value_counts()
477+
result = getattr(empty, comparison_op.__name__)(const).dtypes.value_counts()
481478
tm.assert_series_equal(
482479
result, Series([2], index=[np.dtype(bool)], name="count")
483480
)
@@ -664,11 +661,12 @@ def test_arith_flex_series(self, simple_frame):
664661
tm.assert_frame_equal(df.div(row), df / row)
665662
tm.assert_frame_equal(df.div(col, axis=0), (df.T / col).T)
666663

667-
@pytest.mark.parametrize("dtype", ["int64", "float64"])
668-
def test_arith_flex_series_broadcasting(self, dtype):
664+
def test_arith_flex_series_broadcasting(self, any_real_numpy_dtype):
669665
# broadcasting issue in GH 7325
670-
df = DataFrame(np.arange(3 * 2).reshape((3, 2)), dtype=dtype)
666+
df = DataFrame(np.arange(3 * 2).reshape((3, 2)), dtype=any_real_numpy_dtype)
671667
expected = DataFrame([[np.nan, np.inf], [1.0, 1.5], [1.0, 1.25]])
668+
if any_real_numpy_dtype == "float32":
669+
expected = expected.astype(any_real_numpy_dtype)
672670
result = df.div(df[0], axis="index")
673671
tm.assert_frame_equal(result, expected)
674672

pandas/tests/groupby/transform/test_transform.py

-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ def test_cython_transform_series(op, args, targop):
706706

707707

708708
@pytest.mark.parametrize("op", ["cumprod", "cumsum"])
709-
@pytest.mark.parametrize("skipna", [False, True])
710709
@pytest.mark.parametrize(
711710
"input, exp",
712711
[

0 commit comments

Comments
 (0)