Skip to content

Commit 42065cd

Browse files
authored
REF: parametrize indexing tests (#31592)
1 parent 9bfdb0d commit 42065cd

File tree

5 files changed

+292
-270
lines changed

5 files changed

+292
-270
lines changed

pandas/tests/indexing/test_floats.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,9 @@ def check(self, result, original, indexer, getitem):
2222

2323
tm.assert_almost_equal(result, expected)
2424

25-
def test_scalar_error(self):
26-
27-
# GH 4892
28-
# float_indexers should raise exceptions
29-
# on appropriate Index types & accessors
30-
# this duplicates the code below
31-
# but is specifically testing for the error
32-
# message
33-
34-
for index in [
25+
@pytest.mark.parametrize(
26+
"index_func",
27+
[
3528
tm.makeStringIndex,
3629
tm.makeUnicodeIndex,
3730
tm.makeCategoricalIndex,
@@ -40,22 +33,31 @@ def test_scalar_error(self):
4033
tm.makePeriodIndex,
4134
tm.makeIntIndex,
4235
tm.makeRangeIndex,
43-
]:
36+
],
37+
)
38+
def test_scalar_error(self, index_func):
4439

45-
i = index(5)
40+
# GH 4892
41+
# float_indexers should raise exceptions
42+
# on appropriate Index types & accessors
43+
# this duplicates the code below
44+
# but is specifically testing for the error
45+
# message
4646

47-
s = Series(np.arange(len(i)), index=i)
47+
i = index_func(5)
4848

49-
msg = "Cannot index by location index"
50-
with pytest.raises(TypeError, match=msg):
51-
s.iloc[3.0]
49+
s = Series(np.arange(len(i)), index=i)
5250

53-
msg = (
54-
"cannot do positional indexing on {klass} with these "
55-
r"indexers \[3\.0\] of {kind}".format(klass=type(i), kind=str(float))
56-
)
57-
with pytest.raises(TypeError, match=msg):
58-
s.iloc[3.0] = 0
51+
msg = "Cannot index by location index"
52+
with pytest.raises(TypeError, match=msg):
53+
s.iloc[3.0]
54+
55+
msg = (
56+
"cannot do positional indexing on {klass} with these "
57+
r"indexers \[3\.0\] of {kind}".format(klass=type(i), kind=str(float))
58+
)
59+
with pytest.raises(TypeError, match=msg):
60+
s.iloc[3.0] = 0
5961

6062
def test_scalar_non_numeric(self):
6163

pandas/tests/indexing/test_iloc.py

+41-36
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,44 @@
1515

1616

1717
class TestiLoc(Base):
18+
def test_iloc_getitem_int(self):
19+
# integer
20+
self.check_result(
21+
"iloc",
22+
2,
23+
"iloc",
24+
2,
25+
typs=["labels", "mixed", "ts", "floats", "empty"],
26+
fails=IndexError,
27+
)
28+
29+
def test_iloc_getitem_neg_int(self):
30+
# neg integer
31+
self.check_result(
32+
"iloc",
33+
-1,
34+
"iloc",
35+
-1,
36+
typs=["labels", "mixed", "ts", "floats", "empty"],
37+
fails=IndexError,
38+
)
39+
40+
def test_iloc_getitem_list_int(self):
41+
self.check_result(
42+
"iloc",
43+
[0, 1, 2],
44+
"iloc",
45+
[0, 1, 2],
46+
typs=["labels", "mixed", "ts", "floats", "empty"],
47+
fails=IndexError,
48+
)
49+
50+
# array of ints (GH5006), make sure that a single indexer is returning
51+
# the correct type
52+
53+
54+
class TestiLoc2:
55+
# TODO: better name, just separating out things that dont rely on base class
1856
def test_iloc_exceeds_bounds(self):
1957

2058
# GH6296
@@ -135,28 +173,6 @@ def test_iloc_non_integer_raises(self, index, columns, index_vals, column_vals):
135173
with pytest.raises(IndexError, match=msg):
136174
df.iloc[index_vals, column_vals]
137175

138-
def test_iloc_getitem_int(self):
139-
# integer
140-
self.check_result(
141-
"iloc",
142-
2,
143-
"iloc",
144-
2,
145-
typs=["labels", "mixed", "ts", "floats", "empty"],
146-
fails=IndexError,
147-
)
148-
149-
def test_iloc_getitem_neg_int(self):
150-
# neg integer
151-
self.check_result(
152-
"iloc",
153-
-1,
154-
"iloc",
155-
-1,
156-
typs=["labels", "mixed", "ts", "floats", "empty"],
157-
fails=IndexError,
158-
)
159-
160176
@pytest.mark.parametrize("dims", [1, 2])
161177
def test_iloc_getitem_invalid_scalar(self, dims):
162178
# GH 21982
@@ -183,19 +199,6 @@ def test_iloc_array_not_mutating_negative_indices(self):
183199
df.iloc[:, array_with_neg_numbers]
184200
tm.assert_numpy_array_equal(array_with_neg_numbers, array_copy)
185201

186-
def test_iloc_getitem_list_int(self):
187-
self.check_result(
188-
"iloc",
189-
[0, 1, 2],
190-
"iloc",
191-
[0, 1, 2],
192-
typs=["labels", "mixed", "ts", "floats", "empty"],
193-
fails=IndexError,
194-
)
195-
196-
# array of ints (GH5006), make sure that a single indexer is returning
197-
# the correct type
198-
199202
def test_iloc_getitem_neg_int_can_reach_first_index(self):
200203
# GH10547 and GH10779
201204
# negative integers should be able to reach index 0
@@ -286,7 +289,9 @@ def test_iloc_getitem_slice_dups(self):
286289
tm.assert_frame_equal(df.iloc[10:, 2:], df1)
287290

288291
def test_iloc_setitem(self):
289-
df = self.frame_ints
292+
df = DataFrame(
293+
np.random.randn(4, 4), index=np.arange(0, 8, 2), columns=np.arange(0, 12, 3)
294+
)
290295

291296
df.iloc[1, 1] = 1
292297
result = df.iloc[1, 1]

pandas/tests/indexing/test_indexing.py

+69-62
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
from pandas.core.generic import NDFrame
1818
from pandas.core.indexers import validate_indices
1919
from pandas.core.indexing import _maybe_numeric_slice, _non_reducing_slice
20-
from pandas.tests.indexing.common import Base, _mklbl
20+
from pandas.tests.indexing.common import _mklbl
2121

2222
# ------------------------------------------------------------------------
2323
# Indexing test cases
2424

2525

26-
class TestFancy(Base):
26+
class TestFancy:
2727
""" pure get/set item & fancy indexing """
2828

2929
def test_setitem_ndarray_1d(self):
@@ -750,7 +750,7 @@ def test_index_type_coercion(self):
750750
assert s2.index.is_object()
751751

752752

753-
class TestMisc(Base):
753+
class TestMisc:
754754
def test_float_index_to_mixed(self):
755755
df = DataFrame({0.0: np.random.rand(10), 1.0: np.random.rand(10)})
756756
df["a"] = 10
@@ -875,21 +875,21 @@ def test_indexing_dtypes_on_empty(self):
875875
assert df2.loc[:, "a"].dtype == np.int64
876876
tm.assert_series_equal(df2.loc[:, "a"], df2.iloc[:, 0])
877877

878-
def test_range_in_series_indexing(self):
878+
@pytest.mark.parametrize("size", [5, 999999, 1000000])
879+
def test_range_in_series_indexing(self, size):
879880
# range can cause an indexing error
880881
# GH 11652
881-
for x in [5, 999999, 1000000]:
882-
s = Series(index=range(x), dtype=np.float64)
883-
s.loc[range(1)] = 42
884-
tm.assert_series_equal(s.loc[range(1)], Series(42.0, index=[0]))
882+
s = Series(index=range(size), dtype=np.float64)
883+
s.loc[range(1)] = 42
884+
tm.assert_series_equal(s.loc[range(1)], Series(42.0, index=[0]))
885885

886-
s.loc[range(2)] = 43
887-
tm.assert_series_equal(s.loc[range(2)], Series(43.0, index=[0, 1]))
886+
s.loc[range(2)] = 43
887+
tm.assert_series_equal(s.loc[range(2)], Series(43.0, index=[0, 1]))
888888

889-
def test_non_reducing_slice(self):
890-
df = DataFrame([[0, 1], [2, 3]])
891-
892-
slices = [
889+
@pytest.mark.parametrize(
890+
"slc",
891+
[
892+
# FIXME: dont leave commented-out
893893
# pd.IndexSlice[:, :],
894894
pd.IndexSlice[:, 1],
895895
pd.IndexSlice[1, :],
@@ -902,10 +902,13 @@ def test_non_reducing_slice(self):
902902
[0, 1],
903903
np.array([0, 1]),
904904
Series([0, 1]),
905-
]
906-
for slice_ in slices:
907-
tslice_ = _non_reducing_slice(slice_)
908-
assert isinstance(df.loc[tslice_], DataFrame)
905+
],
906+
)
907+
def test_non_reducing_slice(self, slc):
908+
df = DataFrame([[0, 1], [2, 3]])
909+
910+
tslice_ = _non_reducing_slice(slc)
911+
assert isinstance(df.loc[tslice_], DataFrame)
909912

910913
def test_list_slice(self):
911914
# like dataframe getitem
@@ -965,37 +968,37 @@ class TestSeriesNoneCoercion:
965968
(["foo", "bar", "baz"], [None, "bar", "baz"]),
966969
]
967970

