Skip to content

Commit f13fb07

Browse files
authored
TST: collect indexing tests by method (#39980)
1 parent d25cb7f commit f13fb07

File tree

6 files changed

+587
-577
lines changed

6 files changed

+587
-577
lines changed

pandas/tests/frame/indexing/test_categorical.py

-293
This file was deleted.

pandas/tests/frame/indexing/test_getitem.py

+70
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -81,6 +83,67 @@ def test_getitem_list_missing_key(self):
8183
with pytest.raises(KeyError, match=r"\['y'\] not in index"):
8284
df[["x", "y", "z"]]
8385

86+
def test_getitem_list_duplicates(self):
87+
# GH#1943
88+
df = DataFrame(np.random.randn(4, 4), columns=list("AABC"))
89+
df.columns.name = "foo"
90+
91+
result = df[["B", "C"]]
92+
assert result.columns.name == "foo"
93+
94+
expected = df.iloc[:, 2:]
95+
tm.assert_frame_equal(result, expected)
96+
97+
def test_getitem_dupe_cols(self):
98+
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
99+
msg = "\"None of [Index(['baf'], dtype='object')] are in the [columns]\""
100+
with pytest.raises(KeyError, match=re.escape(msg)):
101+
df[["baf"]]
102+
103+
@pytest.mark.parametrize(
104+
"idx_type",
105+
[
106+
list,
107+
iter,
108+
Index,
109+
set,
110+
lambda l: dict(zip(l, range(len(l)))),
111+
lambda l: dict(zip(l, range(len(l)))).keys(),
112+
],
113+
ids=["list", "iter", "Index", "set", "dict", "dict_keys"],
114+
)
115+
@pytest.mark.parametrize("levels", [1, 2])
116+
def test_getitem_listlike(self, idx_type, levels, float_frame):
117+
# GH#21294
118+
119+
if levels == 1:
120+
frame, missing = float_frame, "food"
121+
else:
122+
# MultiIndex columns
123+
frame = DataFrame(
124+
np.random.randn(8, 3),
125+
columns=Index(
126+
[("foo", "bar"), ("baz", "qux"), ("peek", "aboo")],
127+
name=("sth", "sth2"),
128+
),
129+
)
130+
missing = ("good", "food")
131+
132+
keys = [frame.columns[1], frame.columns[0]]
133+
idx = idx_type(keys)
134+
idx_check = list(idx_type(keys))
135+
136+
result = frame[idx]
137+
138+
expected = frame.loc[:, idx_check]
139+
expected.columns.names = frame.columns.names
140+
141+
tm.assert_frame_equal(result, expected)
142+
143+
idx = idx_type(keys + [missing])
144+
with pytest.raises(KeyError, match="not in index"):
145+
frame[idx]
146+
84147

85148
class TestGetitemCallable:
86149
def test_getitem_callable(self, float_frame):
@@ -258,6 +321,13 @@ def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
258321
result.dtypes
259322
str(result)
260323

324+
def test_getitem_empty_frame_with_boolean(self):
325+
# Test for issue GH#11859
326+
327+
df = DataFrame()
328+
df2 = df[df > 0]
329+
tm.assert_frame_equal(df, df2)
330+
261331

262332
class TestGetitemSlice:
263333
def test_getitem_slice_float64(self, frame_or_series):

0 commit comments

Comments
 (0)