Skip to content

Commit b2bca5e

Browse files
authored
TST/CLN: Reuse more fixtures (#56726)
1 parent 8fb8b9f commit b2bca5e

14 files changed

+65
-83
lines changed

pandas/tests/base/test_misc.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
Index,
1818
Series,
1919
)
20-
import pandas._testing as tm
2120

2221

2322
def test_isnull_notnull_docstrings():
@@ -130,9 +129,13 @@ def test_memory_usage_components_series(series_with_simple_index):
130129
assert total_usage == non_index_usage + index_usage
131130

132131

133-
@pytest.mark.parametrize("dtype", tm.NARROW_NP_DTYPES)
134-
def test_memory_usage_components_narrow_series(dtype):
135-
series = Series(range(5), dtype=dtype, index=[f"i-{i}" for i in range(5)], name="a")
132+
def test_memory_usage_components_narrow_series(any_real_numpy_dtype):
133+
series = Series(
134+
range(5),
135+
dtype=any_real_numpy_dtype,
136+
index=[f"i-{i}" for i in range(5)],
137+
name="a",
138+
)
136139
total_usage = series.memory_usage(index=True)
137140
non_index_usage = series.memory_usage(index=False)
138141
index_usage = series.index.memory_usage()

pandas/tests/indexing/test_iloc.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ class TestiLocBaseIndependent:
7575
np.asarray([0, 1, 2]),
7676
],
7777
)
78-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
79-
def test_iloc_setitem_fullcol_categorical(self, indexer, key):
78+
def test_iloc_setitem_fullcol_categorical(self, indexer_li, key):
8079
frame = DataFrame({0: range(3)}, dtype=object)
8180

8281
cat = Categorical(["alpha", "beta", "gamma"])
@@ -86,7 +85,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key):
8685
df = frame.copy()
8786
orig_vals = df.values
8887

89-
indexer(df)[key, 0] = cat
88+
indexer_li(df)[key, 0] = cat
9089

9190
expected = DataFrame({0: cat}).astype(object)
9291
assert np.shares_memory(df[0].values, orig_vals)
@@ -102,7 +101,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key):
102101
# we retain the object dtype.
103102
frame = DataFrame({0: np.array([0, 1, 2], dtype=object), 1: range(3)})
104103
df = frame.copy()
105-
indexer(df)[key, 0] = cat
104+
indexer_li(df)[key, 0] = cat
106105
expected = DataFrame({0: Series(cat.astype(object), dtype=object), 1: range(3)})
107106
tm.assert_frame_equal(df, expected)
108107

@@ -985,8 +984,7 @@ def test_iloc_setitem_empty_frame_raises_with_3d_ndarray(self):
985984
with pytest.raises(ValueError, match=msg):
986985
obj.iloc[nd3] = 0
987986

988-
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
989-
def test_iloc_getitem_read_only_values(self, indexer):
987+
def test_iloc_getitem_read_only_values(self, indexer_li):
990988
# GH#10043 this is fundamentally a test for iloc, but test loc while
991989
# we're here
992990
rw_array = np.eye(10)
@@ -996,10 +994,12 @@ def test_iloc_getitem_read_only_values(self, indexer):
996994
ro_array.setflags(write=False)
997995
ro_df = DataFrame(ro_array)
998996

999-
tm.assert_frame_equal(indexer(rw_df)[[1, 2, 3]], indexer(ro_df)[[1, 2, 3]])
1000-
tm.assert_frame_equal(indexer(rw_df)[[1]], indexer(ro_df)[[1]])
1001-
tm.assert_series_equal(indexer(rw_df)[1], indexer(ro_df)[1])
1002-
tm.assert_frame_equal(indexer(rw_df)[1:3], indexer(ro_df)[1:3])
997+
tm.assert_frame_equal(
998+
indexer_li(rw_df)[[1, 2, 3]], indexer_li(ro_df)[[1, 2, 3]]
999+
)
1000+
tm.assert_frame_equal(indexer_li(rw_df)[[1]], indexer_li(ro_df)[[1]])
1001+
tm.assert_series_equal(indexer_li(rw_df)[1], indexer_li(ro_df)[1])
1002+
tm.assert_frame_equal(indexer_li(rw_df)[1:3], indexer_li(ro_df)[1:3])
10031003

10041004
def test_iloc_getitem_readonly_key(self):
10051005
# GH#17192 iloc with read-only array raising TypeError

pandas/tests/io/parser/dtypes/test_dtypes_basic.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,12 @@ def test_dtype_with_converters(all_parsers):
132132
tm.assert_frame_equal(result, expected)
133133

134134