968-
def test_coercion_with_setitem(self):
969-
for start_data, expected_result in self.EXPECTED_RESULTS:
970-
start_series = Series(start_data)
971-
start_series[0] = None
971+
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
972+
def test_coercion_with_setitem(self, start_data, expected_result):
973+
start_series = Series(start_data)
974+
start_series[0] = None
972975

973-
expected_series = Series(expected_result)
974-
tm.assert_series_equal(start_series, expected_series)
976+
expected_series = Series(expected_result)
977+
tm.assert_series_equal(start_series, expected_series)
975978

976-
def test_coercion_with_loc_setitem(self):
977-
for start_data, expected_result in self.EXPECTED_RESULTS:
978-
start_series = Series(start_data)
979-
start_series.loc[0] = None
979+
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
980+
def test_coercion_with_loc_setitem(self, start_data, expected_result):
981+
start_series = Series(start_data)
982+
start_series.loc[0] = None
980983

981-
expected_series = Series(expected_result)
982-
tm.assert_series_equal(start_series, expected_series)
984+
expected_series = Series(expected_result)
985+
tm.assert_series_equal(start_series, expected_series)
983986

984-
def test_coercion_with_setitem_and_series(self):
985-
for start_data, expected_result in self.EXPECTED_RESULTS:
986-
start_series = Series(start_data)
987-
start_series[start_series == start_series[0]] = None
987+
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
988+
def test_coercion_with_setitem_and_series(self, start_data, expected_result):
989+
start_series = Series(start_data)
990+
start_series[start_series == start_series[0]] = None
988991

