Skip to content

Commit 6c2c1c8

Browse files
authored
BUG: frame.lookup with non-unique axes (#33045)
1 parent c440ee7 commit 6c2c1c8

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Indexing
337337
- Bug in :meth:`DataFrame.loc:` and :meth:`Series.loc` with a :class:`DatetimeIndex`, :class:`TimedeltaIndex`, or :class:`PeriodIndex` incorrectly allowing lookups of non-matching datetime-like dtypes (:issue:`32650`)
338338
- Bug in :meth:`Series.__getitem__` indexing with non-standard scalars, e.g. ``np.dtype`` (:issue:`32684`)
339339
- Bug in :class:`Index` constructor where an unhelpful error message was raised for ``numpy`` scalars (:issue:`33017`)
340+
- Bug in :meth:`DataFrame.lookup` incorrectly raising an ``AttributeError`` when ``frame.index`` or ``frame.columns`` is not unique; this will now raise a ``ValueError`` with a helpful error message (:issue:`33041`)
340341

341342
Missing
342343
^^^^^^^

pandas/core/frame.py

+3
Original file line numberDiff line numberDiff line change
@@ -3525,6 +3525,9 @@ def lookup(self, row_labels, col_labels) -> np.ndarray:
35253525
n = len(row_labels)
35263526
if n != len(col_labels):
35273527
raise ValueError("Row labels must have same size as column labels")
3528+
if not (self.index.is_unique and self.columns.is_unique):
3529+
# GH#33041
3530+
raise ValueError("DataFrame.lookup requires unique index and columns")
35283531

35293532
thresh = 1000
35303533
if not self._is_mixed_type or n > thresh:

pandas/tests/frame/indexing/test_indexing.py

+18
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,24 @@ def test_lookup_raises(self, float_frame):
14241424
with pytest.raises(ValueError, match="same size"):
14251425
float_frame.lookup(["a", "b", "c"], ["a"])
14261426

1427+
def test_lookup_requires_unique_axes(self):
1428+
# GH#33041 raise with a helpful error message
1429+
df = pd.DataFrame(np.random.randn(6).reshape(3, 2), columns=["A", "A"])
1430+
1431+
rows = [0, 1]
1432+
cols = ["A", "A"]
1433+
1434+
# homogeneous-dtype case
1435+
with pytest.raises(ValueError, match="requires unique index and columns"):
1436+
df.lookup(rows, cols)
1437+
with pytest.raises(ValueError, match="requires unique index and columns"):
1438+
df.T.lookup(cols, rows)
1439+
1440+
# heterogeneous dtype
1441+
df["B"] = 0
1442+
with pytest.raises(ValueError, match="requires unique index and columns"):
1443+
df.lookup(rows, cols)
1444+
14271445
def test_set_value(self, float_frame):
14281446
for idx in float_frame.index:
14291447
for col in float_frame.columns:

0 commit comments

Comments
 (0)