135-
@pytest.mark.parametrize(
136-
"dtype", list(np.typecodes["AllInteger"] + np.typecodes["Float"])
137-
)
138-
def test_numeric_dtype(all_parsers, dtype):
135+
def test_numeric_dtype(all_parsers, any_real_numpy_dtype):
139136
data = "0\n1"
140137
parser = all_parsers
141-
expected = DataFrame([0, 1], dtype=dtype)
138+
expected = DataFrame([0, 1], dtype=any_real_numpy_dtype)
142139

143-
result = parser.read_csv(StringIO(data), header=None, dtype=dtype)
140+
result = parser.read_csv(StringIO(data), header=None, dtype=any_real_numpy_dtype)
144141
tm.assert_frame_equal(expected, result)
145142

146143

pandas/tests/reductions/test_reductions.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1417,13 +1417,10 @@ def test_mode_empty(self, dropna, expected):
14171417
(False, [1, 1, 1, 2, 3, 3, 3], [1, 3]),
14181418
],
14191419
)
1420-
@pytest.mark.parametrize(
1421-
"dt", list(np.typecodes["AllInteger"] + np.typecodes["Float"])
1422-
)
1423-
def test_mode_numerical(self, dropna, data, expected, dt):
1424-
s = Series(data, dtype=dt)
1420+
def test_mode_numerical(self, dropna, data, expected, any_real_numpy_dtype):
1421+
s = Series(data, dtype=any_real_numpy_dtype)
14251422
result = s.mode(dropna)
1426-
expected = Series(expected, dtype=dt)
1423+
expected = Series(expected, dtype=any_real_numpy_dtype)
14271424
tm.assert_series_equal(result, expected)
14281425

14291426
@pytest.mark.parametrize("dropna, expected", [(True, [1.0]), (False, [1, np.nan])])

pandas/tests/reshape/merge/test_merge.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1488,12 +1488,9 @@ def test_different(self, right_vals):
14881488
result = merge(left, right, on="A")
14891489
assert is_object_dtype(result.A.dtype)
14901490

1491-
@pytest.mark.parametrize(
1492-
"d1", [np.int64, np.int32, np.intc, np.int16, np.int8, np.uint8]
1493-
)
14941491
@pytest.mark.parametrize("d2", [np.int64, np.float64, np.float32, np.float16])
1495-
def test_join_multi_dtypes(self, d1, d2):
1496-
dtype1 = np.dtype(d1)
1492+
def test_join_multi_dtypes(self, any_int_numpy_dtype, d2):
1493+
dtype1 = np.dtype(any_int_numpy_dtype)
14971494
dtype2 = np.dtype(d2)
14981495

