Skip to content

Commit c17a020

Browse files
committed
bringing tests back from 1.1.x
1 parent 8156c42 commit c17a020

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,72 @@ def test_loc_named_tuple_for_midx(self):
14141414
)
14151415
tm.assert_frame_equal(result, expected)
14161416

1417+
def test_lookup_float(self, float_frame):
1418+
df = float_frame
1419+
rows = list(df.index) * len(df.columns)
1420+
cols = list(df.columns) * len(df.index)
1421+
result = df.lookup(rows, cols)
1422+
1423+
expected = np.array([df.loc[r, c] for r, c in zip(rows, cols)])
1424+
tm.assert_numpy_array_equal(result, expected)
1425+
1426+
def test_lookup_mixed(self, float_string_frame):
1427+
df = float_string_frame
1428+
rows = list(df.index) * len(df.columns)
1429+
cols = list(df.columns) * len(df.index)
1430+
result = df.lookup(rows, cols)
1431+
1432+
expected = np.array(
1433+
[df.loc[r, c] for r, c in zip(rows, cols)], dtype=np.object_
1434+
)
1435+
tm.assert_almost_equal(result, expected)
1436+
1437+
def test_lookup_bool(self):
1438+
df = DataFrame(
1439+
{
1440+
"label": ["a", "b", "a", "c"],
1441+
"mask_a": [True, True, False, True],
1442+
"mask_b": [True, False, False, False],
1443+
"mask_c": [False, True, False, True],
1444+
}
1445+
)
1446+
df["mask"] = df.lookup(df.index, "mask_" + df["label"])
1447+
1448+
exp_mask = np.array(
1449+
[df.loc[r, c] for r, c in zip(df.index, "mask_" + df["label"])]
1450+
)
1451+
1452+
tm.assert_series_equal(df["mask"], Series(exp_mask, name="mask"))
1453+
assert df["mask"].dtype == np.bool_
1454+
1455+
def test_lookup_raises(self, float_frame):
1456+
with pytest.raises(KeyError, match="'One or more row labels was not found'"):
1457+
float_frame.lookup(["xyz"], ["A"])
1458+
1459+
with pytest.raises(KeyError, match="'One or more column labels was not found'"):
1460+
float_frame.lookup([float_frame.index[0]], ["xyz"])
1461+
1462+
with pytest.raises(ValueError, match="same size"):
1463+
float_frame.lookup(["a", "b", "c"], ["a"])
1464+
1465+
def test_lookup_requires_unique_axes(self):
1466+
# GH#33041 raise with a helpful error message
1467+
df = DataFrame(np.random.Generator(6).reshape(3, 2), columns=["A", "A"])
1468+
1469+
rows = [0, 1]
1470+
cols = ["A", "A"]
1471+
1472+
# homogeneous-dtype case
1473+
with pytest.raises(ValueError, match="requires unique index and columns"):
1474+
df.lookup(rows, cols)
1475+
with pytest.raises(ValueError, match="requires unique index and columns"):
1476+
df.T.lookup(cols, rows)
1477+
1478+
# heterogeneous dtype
1479+
df["B"] = 0
1480+
with pytest.raises(ValueError, match="requires unique index and columns"):
1481+
df.lookup(rows, cols)
1482+
14171483
@pytest.mark.parametrize("indexer", [["a"], "a"])
14181484
@pytest.mark.parametrize("col", [{}, {"b": 1}])
14191485
def test_set_2d_casting_date_to_int(self, col, indexer):

0 commit comments

Comments
 (0)