Skip to content

BUG: frame.lookup with non-unique axes #33045

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 2 commits into from
Mar 26, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ Indexing
- 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`)
- Bug in :meth:`Series.__getitem__` indexing with non-standard scalars, e.g. ``np.dtype`` (:issue:`32684`)
- Bug in :class:`Index` constructor where an unhelpful error message was raised for ``numpy`` scalars (:issue:`33017`)
- 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`)

Missing
^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3525,6 +3525,9 @@ def lookup(self, row_labels, col_labels) -> np.ndarray:
n = len(row_labels)
if n != len(col_labels):
raise ValueError("Row labels must have same size as column labels")
if not (self.index.is_unique and self.columns.is_unique):
# GH#33041
raise ValueError("DataFrame.lookup requires unique index and columns")

thresh = 1000
if not self._is_mixed_type or n > thresh:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,24 @@ def test_lookup_raises(self, float_frame):
with pytest.raises(ValueError, match="same size"):
float_frame.lookup(["a", "b", "c"], ["a"])

def test_lookup_requires_unique_axes(self):
# GH#33041 raise with a helpful error message
df = pd.DataFrame(np.random.randn(6).reshape(3, 2), columns=["A", "A"])

rows = [0, 1]
cols = ["A", "A"]

# homogeneous-dtype case
with pytest.raises(ValueError, match="requires unique index and columns"):
df.lookup(rows, cols)
with pytest.raises(ValueError, match="requires unique index and columns"):
df.T.lookup(cols, rows)

# heterogeneous dtype
df["B"] = 0
with pytest.raises(ValueError, match="requires unique index and columns"):
df.lookup(rows, cols)

def test_set_value(self, float_frame):
for idx in float_frame.index:
for col in float_frame.columns:
Expand Down