989-
expected_series = Series(expected_result)
990-
tm.assert_series_equal(start_series, expected_series)
992+
expected_series = Series(expected_result)
993+
tm.assert_series_equal(start_series, expected_series)
991994

992-
def test_coercion_with_loc_and_series(self):
993-
for start_data, expected_result in self.EXPECTED_RESULTS:
994-
start_series = Series(start_data)
995-
start_series.loc[start_series == start_series[0]] = None
995+
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
996+
def test_coercion_with_loc_and_series(self, start_data, expected_result):
997+
start_series = Series(start_data)
998+
start_series.loc[start_series == start_series[0]] = None
996999

997-
expected_series = Series(expected_result)
998-
tm.assert_series_equal(start_series, expected_series)
1000+
expected_series = Series(expected_result)
1001+
tm.assert_series_equal(start_series, expected_series)
9991002

10001003

10011004
class TestDataframeNoneCoercion:
@@ -1012,31 +1015,35 @@ class TestDataframeNoneCoercion:
10121015
(["foo", "bar", "baz"], [None, "bar", "baz"]),
10131016
]
10141017

1015-
def test_coercion_with_loc(self):
1016-
for start_data, expected_result in self.EXPECTED_SINGLE_ROW_RESULTS:
1017-
start_dataframe = DataFrame({"foo": start_data})
1018-
start_dataframe.loc[0, ["foo"]] = None
1018+
@pytest.mark.parametrize("expected", EXPECTED_SINGLE_ROW_RESULTS)
1019+
def test_coercion_with_loc(self, expected):
1020+
start_data, expected_result = expected
1021+
1022+
start_dataframe = DataFrame({"foo": start_data})
1023+
start_dataframe.loc[0, ["foo"]] = None
1024+
1025+
expected_dataframe = DataFrame({"foo": expected_result})
1026+
tm.assert_frame_equal(start_dataframe, expected_dataframe)
1027+
1028+
@pytest.mark.parametrize("expected", EXPECTED_SINGLE_ROW_RESULTS)
1029+
def test_coercion_with_setitem_and_dataframe(self, expected):
1030+
start_data, expected_result = expected
10191031

