Skip to content
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

BUG: loc setitem with missing integer slices does not raise #30921

Closed
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.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ Indexing
- :meth:`MultiIndex.get_loc` can't find missing values when input includes missing values (:issue:`19132`)
- Bug in :meth:`Series.__setitem__` incorrectly assigning values with boolean indexer when the length of new data matches the number of ``True`` values and new data is not a ``Series`` or an ``np.array`` (:issue:`30567`)
- Bug in indexing with a :class:`PeriodIndex` incorrectly accepting integers representing years, use e.g. ``ser.loc["2007"]`` instead of ``ser.loc[2007]`` (:issue:`30763`)
- Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`Series.loc.__setitem__` not raising error when given integer slice does not exist in index (:issue:`26412`)
Copy link
Member

@gfyoung gfyoung Jan 15, 2020

Choose a reason for hiding this comment

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

Suggested change
- Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`Series.loc.__setitem__` not raising error when given integer slice does not exist in index (:issue:`26412`)
- Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`Series.loc.__setitem__` not raising error when given integer slice that do not exist in the index (:issue:`26412`)


Missing
^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2922,6 +2922,9 @@ def is_int(v):
self._validate_indexer("slice", key.step, kind),
)

if kind == "loc" and not is_null_slicer:
return self.slice_indexer(key.start, key.stop, key.step, kind=kind)

Copy link
Member

Choose a reason for hiding this comment

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

is any of the validation on L2931-2948 relevant? can some or all of it be removed?

This likely affects some tests, see how this is handled in #31840.

Copy link
Member Author

Choose a reason for hiding this comment

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

hey sorry I haven't had time to work on this.
will close this PR in favor of #31840.

# convert the slice to an indexer here

# if we are mixed and have integers
Expand Down
17 changes: 15 additions & 2 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ def test_loc_setitem_empty_append_raises(self):
with pytest.raises(KeyError, match=msg):
df.loc[[0, 1], "x"] = data

msg = "cannot copy sequence with size 2 to array axis with dimension 0"
with pytest.raises(ValueError, match=msg):
msg = "cannot do slice indexing on .* with these indexers"
with pytest.raises(TypeError, match=msg):
df.loc[0:2, "x"] = data

def test_indexing_zerodim_np_array(self):
Expand Down Expand Up @@ -985,6 +985,19 @@ def test_loc_setitem_float_intindex():
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("test_series", [False, True])
def test_loc_setitem_missing_integer_slice(test_series):
# GH 26412
df = DataFrame({"col1": [1, 2, 3]}, index=["a", "b", "c"])
msg = "cannot do slice indexing on .* with these indexers"
with pytest.raises(TypeError, match=msg):
Copy link
Member

Choose a reason for hiding this comment

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

Newline above this one

if test_series:
s = df["col1"]
s.loc[0:2] = 10
else:
df.loc[0:2] = 10


def test_loc_axis_1_slice():
# GH 10586
cols = [(yr, m) for yr in [2014, 2015] for m in [7, 8, 9, 10]]
Expand Down