14991496
left = DataFrame(

pandas/tests/scalar/timestamp/test_formats.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def test_isoformat(ts, timespec, expected_iso):
8888

8989

9090
class TestTimestampRendering:
91-
timezones = ["UTC", "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]
92-
93-
@pytest.mark.parametrize("tz", timezones)
91+
@pytest.mark.parametrize(
92+
"tz", ["UTC", "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]
93+
)
9494
@pytest.mark.parametrize("freq", ["D", "M", "S", "N"])
9595
@pytest.mark.parametrize(
9696
"date", ["2014-03-07", "2014-01-01 09:00", "2014-01-01 00:00:00.000000001"]

pandas/tests/series/methods/test_astype.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,9 @@ def test_astype_ignores_errors_for_extension_dtypes(self, data, dtype, errors):
342342
with pytest.raises((ValueError, TypeError), match=msg):
343343
ser.astype(float, errors=errors)
344344

345-
@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
346-
def test_astype_from_float_to_str(self, dtype):
345+
def test_astype_from_float_to_str(self, any_float_dtype):
347346
# https://github.com/pandas-dev/pandas/issues/36451
348-
ser = Series([0.1], dtype=dtype)
347+
ser = Series([0.1], dtype=any_float_dtype)
349348
result = ser.astype(str)
350349
expected = Series(["0.1"], dtype=object)
351350
tm.assert_series_equal(result, expected)
@@ -374,21 +373,19 @@ def test_astype(self, dtype):
374373
assert as_typed.name == ser.name
375374

376375
@pytest.mark.parametrize("value", [np.nan, np.inf])
377-
@pytest.mark.parametrize("dtype", [np.int32, np.int64])
378-
def test_astype_cast_nan_inf_int(self, dtype, value):
376+
def test_astype_cast_nan_inf_int(self, any_int_numpy_dtype, value):
379377
# gh-14265: check NaN and inf raise error when converting to int
380378
msg = "Cannot convert non-finite values \\(NA or inf\\) to integer"
381379
ser = Series([value])
382380

383381
with pytest.raises(ValueError, match=msg):
384-
ser.astype(dtype)
382+
ser.astype(any_int_numpy_dtype)
385383

386-
@pytest.mark.parametrize("dtype", [int, np.int8, np.int64])
387-
def test_astype_cast_object_int_fail(self, dtype):
384+
def test_astype_cast_object_int_fail(self, any_int_numpy_dtype):
388385
arr = Series(["car", "house", "tree", "1"])
389386
msg = r"invalid literal for int\(\) with base 10: 'car'"
390387
with pytest.raises(ValueError, match=msg):
391-
arr.astype(dtype)
388+
arr.astype(any_int_numpy_dtype)
392389

393390
def test_astype_float_to_uint_negatives_raise(
394391
self, float_numpy_dtype, any_unsigned_int_numpy_dtype

pandas/tests/series/methods/test_compare.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import pandas._testing as tm
66

77

8-
@pytest.mark.parametrize("align_axis", [0, 1, "index", "columns"])
9-
def test_compare_axis(align_axis):
8+
def test_compare_axis(axis):
109
# GH#30429
1110
s1 = pd.Series(["a", "b", "c"])
1211
s2 = pd.Series(["x", "b", "z"])
1312

14-
result = s1.compare(s2, align_axis=align_axis)
13+
result = s1.compare(s2, align_axis=axis)
1514

16-
if align_axis in (1, "columns"):
15+
if axis in (1, "columns"):
1716
indices = pd.Index([0, 2])
1817
columns = pd.Index(["self", "other"])
1918
expected = pd.DataFrame(

pandas/tests/series/methods/test_cov_corr.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ def test_cov_ddof(self, test_ddof, dtype):
5656

5757

5858
class TestSeriesCorr:
59-
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
60-
def test_corr(self, datetime_series, dtype):
59+
def test_corr(self, datetime_series, any_float_dtype):
6160
stats = pytest.importorskip("scipy.stats")
6261

63-
datetime_series = datetime_series.astype(dtype)
62+
datetime_series = datetime_series.astype(any_float_dtype)
6463

6564
# full overlap
6665
tm.assert_almost_equal(datetime_series.corr(datetime_series), 1)

pandas/tests/series/methods/test_fillna.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -838,12 +838,11 @@ def test_fillna_categorical_raises(self):
838838
ser.fillna(DataFrame({1: ["a"], 3: ["b"]}))
839839

840840
@pytest.mark.parametrize("dtype", [float, "float32", "float64"])
841-
@pytest.mark.parametrize("fill_type", tm.ALL_REAL_NUMPY_DTYPES)
842841
@pytest.mark.parametrize("scalar", [True, False])
843-
def test_fillna_float_casting(self, dtype, fill_type, scalar):
842+
def test_fillna_float_casting(self, dtype, any_real_numpy_dtype, scalar):
844843
# GH-43424
845844
ser = Series([np.nan, 1.2], dtype=dtype)
846-
fill_values = Series([2, 2], dtype=fill_type)
845+
fill_values = Series([2, 2], dtype=any_real_numpy_dtype)
847846
if scalar:
848847
fill_values = fill_values.dtype.type(2)
849848

pandas/tests/test_algos.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1795,11 +1795,10 @@ def test_scipy_compat(self, arr):
17951795
exp[mask] = np.nan
17961796
tm.assert_almost_equal(result, exp)
17971797

1798-
@pytest.mark.parametrize("dtype", np.typecodes["AllInteger"])
1799-
def test_basic(self, writable, dtype):
1798+
def test_basic(self, writable, any_int_numpy_dtype):
18001799
exp = np.array([1, 2], dtype=np.float64)
18011800

1802-
data = np.array([1, 100], dtype=dtype)
1801+
data = np.array([1, 100], dtype=any_int_numpy_dtype)
18031802
data.setflags(write=writable)
18041803
ser = Series(data)
18051804
result = algos.rank(ser)
@@ -1836,22 +1835,21 @@ def test_no_mode(self):
18361835
exp = Series([], dtype=np.float64, index=Index([], dtype=int))
18371836
tm.assert_numpy_array_equal(algos.mode(np.array([])), exp.values)
18381837

1839-
@pytest.mark.parametrize("dt", np.typecodes["AllInteger"] + np.typecodes["Float"])
1840-
def test_mode_single(self, dt):
1838+
def test_mode_single(self, any_real_numpy_dtype):
18411839
# GH 15714
18421840
exp_single = [1]
18431841
data_single = [1]
18441842

18451843
exp_multi = [1]
18461844
data_multi = [1, 1]
18471845

1848-
ser = Series(data_single, dtype=dt)
1849-
exp = Series(exp_single, dtype=dt)
1846+
ser = Series(data_single, dtype=any_real_numpy_dtype)
1847+
exp = Series(exp_single, dtype=any_real_numpy_dtype)
18501848
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
18511849
tm.assert_series_equal(ser.mode(), exp)
18521850

1853-
ser = Series(data_multi, dtype=dt)
1854-
exp = Series(exp_multi, dtype=dt)
1851+
ser = Series(data_multi, dtype=any_real_numpy_dtype)
1852+
exp = Series(exp_multi, dtype=any_real_numpy_dtype)
18551853
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
18561854
tm.assert_series_equal(ser.mode(), exp)
18571855

@@ -1862,21 +1860,20 @@ def test_mode_obj_int(self):
18621860
exp = Series(["a", "b", "c"], dtype=object)
18631861
tm.assert_numpy_array_equal(algos.mode(exp.values), exp.values)
18641862

1865-
@pytest.mark.parametrize("dt", np.typecodes["AllInteger"] + np.typecodes["Float"])
1866-
def test_number_mode(self, dt):
1863+
def test_number_mode(self, any_real_numpy_dtype):
18671864
exp_single = [1]
18681865
data_single = [1] * 5 + [2] * 3
18691866

18701867
exp_multi = [1, 3]
18711868
data_multi = [1] * 5 + [2] * 3 + [3] * 5
18721869

1873-
ser = Series(data_single, dtype=dt)
1874-
exp = Series(exp_single, dtype=dt)
1870+
ser = Series(data_single, dtype=any_real_numpy_dtype)
1871+
exp = Series(exp_single, dtype=any_real_numpy_dtype)
18751872
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
18761873
tm.assert_series_equal(ser.mode(), exp)
18771874

1878-
ser = Series(data_multi, dtype=dt)
1879-
exp = Series(exp_multi, dtype=dt)
1875+
ser = Series(data_multi, dtype=any_real_numpy_dtype)
1876+
exp = Series(exp_multi, dtype=any_real_numpy_dtype)
18801877
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
18811878
tm.assert_series_equal(ser.mode(), exp)
18821879

pandas/tests/util/test_assert_extension_array_equal.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ def test_assert_extension_array_equal_non_extension_array(side):
108108
tm.assert_extension_array_equal(*args)
109109

110110

111-
@pytest.mark.parametrize("right_dtype", ["Int32", "int64"])
112-
def test_assert_extension_array_equal_ignore_dtype_mismatch(right_dtype):
111+
def test_assert_extension_array_equal_ignore_dtype_mismatch(any_int_dtype):
113112
# https://github.com/pandas-dev/pandas/issues/35715
114113
left = array([1, 2, 3], dtype="Int64")
115-
right = array([1, 2, 3], dtype=right_dtype)
114+
right = array([1, 2, 3], dtype=any_int_dtype)
116115
tm.assert_extension_array_equal(left, right, check_dtype=False)
117116

118117

pandas/tests/util/test_assert_series_equal.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ def test_series_not_equal_metadata_mismatch(kwargs):
106106

107107

108108
@pytest.mark.parametrize("data1,data2", [(0.12345, 0.12346), (0.1235, 0.1236)])
109-
@pytest.mark.parametrize("dtype", ["float32", "float64", "Float32"])
110109
@pytest.mark.parametrize("decimals", [0, 1, 2, 3, 5, 10])
111-
def test_less_precise(data1, data2, dtype, decimals):
110+
def test_less_precise(data1, data2, any_float_dtype, decimals):
112111
rtol = 10**-decimals
113-
s1 = Series([data1], dtype=dtype)
114-
s2 = Series([data2], dtype=dtype)
112+
s1 = Series([data1], dtype=any_float_dtype)
113+
s2 = Series([data2], dtype=any_float_dtype)
115114

116115
if decimals in (5, 10) or (decimals >= 3 and abs(data1 - data2) >= 0.0005):
117116
msg = "Series values are different"

pandas/tests/window/test_rolling_functions.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -471,20 +471,19 @@ def test_rolling_median_memory_error():
471471
).median()
472472

473473

474-
@pytest.mark.parametrize(
475-
"data_type",
476-
[np.dtype(f"f{width}") for width in [4, 8]]
477-
+ [np.dtype(f"{sign}{width}") for width in [1, 2, 4, 8] for sign in "ui"],
478-
)
479-
def test_rolling_min_max_numeric_types(data_type):
474+
def test_rolling_min_max_numeric_types(any_real_numpy_dtype):
480475
# GH12373
481476

482477
# Just testing that these don't throw exceptions and that
483478
# the return type is float64. Other tests will cover quantitative
484479
# correctness
485-
result = DataFrame(np.arange(20, dtype=data_type)).rolling(window=5).max()
480+
result = (
481+
DataFrame(np.arange(20, dtype=any_real_numpy_dtype)).rolling(window=5).max()
482+
)
486483
assert result.dtypes[0] == np.dtype("f8")
487-
result = DataFrame(np.arange(20, dtype=data_type)).rolling(window=5).min()
484+
result = (
485+
DataFrame(np.arange(20, dtype=any_real_numpy_dtype)).rolling(window=5).min()
486+
)
488487
assert result.dtypes[0] == np.dtype("f8")
489488

490489

0 commit comments

Comments
 (0)