Skip to content

Commit 8ea52bb

Browse files
authored
DEPR: Enforce disallowing loc with positionals (#49345)
* DEPR: Enforce disallowing loc with positionals * Fix asv usage * Trigger CI
1 parent f9ff379 commit 8ea52bb

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

asv_bench/benchmarks/io/sql.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def setup(self, connection):
3838
},
3939
index=tm.makeStringIndex(N),
4040
)
41-
self.df.loc[1000:3000, "float_with_nan"] = np.nan
41+
self.df.iloc[1000:3000, 1] = np.nan
4242
self.df["date"] = self.df["datetime"].dt.date
4343
self.df["time"] = self.df["datetime"].dt.time
4444
self.df["datetime_string"] = self.df["datetime"].astype(str)
@@ -88,7 +88,7 @@ def setup(self, connection, dtype):
8888
},
8989
index=tm.makeStringIndex(N),
9090
)
91-
self.df.loc[1000:3000, "float_with_nan"] = np.nan
91+
self.df.iloc[1000:3000, 1] = np.nan
9292
self.df["date"] = self.df["datetime"].dt.date
9393
self.df["time"] = self.df["datetime"].dt.time
9494
self.df["datetime_string"] = self.df["datetime"].astype(str)
@@ -117,7 +117,7 @@ def setup(self):
117117
},
118118
index=tm.makeStringIndex(N),
119119
)
120-
self.df.loc[1000:3000, "float_with_nan"] = np.nan
120+
self.df.iloc[1000:3000, 1] = np.nan
121121
self.df["date"] = self.df["datetime"].dt.date
122122
self.df["time"] = self.df["datetime"].dt.time
123123
self.df["datetime_string"] = self.df["datetime"].astype(str)
@@ -164,7 +164,7 @@ def setup(self, dtype):
164164
},
165165
index=tm.makeStringIndex(N),
166166
)
167-
self.df.loc[1000:3000, "float_with_nan"] = np.nan
167+
self.df.iloc[1000:3000, 1] = np.nan
168168
self.df["date"] = self.df["datetime"].dt.date
169169
self.df["time"] = self.df["datetime"].dt.time
170170
self.df["datetime_string"] = self.df["datetime"].astype(str)

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ Removal of prior version deprecations/changes
256256
- Enforced disallowing passing an integer ``fill_value`` to :meth:`DataFrame.shift` and :meth:`Series.shift`` with datetime64, timedelta64, or period dtypes (:issue:`32591`)
257257
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
258258
- Enforced disallowing a tuple of column labels into :meth:`.DataFrameGroupBy.__getitem__` (:issue:`30546`)
259+
- Enforced disallowing setting values with ``.loc`` using a positional slice. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`)
259260
- Removed setting Categorical._codes directly (:issue:`41429`)
260261
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
261262
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)

pandas/core/indexes/base.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -4189,12 +4189,9 @@ def is_int(v):
41894189
elif is_positional:
41904190
if kind == "loc":
41914191
# GH#16121, GH#24612, GH#31810
4192-
warnings.warn(
4193-
"Slicing a positional slice with .loc is not supported, "
4194-
"and will raise TypeError in a future version. "
4192+
raise TypeError(
4193+
"Slicing a positional slice with .loc is not allowed, "
41954194
"Use .loc with labels or .iloc with positions instead.",
4196-
FutureWarning,
4197-
stacklevel=find_stack_level(),
41984195
)
41994196
indexer = key
42004197
else:

pandas/tests/indexing/test_loc.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -2795,16 +2795,13 @@ def test_loc_mixed_int_float():
27952795
assert result == 0
27962796

27972797

2798-
def test_loc_with_positional_slice_deprecation():
2798+
def test_loc_with_positional_slice_raises():
27992799
# GH#31840
28002800
ser = Series(range(4), index=["A", "B", "C", "D"])
28012801

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

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

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

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

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

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

28362833

0 commit comments

Comments
 (0)