Skip to content

TST: collect indexing tests by method #39980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 0 additions & 293 deletions pandas/tests/frame/indexing/test_categorical.py

This file was deleted.

70 changes: 70 additions & 0 deletions pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

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

def test_getitem_list_duplicates(self):
# GH#1943
df = DataFrame(np.random.randn(4, 4), columns=list("AABC"))
df.columns.name = "foo"

result = df[["B", "C"]]
assert result.columns.name == "foo"

expected = df.iloc[:, 2:]
tm.assert_frame_equal(result, expected)

def test_getitem_dupe_cols(self):
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
msg = "\"None of [Index(['baf'], dtype='object')] are in the [columns]\""
with pytest.raises(KeyError, match=re.escape(msg)):
df[["baf"]]

@pytest.mark.parametrize(
"idx_type",
[
list,
iter,
Index,
set,
lambda l: dict(zip(l, range(len(l)))),
lambda l: dict(zip(l, range(len(l)))).keys(),
],
ids=["list", "iter", "Index", "set", "dict", "dict_keys"],
)
@pytest.mark.parametrize("levels", [1, 2])
def test_getitem_listlike(self, idx_type, levels, float_frame):
# GH#21294

if levels == 1:
frame, missing = float_frame, "food"
else:
# MultiIndex columns
frame = DataFrame(
np.random.randn(8, 3),
columns=Index(
[("foo", "bar"), ("baz", "qux"), ("peek", "aboo")],
name=("sth", "sth2"),
),
)
missing = ("good", "food")

keys = [frame.columns[1], frame.columns[0]]
idx = idx_type(keys)
idx_check = list(idx_type(keys))

result = frame[idx]

expected = frame.loc[:, idx_check]
expected.columns.names = frame.columns.names

tm.assert_frame_equal(result, expected)

idx = idx_type(keys + [missing])
with pytest.raises(KeyError, match="not in index"):
frame[idx]


class TestGetitemCallable:
def test_getitem_callable(self, float_frame):
Expand Down Expand Up @@ -258,6 +321,13 @@ def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
result.dtypes
str(result)

def test_getitem_empty_frame_with_boolean(self):
# Test for issue GH#11859

df = DataFrame()
df2 = df[df > 0]
tm.assert_frame_equal(df, df2)


class TestGetitemSlice:
def test_getitem_slice_float64(self, frame_or_series):
Expand Down
Loading