1020-
expected_dataframe = DataFrame({"foo": expected_result})
1021-
tm.assert_frame_equal(start_dataframe, expected_dataframe)
1032+
start_dataframe = DataFrame({"foo": start_data})
1033+
start_dataframe[start_dataframe["foo"] == start_dataframe["foo"][0]] = None
10221034

1023-
def test_coercion_with_setitem_and_dataframe(self):
1024-
for start_data, expected_result in self.EXPECTED_SINGLE_ROW_RESULTS:
1025-
start_dataframe = DataFrame({"foo": start_data})
1026-
start_dataframe[start_dataframe["foo"] == start_dataframe["foo"][0]] = None
1035+
expected_dataframe = DataFrame({"foo": expected_result})
1036+
tm.assert_frame_equal(start_dataframe, expected_dataframe)
10271037

1028-
expected_dataframe = DataFrame({"foo": expected_result})
1029-
tm.assert_frame_equal(start_dataframe, expected_dataframe)
1038+
@pytest.mark.parametrize("expected", EXPECTED_SINGLE_ROW_RESULTS)
1039+
def test_none_coercion_loc_and_dataframe(self, expected):
1040+
start_data, expected_result = expected
10301041

1031-
def test_none_coercion_loc_and_dataframe(self):
1032-
for start_data, expected_result in self.EXPECTED_SINGLE_ROW_RESULTS:
1033-
start_dataframe = DataFrame({"foo": start_data})
1034-
start_dataframe.loc[
1035-
start_dataframe["foo"] == start_dataframe["foo"][0]
1036-
] = None
1042+
start_dataframe = DataFrame({"foo": start_data})
1043+
start_dataframe.loc[start_dataframe["foo"] == start_dataframe["foo"][0]] = None
10371044

1038-
expected_dataframe = DataFrame({"foo": expected_result})
1039-
tm.assert_frame_equal(start_dataframe, expected_dataframe)
1045+
expected_dataframe = DataFrame({"foo": expected_result})
1046+
tm.assert_frame_equal(start_dataframe, expected_dataframe)
10401047

10411048
def test_none_coercion_mixed_dtypes(self):
10421049
start_dataframe = DataFrame(

0 commit comments

Comments
 (0)