Skip to content

Commit d84c57c

Browse files
authored
TST: collect indexing tests by method (#40042)
1 parent 30021ac commit d84c57c

File tree

11 files changed

+334
-333
lines changed

11 files changed

+334
-333
lines changed

pandas/tests/frame/indexing/test_indexing.py

+4-221
Original file line numberDiff line numberDiff line change
@@ -98,114 +98,7 @@ def test_setitem_list2(self):
9898
expected = Series(["1", "2"], df.columns, name=1)
9999
tm.assert_series_equal(result, expected)
100100

101-
def test_setitem_list_of_tuples(self, float_frame):
102-
tuples = list(zip(float_frame["A"], float_frame["B"]))
103-
float_frame["tuples"] = tuples
104-
105-
result = float_frame["tuples"]
106-
expected = Series(tuples, index=float_frame.index, name="tuples")
107-
tm.assert_series_equal(result, expected)
108-
109-
@pytest.mark.parametrize(
110-
"columns,box,expected",
111-
[
112-
(
113-
["A", "B", "C", "D"],
114-
7,
115-
DataFrame(
116-
[[7, 7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]],
117-
columns=["A", "B", "C", "D"],
118-
),
119-
),
120-
(
121-
["C", "D"],
122-
[7, 8],
123-
DataFrame(
124-
[[1, 2, 7, 8], [3, 4, 7, 8], [5, 6, 7, 8]],
125-
columns=["A", "B", "C", "D"],
126-
),
127-
),
128-
(
129-
["A", "B", "C"],
130-
np.array([7, 8, 9], dtype=np.int64),
131-
DataFrame([[7, 8, 9], [7, 8, 9], [7, 8, 9]], columns=["A", "B", "C"]),
132-
),
133-
(
134-
["B", "C", "D"],
135-
[[7, 8, 9], [10, 11, 12], [13, 14, 15]],
136-
DataFrame(
137-
[[1, 7, 8, 9], [3, 10, 11, 12], [5, 13, 14, 15]],
138-
columns=["A", "B", "C", "D"],
139-
),
140-
),
141-
(
142-
["C", "A", "D"],
143-
np.array([[7, 8, 9], [10, 11, 12], [13, 14, 15]], dtype=np.int64),
144-
DataFrame(
145-
[[8, 2, 7, 9], [11, 4, 10, 12], [14, 6, 13, 15]],
146-
columns=["A", "B", "C", "D"],
147-
),
148-
),
149-
(
150-
["A", "C"],
151-
DataFrame([[7, 8], [9, 10], [11, 12]], columns=["A", "C"]),
152-
DataFrame(
153-
[[7, 2, 8], [9, 4, 10], [11, 6, 12]], columns=["A", "B", "C"]
154-
),
155-
),
156-
],
157-
)
158-
def test_setitem_list_missing_columns(self, columns, box, expected):
159-
# GH 29334
160-
df = DataFrame([[1, 2], [3, 4], [5, 6]], columns=["A", "B"])
161-
df[columns] = box
162-
tm.assert_frame_equal(df, expected)
163-
164-
def test_setitem_multi_index(self):
165-
# GH7655, test that assigning to a sub-frame of a frame
166-
# with multi-index columns aligns both rows and columns
167-
it = ["jim", "joe", "jolie"], ["first", "last"], ["left", "center", "right"]
168-
169-
cols = MultiIndex.from_product(it)
170-
index = date_range("20141006", periods=20)
171-
vals = np.random.randint(1, 1000, (len(index), len(cols)))
172-
df = DataFrame(vals, columns=cols, index=index)
173-
174-
i, j = df.index.values.copy(), it[-1][:]
175-
176-
np.random.shuffle(i)
177-
df["jim"] = df["jolie"].loc[i, ::-1]
178-
tm.assert_frame_equal(df["jim"], df["jolie"])
179-
180-
np.random.shuffle(j)
181-
df[("joe", "first")] = df[("jolie", "last")].loc[i, j]
182-
tm.assert_frame_equal(df[("joe", "first")], df[("jolie", "last")])
183-
184-
np.random.shuffle(j)
185-
df[("joe", "last")] = df[("jolie", "first")].loc[i, j]
186-
tm.assert_frame_equal(df[("joe", "last")], df[("jolie", "first")])
187-
188-
@pytest.mark.parametrize(
189-
"cols, values, expected",
190-
[
191-
(["C", "D", "D", "a"], [1, 2, 3, 4], 4), # with duplicates
192-
(["D", "C", "D", "a"], [1, 2, 3, 4], 4), # mixed order
193-
(["C", "B", "B", "a"], [1, 2, 3, 4], 4), # other duplicate cols
194-
(["C", "B", "a"], [1, 2, 3], 3), # no duplicates
195-
(["B", "C", "a"], [3, 2, 1], 1), # alphabetical order
196-
(["C", "a", "B"], [3, 2, 1], 2), # in the middle
197-
],
198-
)
199-
def test_setitem_same_column(self, cols, values, expected):
200-
# GH 23239
201-
df = DataFrame([values], columns=cols)
202-
df["a"] = df["a"]
203-
result = df["a"].values[0]
204-
assert result == expected
205-
206-
def test_getitem_boolean(
207-
self, float_string_frame, mixed_float_frame, mixed_int_frame, datetime_frame
208-
):
101+
def test_getitem_boolean(self, mixed_float_frame, mixed_int_frame, datetime_frame):
209102
# boolean indexing
210103
d = datetime_frame.index[10]
211104
indexer = datetime_frame.index > d
@@ -242,12 +135,9 @@ def test_getitem_boolean(
242135
# test df[df > 0]
243136
for df in [
244137
datetime_frame,
245-
float_string_frame,
246138
mixed_float_frame,
247139
mixed_int_frame,
248140
]:
249-
if df is float_string_frame:
250-
continue
251141

252142
data = df._get_numeric_data()
253143
bif = df[df > 0]
@@ -348,6 +238,7 @@ def test_getitem_ix_mixed_integer(self):
348238
expected = df.loc[Index([1, 10])]
349239
tm.assert_frame_equal(result, expected)
350240

241+
def test_getitem_ix_mixed_integer2(self):
351242
# 11320
352243
df = DataFrame(
353244
{
@@ -419,6 +310,7 @@ def test_setitem(self, float_frame):
419310
assert smaller["col10"].dtype == np.object_
420311
assert (smaller["col10"] == ["1", "2"]).all()
421312

313+
def test_setitem2(self):
422314
# dtype changing GH4204
423315
df = DataFrame([[0, 0]])
424316
df.iloc[0] = np.nan
@@ -508,34 +400,6 @@ def test_setitem_cast(self, float_frame):
508400
float_frame["something"] = 2.5
509401
assert float_frame["something"].dtype == np.float64
510402

511-
# GH 7704
512-
# dtype conversion on setting
513-
df = DataFrame(np.random.rand(30, 3), columns=tuple("ABC"))
514-
df["event"] = np.nan
515-
df.loc[10, "event"] = "foo"
516-
result = df.dtypes
517-
expected = Series(
518-
[np.dtype("float64")] * 3 + [np.dtype("object")],
519-
index=["A", "B", "C", "event"],
520-
)
521-
tm.assert_series_equal(result, expected)
522-
523-
# Test that data type is preserved . #5782
524-
df = DataFrame({"one": np.arange(6, dtype=np.int8)})
525-
df.loc[1, "one"] = 6
526-
assert df.dtypes.one == np.dtype(np.int8)
527-
df.one = np.int8(7)
528-
assert df.dtypes.one == np.dtype(np.int8)
529-
530-
def test_setitem_boolean_column(self, float_frame):
531-
expected = float_frame.copy()
532-
mask = float_frame["A"] > 0
533-
534-
float_frame.loc[mask, "B"] = 0
535-
expected.values[mask.values, 1] = 0
536-
537-
tm.assert_frame_equal(float_frame, expected)
538-
539403
def test_setitem_corner(self, float_frame):
540404
# corner case
541405
df = DataFrame({"B": [1.0, 2.0, 3.0], "C": ["a", "b", "c"]}, index=np.arange(3))
@@ -908,17 +772,6 @@ def test_getitem_setitem_float_labels(self):
908772
result = cp.loc[1.0:5.0]
909773
assert (result == 0).values.all()
910774

911-
def test_setitem_single_column_mixed(self):
912-
df = DataFrame(
913-
np.random.randn(5, 3),
914-
index=["a", "b", "c", "d", "e"],
915-
columns=["foo", "bar", "baz"],
916-
)
917-
df["str"] = "qux"
918-
df.loc[df.index[::2], "str"] = np.nan
919-
expected = np.array([np.nan, "qux", np.nan, "qux", np.nan], dtype=object)
920-
tm.assert_almost_equal(df["str"].values, expected)
921-
922775
def test_setitem_single_column_mixed_datetime(self):
923776
df = DataFrame(
924777
np.random.randn(5, 3),
@@ -1182,24 +1035,6 @@ def test_iloc_col(self):
11821035
expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
11831036
tm.assert_frame_equal(result, expected)
11841037

1185-
def test_iloc_duplicates(self):
1186-
1187-
df = DataFrame(np.random.rand(3, 3), columns=list("ABC"), index=list("aab"))
1188-
1189-
result = df.iloc[0]
1190-
assert isinstance(result, Series)
1191-
tm.assert_almost_equal(result.values, df.values[0])
1192-
1193-
result = df.T.iloc[:, 0]
1194-
assert isinstance(result, Series)
1195-
tm.assert_almost_equal(result.values, df.values[0])
1196-
1197-
# #2259
1198-
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=[1, 1, 2])
1199-
result = df.iloc[:, [0]]
1200-
expected = df.take([0], axis=1)
1201-
tm.assert_frame_equal(result, expected)
1202-
12031038
def test_loc_duplicates(self):
12041039
# gh-17105
12051040

@@ -1227,10 +1062,6 @@ def test_loc_duplicates(self):
12271062
df.loc[trange[bool_idx], "A"] += 6
12281063
tm.assert_frame_equal(df, expected)
12291064

1230-
def test_set_dataframe_column_ns_dtype(self):
1231-
x = DataFrame([datetime.now(), datetime.now()])
1232-
assert x[0].dtype == np.dtype("M8[ns]")
1233-
12341065
def test_setitem_with_unaligned_tz_aware_datetime_column(self):
12351066
# GH 12981
12361067
# Assignment of unaligned offset-aware datetime series.
@@ -1266,33 +1097,6 @@ def test_loc_setitem_datetimelike_with_inference(self):
12661097
)
12671098
tm.assert_series_equal(result, expected)
12681099

1269-
def test_loc_getitem_index_namedtuple(self):
1270-
from collections import namedtuple
1271-
1272-
IndexType = namedtuple("IndexType", ["a", "b"])
1273-
idx1 = IndexType("foo", "bar")
1274-
idx2 = IndexType("baz", "bof")
1275-
index = Index([idx1, idx2], name="composite_index", tupleize_cols=False)
1276-
df = DataFrame([(1, 2), (3, 4)], index=index, columns=["A", "B"])
1277-
1278-
result = df.loc[IndexType("foo", "bar")]["A"]
1279-
assert result == 1
1280-
1281-
@pytest.mark.parametrize("tpl", [(1,), (1, 2)])
1282-
def test_loc_getitem_index_single_double_tuples(self, tpl):
1283-
# GH 20991
1284-
idx = Index(
1285-
[(1,), (1, 2)],
1286-
name="A",
1287-
tupleize_cols=False,
1288-
)
1289-
df = DataFrame(index=idx)
1290-
1291-
result = df.loc[[tpl]]
1292-
idx = Index([tpl], name="A", tupleize_cols=False)
1293-
expected = DataFrame(index=idx)
1294-
tm.assert_frame_equal(result, expected)
1295-
12961100
def test_getitem_boolean_indexing_mixed(self):
12971101
df = DataFrame(
12981102
{
@@ -1346,7 +1150,7 @@ def test_type_error_multiindex(self):
13461150
data=[[0, 0, 1, 2], [1, 0, 3, 4], [0, 1, 1, 2], [1, 1, 3, 4]],
13471151
)
13481152
dg = df.pivot_table(index="i", columns="c", values=["x", "y"])
1349-
1153+
# TODO: Is this test for pivot_table?
13501154
with pytest.raises(TypeError, match="unhashable type"):
13511155
dg[:, 0]
13521156

@@ -1366,27 +1170,6 @@ def test_type_error_multiindex(self):
13661170
result = dg["x", 0]
13671171
tm.assert_series_equal(result, expected)
13681172

1369-
def test_loc_getitem_interval_index(self):
1370-
# GH 19977
1371-
index = pd.interval_range(start=0, periods=3)
1372-
df = DataFrame(
1373-
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=index, columns=["A", "B", "C"]
1374-
)
1375-
1376-
expected = 1
1377-
result = df.loc[0.5, "A"]
1378-
tm.assert_almost_equal(result, expected)
1379-
1380-
index = pd.interval_range(start=0, periods=3, closed="both")
1381-
df = DataFrame(
1382-
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=index, columns=["A", "B", "C"]
1383-
)
1384-
1385-
index_exp = pd.interval_range(start=0, periods=2, freq=1, closed="both")
1386-
expected = Series([1, 4], index=index_exp, name="A")
1387-
result = df.loc[1, "A"]
1388-
tm.assert_series_equal(result, expected)
1389-
13901173
def test_getitem_interval_index_partial_indexing(self):
13911174
# GH#36490
13921175
df = DataFrame(

0 commit comments

Comments
 (0)