Skip to content

Commit b2bca5e

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

File tree

14 files changed

+65
-83
lines changed

14 files changed

+65
-83
lines changed

pandas/tests/base/test_misc.py

Lines changed: 7 additions & 4 deletions
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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 9 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

0 commit comments

Comments
 (0)