-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: fix .loc.__setitem__ not raising when using too many indexers #44656
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
Conversation
DriesSchaumont
commented
Nov 28, 2021
•
edited
Loading
edited
- closes Series.loc/iloc[x, y] does not raise exception #13831
- tests added / passed
- Ensure all linting tests pass, see here for how to run them
- whatsnew entry
pandas/tests/indexing/test_loc.py
Outdated
df = DataFrame({"a": [10]}) | ||
msg = "Too many indexers" | ||
with pytest.raises(IndexingError, match=msg): | ||
df["a"].loc[0, 0] = 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you construct the series explicitly?
pandas/tests/indexing/test_loc.py
Outdated
@@ -2715,6 +2715,13 @@ def test_loc_getitem_multiindex_tuple_level(): | |||
assert result2 == 6 | |||
|
|||
|
|||
def test_loc_setitem_indexer_length(): | |||
df = DataFrame({"a": [10]}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you cover all of the tests in the OP (parameterize if possible)
bd23ab4
to
abb0dbf
Compare
abb0dbf
to
ca37c9c
Compare
pandas/tests/indexing/test_iloc.py
Outdated
) | ||
def test_iloc_setitem_indexer_length(self, ser, keys): | ||
# GH#13831 | ||
with pytest.raises(IndexError, match="too many indices for array"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the difference in error between ser.iloc[keys] = 1000
and ser.iloc[keys]
. IndexError
is the result of the fact that we let numpy handle this, while pandas raises IndexingError
. I was wondering if this should be made consistent? Us catching and re-raising from numpy seems ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but @DriesSchaumont I think we would want to catch that case & re-raise appropriately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC a while back we removed some checks (for perf) and let numpy exceptions surface.
Not knowing anything about the specific use case, in cases where we have a choice between raising IndexError vs IndexingError (i.e. either would be a reasonable choice) I'd rather raise IndexError.
@phofl @jbrockmendel good here? (ex the question) |
cc @phofl @jbrockmendel if any comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small comments
pandas/tests/indexing/test_loc.py
Outdated
ser.loc[keys] = 1000 | ||
|
||
with pytest.raises(IndexingError, match=msg): | ||
ser.loc[keys] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use indexer_sli fixture to share this test with the iloc one i think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, also moved the tests.
@phofl @jbrockmendel good here? |
|
||
def test_ser_list_indexer_exceeds_dimensions(indexer_sli): | ||
# GH#13831 | ||
if indexer_sli == tm.setitem: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could make an indexer_li
fixture
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
@phofl @jbrockmendel ok here? |
@DriesSchaumont can you merge master |
Should I move this to 1.5? |
doc/source/whatsnew/v1.4.0.rst
Outdated
@@ -809,6 +809,7 @@ Indexing | |||
- Bug in :meth:`DataFrame.loc.__setitem__` changing dtype when indexer was completely ``False`` (:issue:`37550`) | |||
- Bug in :meth:`IntervalIndex.get_indexer_non_unique` returning boolean mask instead of array of integers for a non unique and non monotonic index (:issue:`44084`) | |||
- Bug in :meth:`IntervalIndex.get_indexer_non_unique` not handling targets of ``dtype`` 'object' with NaNs correctly (:issue:`44482`) | |||
- Bug in :meth:`Series.loc.__setitem__` and :meth:`Series.loc.__getitem__` not raising when using multiple keys without using a :class:`MultiIndex` (:issue:`13831`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah let's move this to 1.5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I moved whatsnew
@DriesSchaumont if you can merge master and address comments |
@jreback @jbrockmendel merged master, please let me know which/if questions still need answering. |
thanks @DriesSchaumont very nice! |