|
| 1 | +import re |
| 2 | + |
1 | 3 | import numpy as np
|
2 | 4 | import pytest
|
3 | 5 |
|
@@ -81,6 +83,67 @@ def test_getitem_list_missing_key(self):
|
81 | 83 | with pytest.raises(KeyError, match=r"\['y'\] not in index"):
|
82 | 84 | df[["x", "y", "z"]]
|
83 | 85 |
|
| 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 | + |
84 | 147 |
|
85 | 148 | class TestGetitemCallable:
|
86 | 149 | def test_getitem_callable(self, float_frame):
|
@@ -258,6 +321,13 @@ def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
|
258 | 321 | result.dtypes
|
259 | 322 | str(result)
|
260 | 323 |
|
| 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 | + |
261 | 331 |
|
262 | 332 | class TestGetitemSlice:
|
263 | 333 | def test_getitem_slice_float64(self, frame_or_series):
|
|
0 commit comments