Skip to content

PERF: pass through to numpy validation for iloc setitem #32257

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 9 commits into from
Feb 27, 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
15 changes: 3 additions & 12 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pandas.util._decorators import Appender

from pandas.core.dtypes.common import (
is_float,
is_integer,
is_iterator,
is_list_like,
Expand Down Expand Up @@ -1500,18 +1499,10 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
"""
Much simpler as we only have to deal with our valid types.
"""
labels = self.obj._get_axis(axis)

# make need to convert a float key
if isinstance(key, slice):
labels._validate_positional_slice(key)
return key

elif is_float(key):
# _validate_indexer call will always raise
labels._validate_indexer("positional", key, "iloc")
return key

self._validate_key(key, axis)
def _get_setitem_indexer(self, key):
# GH#32257 Fall through to let numnpy do validation
return key

# -------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

from pandas.tseries.offsets import BDay

# We pass through a TypeError raised by numpy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. you used iloc_msg in the other function (non-private); i would make these more verbose

slice_exception_msg

_slice_msg = "slice indices must be integers or None or have an __index__ method"


class TestGet:
def test_get(self, float_frame):
Expand Down Expand Up @@ -994,7 +997,8 @@ def test_getitem_setitem_fancy_exceptions(self, float_frame):
with pytest.raises(IndexingError, match="Too many indexers"):
ix[:, :, :]

with pytest.raises(IndexingError, match="Too many indexers"):
with pytest.raises(IndexError, match="too many indices for array"):
# GH#32257 we let numpy do validation, get their exception
ix[:, :, :] = 1

def test_getitem_setitem_boolean_misaligned(self, float_frame):
Expand Down Expand Up @@ -1073,7 +1077,7 @@ def test_getitem_setitem_float_labels(self):

cp = df.copy()

with pytest.raises(TypeError, match=msg):
with pytest.raises(TypeError, match=_slice_msg):
cp.iloc[1.0:5] = 0

with pytest.raises(TypeError, match=msg):
Expand Down
36 changes: 13 additions & 23 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import re

import numpy as np
import pytest

from pandas import DataFrame, Float64Index, Index, Int64Index, RangeIndex, Series
import pandas._testing as tm

# We pass through the error message from numpy
_slice_iloc_msg = re.escape(
"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) "
"and integer or boolean arrays are valid indices"
)


def gen_obj(klass, index):
if klass is Series:
Expand Down Expand Up @@ -62,11 +70,7 @@ def test_scalar_error(self, index_func):
with pytest.raises(TypeError, match=msg):
s.iloc[3.0]

msg = (
f"cannot do positional indexing on {type(i).__name__} with these "
r"indexers \[3\.0\] of type float"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(IndexError, match=_slice_iloc_msg):
s.iloc[3.0] = 0

@pytest.mark.parametrize(
Expand Down Expand Up @@ -133,12 +137,7 @@ def test_scalar_non_numeric(self, index_func, klass):
assert 3.0 not in s

# setting with a float fails with iloc
msg = (
r"cannot do (label|positional) indexing "
fr"on {type(i).__name__} with these indexers \[3\.0\] of "
"type float"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(IndexError, match=_slice_iloc_msg):
s.iloc[3.0] = 0

# setting with an indexer
Expand Down Expand Up @@ -327,12 +326,7 @@ def test_scalar_float(self, klass):
with pytest.raises(TypeError, match=msg):
s.iloc[3.0]

msg = (
"cannot do positional indexing "
fr"on {Float64Index.__name__} with these indexers \[3\.0\] of "
"type float"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(IndexError, match=_slice_iloc_msg):
s2.iloc[3.0] = 0

@pytest.mark.parametrize(
Expand Down Expand Up @@ -376,11 +370,7 @@ def test_slice_non_numeric(self, index_func, l, klass):
idxr(s)[l]

# setitem
msg = (
"cannot do positional indexing "
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
"type float"
)
msg = "slice indices must be integers or None or have an __index__ method"
with pytest.raises(TypeError, match=msg):
s.iloc[l] = 0

Expand All @@ -390,7 +380,7 @@ def test_slice_non_numeric(self, index_func, l, klass):
r"\[(3|4)(\.0)?\] "
r"of type (float|int)"
)
for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]:
for idxr in [lambda x: x.loc, lambda x: x]:
with pytest.raises(TypeError, match=msg):
idxr(s)[l] = 0

Expand Down