Skip to content

Commit 52cb549

Browse files
authored
TST/CLN: Test parametrizations 4 (#56787)
* TST/CLN: Test parametrizations * Fix signature * Add object
1 parent dca658b commit 52cb549

16 files changed

+126
-164
lines changed

pandas/tests/strings/test_find_replace.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,11 @@ def test_replace_compiled_regex_callable(any_string_dtype):
515515
tm.assert_series_equal(result, expected)
516516

517517

518-
@pytest.mark.parametrize(
519-
"regex,expected", [(True, ["bao", "bao", np.nan]), (False, ["bao", "foo", np.nan])]
520-
)
521-
def test_replace_literal(regex, expected, any_string_dtype):
518+
@pytest.mark.parametrize("regex,expected_val", [(True, "bao"), (False, "foo")])
519+
def test_replace_literal(regex, expected_val, any_string_dtype):
522520
# GH16808 literal replace (regex=False vs regex=True)
523521
ser = Series(["f.o", "foo", np.nan], dtype=any_string_dtype)
524-
expected = Series(expected, dtype=any_string_dtype)
522+
expected = Series(["bao", expected_val, np.nan], dtype=any_string_dtype)
525523
result = ser.str.replace("f.", "ba", regex=regex)
526524
tm.assert_series_equal(result, expected)
527525

pandas/tests/strings/test_split_partition.py

+12-21
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,24 @@ def test_split_maxsplit(data, pat, any_string_dtype, n):
190190

191191

192192
@pytest.mark.parametrize(
193-
"data, pat, expected",
193+
"data, pat, expected_val",
194194
[
195195
(
196196
["split once", "split once too!"],
197197
None,
198-
Series({0: ["split", "once"], 1: ["split", "once too!"]}),
198+
"once too!",
199199
),
200200
(
201201
["split_once", "split_once_too!"],
202202
"_",
203-
Series({0: ["split", "once"], 1: ["split", "once_too!"]}),
203+
"once_too!",
204204
),
205205
],
206206
)
207-
def test_split_no_pat_with_nonzero_n(data, pat, expected, any_string_dtype):
207+
def test_split_no_pat_with_nonzero_n(data, pat, expected_val, any_string_dtype):
208208
s = Series(data, dtype=any_string_dtype)
209209
result = s.str.split(pat=pat, n=1)
210+
expected = Series({0: ["split", "once"], 1: ["split", expected_val]})
210211
tm.assert_series_equal(expected, result, check_index_type=False)
211212

212213

@@ -533,37 +534,27 @@ def test_partition_series_stdlib(any_string_dtype, method):
533534

534535

535536
@pytest.mark.parametrize(
536-
"method, expand, exp, exp_levels",
537+
"method, exp",
537538
[
538539
[
539540
"partition",
540-
False,
541-
np.array(
542-
[("a", "_", "b_c"), ("c", "_", "d_e"), ("f", "_", "g_h"), np.nan, None],
543-
dtype=object,
544-
),
545-
1,
541+
[("a", "_", "b_c"), ("c", "_", "d_e"), ("f", "_", "g_h"), np.nan, None],
546542
],
547543
[
548544
"rpartition",
549-
False,
550-
np.array(
551-
[("a_b", "_", "c"), ("c_d", "_", "e"), ("f_g", "_", "h"), np.nan, None],
552-
dtype=object,
553-
),
554-
1,
545+
[("a_b", "_", "c"), ("c_d", "_", "e"), ("f_g", "_", "h"), np.nan, None],
555546
],
556547
],
557548
)
558-
def test_partition_index(method, expand, exp, exp_levels):
549+
def test_partition_index(method, exp):
559550
# https://github.com/pandas-dev/pandas/issues/23558
560551

561552
values = Index(["a_b_c", "c_d_e", "f_g_h", np.nan, None])
562553

563-
result = getattr(values.str, method)("_", expand=expand)
564-
exp = Index(exp)
554+
result = getattr(values.str, method)("_", expand=False)
555+
exp = Index(np.array(exp, dtype=object), dtype=object)
565556
tm.assert_index_equal(result, exp)
566-
assert result.nlevels == exp_levels
557+
assert result.nlevels == 1
567558

568559

569560
@pytest.mark.parametrize(

pandas/tests/test_errors.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,7 @@ def test_catch_oob():
6767
pd.Timestamp("15000101").as_unit("ns")
6868

6969

70-
@pytest.mark.parametrize(
71-
"is_local",
72-
[
73-
True,
74-
False,
75-
],
76-
)
70+
@pytest.mark.parametrize("is_local", [True, False])
7771
def test_catch_undefined_variable_error(is_local):
7872
variable_name = "x"
7973
if is_local:

pandas/tests/tools/test_to_datetime.py

+32-32
Original file line numberDiff line numberDiff line change
@@ -212,51 +212,54 @@ def test_to_datetime_format_YYYYMMDD_with_none(self, input_s):
212212
[
213213
# NaN before strings with invalid date values
214214
[
215-
Series(["19801222", np.nan, "20010012", "10019999"]),
216-
Series([Timestamp("19801222"), np.nan, np.nan, np.nan]),
215+
["19801222", np.nan, "20010012", "10019999"],
216+
[Timestamp("19801222"), np.nan, np.nan, np.nan],
217217
],
218218
# NaN after strings with invalid date values
219219
[
220-
Series(["19801222", "20010012", "10019999", np.nan]),
221-
Series([Timestamp("19801222"), np.nan, np.nan, np.nan]),
220+
["19801222", "20010012", "10019999", np.nan],
221+
[Timestamp("19801222"), np.nan, np.nan, np.nan],
222222
],
223223
# NaN before integers with invalid date values
224224
[
225-
Series([20190813, np.nan, 20010012, 20019999]),
226-
Series([Timestamp("20190813"), np.nan, np.nan, np.nan]),
225+
[20190813, np.nan, 20010012, 20019999],
226+
[Timestamp("20190813"), np.nan, np.nan, np.nan],
227227
],
228228
# NaN after integers with invalid date values
229229
[
230-
Series([20190813, 20010012, np.nan, 20019999]),
231-
Series([Timestamp("20190813"), np.nan, np.nan, np.nan]),
230+
[20190813, 20010012, np.nan, 20019999],
231+
[Timestamp("20190813"), np.nan, np.nan, np.nan],
232232
],
233233
],
234234
)
235235
def test_to_datetime_format_YYYYMMDD_overflow(self, input_s, expected):
236236
# GH 25512
237237
# format='%Y%m%d', errors='coerce'
238+
input_s = Series(input_s)
238239
result = to_datetime(input_s, format="%Y%m%d", errors="coerce")
240+
expected = Series(expected)
239241
tm.assert_series_equal(result, expected)
240242

241243
@pytest.mark.parametrize(
242244
"data, format, expected",
243245
[
244-
([pd.NA], "%Y%m%d%H%M%S", DatetimeIndex(["NaT"])),
245-
([pd.NA], None, DatetimeIndex(["NaT"])),
246+
([pd.NA], "%Y%m%d%H%M%S", ["NaT"]),
247+
([pd.NA], None, ["NaT"]),
246248
(
247249
[pd.NA, "20210202202020"],
248250
"%Y%m%d%H%M%S",
249-
DatetimeIndex(["NaT", "2021-02-02 20:20:20"]),
251+
["NaT", "2021-02-02 20:20:20"],
250252
),
251-
(["201010", pd.NA], "%y%m%d", DatetimeIndex(["2020-10-10", "NaT"])),
252-
(["201010", pd.NA], "%d%m%y", DatetimeIndex(["2010-10-20", "NaT"])),
253-
([None, np.nan, pd.NA], None, DatetimeIndex(["NaT", "NaT", "NaT"])),
254-
([None, np.nan, pd.NA], "%Y%m%d", DatetimeIndex(["NaT", "NaT", "NaT"])),
253+
(["201010", pd.NA], "%y%m%d", ["2020-10-10", "NaT"]),
254+
(["201010", pd.NA], "%d%m%y", ["2010-10-20", "NaT"]),
255+
([None, np.nan, pd.NA], None, ["NaT", "NaT", "NaT"]),
256+
([None, np.nan, pd.NA], "%Y%m%d", ["NaT", "NaT", "NaT"]),
255257
],
256258
)
257259
def test_to_datetime_with_NA(self, data, format, expected):
258260
# GH#42957
259261
result = to_datetime(data, format=format)
262+
expected = DatetimeIndex(expected)
260263
tm.assert_index_equal(result, expected)
261264

262265
def test_to_datetime_with_NA_with_warning(self):
@@ -422,12 +425,12 @@ def test_parse_nanoseconds_with_formula(self, cache, arg):
422425
@pytest.mark.parametrize(
423426
"value,fmt,expected",
424427
[
425-
["2009324", "%Y%W%w", Timestamp("2009-08-13")],
426-
["2013020", "%Y%U%w", Timestamp("2013-01-13")],
428+
["2009324", "%Y%W%w", "2009-08-13"],
429+
["2013020", "%Y%U%w", "2013-01-13"],
427430
],
428431
)
429432
def test_to_datetime_format_weeks(self, value, fmt, expected, cache):
430-
assert to_datetime(value, format=fmt, cache=cache) == expected
433+
assert to_datetime(value, format=fmt, cache=cache) == Timestamp(expected)
431434

432435
@pytest.mark.parametrize(
433436
"fmt,dates,expected_dates",
@@ -715,24 +718,20 @@ def test_to_datetime_mixed_datetime_and_string_with_format_mixed_offsets_utc_fal
715718
[
716719
pytest.param(
717720
"%Y-%m-%d %H:%M:%S%z",
718-
Index(
719-
[
720-
Timestamp("2000-01-01 09:00:00+0100", tz="UTC+01:00"),
721-
Timestamp("2000-01-02 02:00:00+0200", tz="UTC+02:00"),
722-
NaT,
723-
]
724-
),
721+
[
722+
Timestamp("2000-01-01 09:00:00+0100", tz="UTC+01:00"),
723+
Timestamp("2000-01-02 02:00:00+0200", tz="UTC+02:00"),
724+
NaT,
725+
],
725726
id="ISO8601, non-UTC",
726727
),
727728
pytest.param(
728729
"%Y-%d-%m %H:%M:%S%z",
729-
Index(
730-
[
731-
Timestamp("2000-01-01 09:00:00+0100", tz="UTC+01:00"),
732-
Timestamp("2000-02-01 02:00:00+0200", tz="UTC+02:00"),
733-
NaT,
734-
]
735-
),
730+
[
731+
Timestamp("2000-01-01 09:00:00+0100", tz="UTC+01:00"),
732+
Timestamp("2000-02-01 02:00:00+0200", tz="UTC+02:00"),
733+
NaT,
734+
],
736735
id="non-ISO8601, non-UTC",
737736
),
738737
],
@@ -747,6 +746,7 @@ def test_to_datetime_mixed_offsets_with_none_tz(self, fmt, expected):
747746
format=fmt,
748747
utc=False,
749748
)
749+
expected = Index(expected)
750750
tm.assert_index_equal(result, expected)
751751

752752
@pytest.mark.parametrize(

pandas/tests/tools/test_to_numeric.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -596,21 +596,12 @@ def test_downcast_float64_to_float32():
596596
assert series.dtype == result.dtype
597597

598598

599-
@pytest.mark.parametrize(
600-
"ser,expected",
601-
[
602-
(
603-
Series([0, 9223372036854775808]),
604-
Series([0, 9223372036854775808], dtype=np.uint64),
605-
)
606-
],
607-
)
608-
def test_downcast_uint64(ser, expected):
599+
def test_downcast_uint64():
609600
# see gh-14422:
610601
# BUG: to_numeric doesn't work uint64 numbers
611-
602+
ser = Series([0, 9223372036854775808])
612603
result = to_numeric(ser, downcast="unsigned")
613-
604+
expected = Series([0, 9223372036854775808], dtype=np.uint64)
614605
tm.assert_series_equal(result, expected)
615606

616607

pandas/tests/tools/test_to_timedelta.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ def test_to_timedelta_oob_non_nano(self):
9898
with pytest.raises(OutOfBoundsTimedelta, match=msg):
9999
TimedeltaArray._from_sequence(arr, dtype="m8[s]")
100100

101-
@pytest.mark.parametrize(
102-
"arg", [np.arange(10).reshape(2, 5), pd.DataFrame(np.arange(10).reshape(2, 5))]
103-
)
101+
@pytest.mark.parametrize("box", [lambda x: x, pd.DataFrame])
104102
@pytest.mark.parametrize("errors", ["ignore", "raise", "coerce"])
105103
@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning")
106-
def test_to_timedelta_dataframe(self, arg, errors):
104+
def test_to_timedelta_dataframe(self, box, errors):
107105
# GH 11776
106+
arg = box(np.arange(10).reshape(2, 5))
108107
with pytest.raises(TypeError, match="1-d array"):
109108
to_timedelta(arg, errors=errors)
110109

pandas/tests/tslibs/test_array_to_datetime.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,14 @@ class SubDatetime(datetime):
300300
pass
301301

302302

303-
@pytest.mark.parametrize(
304-
"data,expected",
305-
[
306-
([SubDatetime(2000, 1, 1)], ["2000-01-01T00:00:00.000000000"]),
307-
([datetime(2000, 1, 1)], ["2000-01-01T00:00:00.000000000"]),
308-
([Timestamp(2000, 1, 1)], ["2000-01-01T00:00:00.000000000"]),
309-
],
310-
)
311-
def test_datetime_subclass(data, expected):
303+
@pytest.mark.parametrize("klass", [SubDatetime, datetime, Timestamp])
304+
def test_datetime_subclass(klass):
312305
# GH 25851
313306
# ensure that subclassed datetime works with
314307
# array_to_datetime
315308

316-
arr = np.array(data, dtype=object)
309+
arr = np.array([klass(2000, 1, 1)], dtype=object)
317310
result, _ = tslib.array_to_datetime(arr)
318311

319-
expected = np.array(expected, dtype="M8[ns]")
312+
expected = np.array(["2000-01-01T00:00:00.000000000"], dtype="M8[ns]")
320313
tm.assert_numpy_array_equal(result, expected)

pandas/tests/tslibs/test_conversion.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ def test_tz_convert_single_matches_tz_convert(tz_aware_fixture, freq):
8686
@pytest.mark.parametrize(
8787
"arr",
8888
[
89-
pytest.param(np.array([], dtype=np.int64), id="empty"),
90-
pytest.param(np.array([iNaT], dtype=np.int64), id="all_nat"),
89+
pytest.param([], id="empty"),
90+
pytest.param([iNaT], id="all_nat"),
9191
],
9292
)
9393
def test_tz_convert_corner(arr):
94+
arr = np.array([iNaT], dtype=np.int64)
9495
result = tz_convert_from_utc(arr, timezones.maybe_get_tz("Asia/Tokyo"))
9596
tm.assert_numpy_array_equal(result, arr)
9697

pandas/tests/tslibs/test_liboffsets.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ def test_get_day_of_month_error():
127127
roll_qtrday(dt, n=3, month=11, day_opt=day_opt, modby=12)
128128

129129

130-
@pytest.mark.parametrize(
131-
"month",
132-
[3, 5], # (other.month % 3) < (month % 3) # (other.month % 3) > (month % 3)
133-
)
130+
@pytest.mark.parametrize("month", [3, 5])
131+
# (other.month % 3) < (month % 3) # (other.month % 3) > (month % 3)
134132
@pytest.mark.parametrize("n", [4, -3])
135133
def test_roll_qtr_day_not_mod_unequal(day_opt, month, n):
136134
expected = {3: {-3: -2, 4: 4}, 5: {-3: -3, 4: 3}}

pandas/tests/util/test_assert_almost_equal.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,14 @@ def test_assert_almost_equal_strings():
257257
_assert_almost_equal_both("abc", "abc")
258258

259259

260-
@pytest.mark.parametrize(
261-
"a,b", [("abc", "abcd"), ("abc", "abd"), ("abc", 1), ("abc", [1])]
262-
)
263-
def test_assert_not_almost_equal_strings(a, b):
264-
_assert_not_almost_equal_both(a, b)
260+
@pytest.mark.parametrize("b", ["abcd", "abd", 1, [1]])
261+
def test_assert_not_almost_equal_strings(b):
262+
_assert_not_almost_equal_both("abc", b)
265263

266264

267-
@pytest.mark.parametrize(
268-
"a,b", [([1, 2, 3], [1, 2, 3]), (np.array([1, 2, 3]), np.array([1, 2, 3]))]
269-
)
270-
def test_assert_almost_equal_iterables(a, b):
271-
_assert_almost_equal_both(a, b)
265+
@pytest.mark.parametrize("box", [list, np.array])
266+
def test_assert_almost_equal_iterables(box):
267+
_assert_almost_equal_both(box([1, 2, 3]), box([1, 2, 3]))
272268

273269

274270
@pytest.mark.parametrize(

pandas/tests/util/test_assert_categorical_equal.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import pandas._testing as tm
55

66

7-
@pytest.mark.parametrize(
8-
"c",
9-
[Categorical([1, 2, 3, 4]), Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4, 5])],
10-
)
7+
@pytest.mark.parametrize("c", [None, [1, 2, 3, 4, 5]])
118
def test_categorical_equal(c):
9+
c = Categorical([1, 2, 3, 4], categories=c)
1210
tm.assert_categorical_equal(c, c)
1311

1412

0 commit comments

Comments
 (0)