Skip to content

Commit

Permalink
TST/CLN: Reuse more fixtures (pandas-dev#56726)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Jan 4, 2024
1 parent 8fb8b9f commit b2bca5e
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 83 deletions.
11 changes: 7 additions & 4 deletions pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Index,
Series,
)
import pandas._testing as tm


def test_isnull_notnull_docstrings():
Expand Down Expand Up @@ -130,9 +129,13 @@ def test_memory_usage_components_series(series_with_simple_index):
assert total_usage == non_index_usage + index_usage


@pytest.mark.parametrize("dtype", tm.NARROW_NP_DTYPES)
def test_memory_usage_components_narrow_series(dtype):
series = Series(range(5), dtype=dtype, index=[f"i-{i}" for i in range(5)], name="a")
def test_memory_usage_components_narrow_series(any_real_numpy_dtype):
series = Series(
range(5),
dtype=any_real_numpy_dtype,
index=[f"i-{i}" for i in range(5)],
name="a",
)
total_usage = series.memory_usage(index=True)
non_index_usage = series.memory_usage(index=False)
index_usage = series.index.memory_usage()
Expand Down
20 changes: 10 additions & 10 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ class TestiLocBaseIndependent:
np.asarray([0, 1, 2]),
],
)
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
def test_iloc_setitem_fullcol_categorical(self, indexer, key):
def test_iloc_setitem_fullcol_categorical(self, indexer_li, key):
frame = DataFrame({0: range(3)}, dtype=object)

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

indexer(df)[key, 0] = cat
indexer_li(df)[key, 0] = cat

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

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

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

tm.assert_frame_equal(indexer(rw_df)[[1, 2, 3]], indexer(ro_df)[[1, 2, 3]])
tm.assert_frame_equal(indexer(rw_df)[[1]], indexer(ro_df)[[1]])
tm.assert_series_equal(indexer(rw_df)[1], indexer(ro_df)[1])
tm.assert_frame_equal(indexer(rw_df)[1:3], indexer(ro_df)[1:3])
tm.assert_frame_equal(
indexer_li(rw_df)[[1, 2, 3]], indexer_li(ro_df)[[1, 2, 3]]
)
tm.assert_frame_equal(indexer_li(rw_df)[[1]], indexer_li(ro_df)[[1]])
tm.assert_series_equal(indexer_li(rw_df)[1], indexer_li(ro_df)[1])
tm.assert_frame_equal(indexer_li(rw_df)[1:3], indexer_li(ro_df)[1:3])

def test_iloc_getitem_readonly_key(self):
# GH#17192 iloc with read-only array raising TypeError
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/io/parser/dtypes/test_dtypes_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,12 @@ def test_dtype_with_converters(all_parsers):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"dtype", list(np.typecodes["AllInteger"] + np.typecodes["Float"])
)
def test_numeric_dtype(all_parsers, dtype):
def test_numeric_dtype(all_parsers, any_real_numpy_dtype):
data = "0\n1"
parser = all_parsers
expected = DataFrame([0, 1], dtype=dtype)
expected = DataFrame([0, 1], dtype=any_real_numpy_dtype)

result = parser.read_csv(StringIO(data), header=None, dtype=dtype)
result = parser.read_csv(StringIO(data), header=None, dtype=any_real_numpy_dtype)
tm.assert_frame_equal(expected, result)


Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,13 +1417,10 @@ def test_mode_empty(self, dropna, expected):
(False, [1, 1, 1, 2, 3, 3, 3], [1, 3]),
],
)
@pytest.mark.parametrize(
"dt", list(np.typecodes["AllInteger"] + np.typecodes["Float"])
)
def test_mode_numerical(self, dropna, data, expected, dt):
s = Series(data, dtype=dt)
def test_mode_numerical(self, dropna, data, expected, any_real_numpy_dtype):
s = Series(data, dtype=any_real_numpy_dtype)
result = s.mode(dropna)
expected = Series(expected, dtype=dt)
expected = Series(expected, dtype=any_real_numpy_dtype)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("dropna, expected", [(True, [1.0]), (False, [1, np.nan])])
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,12 +1488,9 @@ def test_different(self, right_vals):
result = merge(left, right, on="A")
assert is_object_dtype(result.A.dtype)

