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 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
8 changes: 4 additions & 4 deletions asv_bench/benchmarks/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def setup(self, connection):
},
index=tm.makeStringIndex(N),
)
self.df.loc[1000:3000, "float_with_nan"] = np.nan
self.df.iloc[1000:3000, 1] = np.nan
self.df["date"] = self.df["datetime"].dt.date
self.df["time"] = self.df["datetime"].dt.time
self.df["datetime_string"] = self.df["datetime"].astype(str)
Expand Down Expand Up @@ -88,7 +88,7 @@ def setup(self, connection, dtype):
},
index=tm.makeStringIndex(N),
)
self.df.loc[1000:3000, "float_with_nan"] = np.nan
self.df.iloc[1000:3000, 1] = np.nan
self.df["date"] = self.df["datetime"].dt.date
self.df["time"] = self.df["datetime"].dt.time
self.df["datetime_string"] = self.df["datetime"].astype(str)
Expand Down Expand Up @@ -117,7 +117,7 @@ def setup(self):
},
index=tm.makeStringIndex(N),
)
self.df.loc[1000:3000, "float_with_nan"] = np.nan
self.df.iloc[1000:3000, 1] = np.nan
self.df["date"] = self.df["datetime"].dt.date
self.df["time"] = self.df["datetime"].dt.time
self.df["datetime_string"] = self.df["datetime"].astype(str)
Expand Down Expand Up @@ -164,7 +164,7 @@ def setup(self, dtype):
},
index=tm.makeStringIndex(N),
)
self.df.loc[1000:3000, "float_with_nan"] = np.nan
self.df.iloc[1000:3000, 1] = np.nan
self.df["date"] = self.df["datetime"].dt.date
self.df["time"] = self.df["datetime"].dt.time
self.df["datetime_string"] = self.df["datetime"].astype(str)
Expand Down
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 @@ -256,6 +256,7 @@ Removal of prior version deprecations/changes
- Enforced disallowing passing an integer ``fill_value`` to :meth:`DataFrame.shift` and :meth:`Series.shift`` with datetime64, timedelta64, or period dtypes (:issue:`32591`)
- 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 @@ -4189,12 +4189,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