Skip to content

DEPR: Enforce disallowing loc with positionals #49345

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 6 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
DEPR: Enforce disallowing loc with positionals
  • Loading branch information
mroeschke committed Oct 26, 2022
commit 3e173af582c0c10e206c88be01f4de85b0877745
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Removal of prior version deprecations/changes
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame``, including pickle support. (:issue:`30642`)
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
- Enforced disallowing a tuple of column labels into :meth:`.DataFrameGroupBy.__getitem__` (:issue:`30546`)
- Enforced disallowing setting values with ``.loc`` using a positional slice. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`)
- Removed setting Categorical._codes directly (:issue:`41429`)
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4198,12 +4198,9 @@ def is_int(v):
elif is_positional:
if kind == "loc":
# GH#16121, GH#24612, GH#31810
warnings.warn(
"Slicing a positional slice with .loc is not supported, "
"and will raise TypeError in a future version. "
raise TypeError(
"Slicing a positional slice with .loc is not allowed, "
"Use .loc with labels or .iloc with positions instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
indexer = key
else:
Expand Down
15 changes: 6 additions & 9 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2795,16 +2795,13 @@ def test_loc_mixed_int_float():
assert result == 0


def test_loc_with_positional_slice_deprecation():
def test_loc_with_positional_slice_raises():
# GH#31840
ser = Series(range(4), index=["A", "B", "C", "D"])

with tm.assert_produces_warning(FutureWarning):
with pytest.raises(TypeError, match="Slicing a positional slice with .loc"):
ser.loc[:3] = 2

expected = Series([2, 2, 2, 3], index=["A", "B", "C", "D"])
tm.assert_series_equal(ser, expected)


def test_loc_slice_disallows_positional():
# GH#16121, GH#24612, GH#31810
Expand All @@ -2822,15 +2819,15 @@ def test_loc_slice_disallows_positional():
with pytest.raises(TypeError, match=msg):
obj.loc[1:3]

with tm.assert_produces_warning(FutureWarning):
# GH#31840 deprecated incorrect behavior
with pytest.raises(TypeError, match="Slicing a positional slice with .loc"):
# GH#31840 enforce incorrect behavior
obj.loc[1:3] = 1

with pytest.raises(TypeError, match=msg):
df.loc[1:3, 1]

with tm.assert_produces_warning(FutureWarning):
# GH#31840 deprecated incorrect behavior
with pytest.raises(TypeError, match="Slicing a positional slice with .loc"):
# GH#31840 enforce incorrect behavior
df.loc[1:3, 1] = 2


Expand Down