@pytest.mark.parametrize(
"d1", [np.int64, np.int32, np.intc, np.int16, np.int8, np.uint8]
)
@pytest.mark.parametrize("d2", [np.int64, np.float64, np.float32, np.float16])
def test_join_multi_dtypes(self, d1, d2):
dtype1 = np.dtype(d1)
def test_join_multi_dtypes(self, any_int_numpy_dtype, d2):
dtype1 = np.dtype(any_int_numpy_dtype)
dtype2 = np.dtype(d2)

left = DataFrame(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/scalar/timestamp/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def test_isoformat(ts, timespec, expected_iso):


class TestTimestampRendering:
timezones = ["UTC", "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]

@pytest.mark.parametrize("tz", timezones)
@pytest.mark.parametrize(
"tz", ["UTC", "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]
)
@pytest.mark.parametrize("freq", ["D", "M", "S", "N"])
@pytest.mark.parametrize(
"date", ["2014-03-07", "2014-01-01 09:00", "2014-01-01 00:00:00.000000001"]
Expand Down
15 changes: 6 additions & 9 deletions pandas/tests/series/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,9 @@ def test_astype_ignores_errors_for_extension_dtypes(self, data, dtype, errors):
with pytest.raises((ValueError, TypeError), match=msg):
ser.astype(float, errors=errors)

@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
def test_astype_from_float_to_str(self, dtype):
def test_astype_from_float_to_str(self, any_float_dtype):
# https://github.com/pandas-dev/pandas/issues/36451
ser = Series([0.1], dtype=dtype)
ser = Series([0.1], dtype=any_float_dtype)
result = ser.astype(str)
expected = Series(["0.1"], dtype=object)
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -374,21 +373,19 @@ def test_astype(self, dtype):
assert as_typed.name == ser.name

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

with pytest.raises(ValueError, match=msg):
ser.astype(dtype)
ser.astype(any_int_numpy_dtype)

@pytest.mark.parametrize("dtype", [int, np.int8, np.int64])
def test_astype_cast_object_int_fail(self, dtype):
def test_astype_cast_object_int_fail(self, any_int_numpy_dtype):
arr = Series(["car", "house", "tree", "1"])
msg = r"invalid literal for int\(\) with base 10: 'car'"
with pytest.raises(ValueError, match=msg):
arr.astype(dtype)
arr.astype(any_int_numpy_dtype)

def test_astype_float_to_uint_negatives_raise(
self, float_numpy_dtype, any_unsigned_int_numpy_dtype
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/series/methods/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import pandas._testing as tm


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

result = s1.compare(s2, align_axis=align_axis)
result = s1.compare(s2, align_axis=axis)

if align_axis in (1, "columns"):
if axis in (1, "columns"):
indices = pd.Index([0, 2])
columns = pd.Index(["self", "other"])
expected = pd.DataFrame(
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/series/methods/test_cov_corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ def test_cov_ddof(self, test_ddof, dtype):


class TestSeriesCorr:
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
def test_corr(self, datetime_series, dtype):
def test_corr(self, datetime_series, any_float_dtype):
stats = pytest.importorskip("scipy.stats")

datetime_series = datetime_series.astype(dtype)
datetime_series = datetime_series.astype(any_float_dtype)

# full overlap
tm.assert_almost_equal(datetime_series.corr(datetime_series), 1)
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,12 +838,11 @@ def test_fillna_categorical_raises(self):
ser.fillna(DataFrame({1: ["a"], 3: ["b"]}))

@pytest.mark.parametrize("dtype", [float, "float32", "float64"])
@pytest.mark.parametrize("fill_type", tm.ALL_REAL_NUMPY_DTYPES)
@pytest.mark.parametrize("scalar", [True, False])
def test_fillna_float_casting(self, dtype, fill_type, scalar):
def test_fillna_float_casting(self, dtype, any_real_numpy_dtype, scalar):
# GH-43424
ser = Series([np.nan, 1.2], dtype=dtype)
fill_values = Series([2, 2], dtype=fill_type)
fill_values = Series([2, 2], dtype=any_real_numpy_dtype)
if scalar:
fill_values = fill_values.dtype.type(2)

Expand Down
27 changes: 12 additions & 15 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1795,11 +1795,10 @@ def test_scipy_compat(self, arr):
exp[mask] = np.nan
tm.assert_almost_equal(result, exp)

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

data = np.array([1, 100], dtype=dtype)
data = np.array([1, 100], dtype=any_int_numpy_dtype)
data.setflags(write=writable)
ser = Series(data)
result = algos.rank(ser)
Expand Down Expand Up @@ -1836,22 +1835,21 @@ def test_no_mode(self):
exp = Series([], dtype=np.float64, index=Index([], dtype=int))
tm.assert_numpy_array_equal(algos.mode(np.array([])), exp.values)

@pytest.mark.parametrize("dt", np.typecodes["AllInteger"] + np.typecodes["Float"])
def test_mode_single(self, dt):
def test_mode_single(self, any_real_numpy_dtype):
# GH 15714
exp_single = [1]
data_single = [1]

exp_multi = [1]
data_multi = [1, 1]

ser = Series(data_single, dtype=dt)
exp = Series(exp_single, dtype=dt)
ser = Series(data_single, dtype=any_real_numpy_dtype)
exp = Series(exp_single, dtype=any_real_numpy_dtype)
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
tm.assert_series_equal(ser.mode(), exp)

ser = Series(data_multi, dtype=dt)
exp = Series(exp_multi, dtype=dt)
ser = Series(data_multi, dtype=any_real_numpy_dtype)
exp = Series(exp_multi, dtype=any_real_numpy_dtype)
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
tm.assert_series_equal(ser.mode(), exp)

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

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

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

ser = Series(data_single, dtype=dt)
exp = Series(exp_single, dtype=dt)
ser = Series(data_single, dtype=any_real_numpy_dtype)
exp = Series(exp_single, dtype=any_real_numpy_dtype)
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
tm.assert_series_equal(ser.mode(), exp)

ser = Series(data_multi, dtype=dt)
exp = Series(exp_multi, dtype=dt)
ser = Series(data_multi, dtype=any_real_numpy_dtype)
exp = Series(exp_multi, dtype=any_real_numpy_dtype)
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
tm.assert_series_equal(ser.mode(), exp)

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/util/test_assert_extension_array_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ def test_assert_extension_array_equal_non_extension_array(side):
tm.assert_extension_array_equal(*args)


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


Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ def test_series_not_equal_metadata_mismatch(kwargs):


@pytest.mark.parametrize("data1,data2", [(0.12345, 0.12346), (0.1235, 0.1236)])
@pytest.mark.parametrize("dtype", ["float32", "float64", "Float32"])
@pytest.mark.parametrize("decimals", [0, 1, 2, 3, 5, 10])
def test_less_precise(data1, data2, dtype, decimals):
def test_less_precise(data1, data2, any_float_dtype, decimals):
rtol = 10**-decimals
s1 = Series([data1], dtype=dtype)
s2 = Series([data2], dtype=dtype)
s1 = Series([data1], dtype=any_float_dtype)
s2 = Series([data2], dtype=any_float_dtype)

if decimals in (5, 10) or (decimals >= 3 and abs(data1 - data2) >= 0.0005):
msg = "Series values are different"
Expand Down
15 changes: 7 additions & 8 deletions pandas/tests/window/test_rolling_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,19 @@ def test_rolling_median_memory_error():
).median()


@pytest.mark.parametrize(
"data_type",
[np.dtype(f"f{width}") for width in [4, 8]]
+ [np.dtype(f"{sign}{width}") for width in [1, 2, 4, 8] for sign in "ui"],
)
def test_rolling_min_max_numeric_types(data_type):
def test_rolling_min_max_numeric_types(any_real_numpy_dtype):
# GH12373

# Just testing that these don't throw exceptions and that
# the return type is float64. Other tests will cover quantitative
# correctness
result = DataFrame(np.arange(20, dtype=data_type)).rolling(window=5).max()
result = (
DataFrame(np.arange(20, dtype=any_real_numpy_dtype)).rolling(window=5).max()
)
assert result.dtypes[0] == np.dtype("f8")
result = DataFrame(np.arange(20, dtype=data_type)).rolling(window=5).min()
result = (
DataFrame(np.arange(20, dtype=any_real_numpy_dtype)).rolling(window=5).min()
)
assert result.dtypes[0] == np.dtype("f8")


Expand Down

0 comments on commit b2bca5e

Please sign in to comment.