Skip to content

Commit 4ea0840

Browse files
aneesh98datapythonistamroeschke
authored
BUG: Change FutureWarning to DeprecationWarning for inplace setitem with DataFrame.(i)loc (#50044)
Co-authored-by: Marc Garcia <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent d9dec94 commit 4ea0840

File tree

17 files changed

+47
-32
lines changed

17 files changed

+47
-32
lines changed

doc/source/whatsnew/v1.5.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Other
4848
as pandas works toward compatibility with SQLAlchemy 2.0.
4949

5050
- Reverted deprecation (:issue:`45324`) of behavior of :meth:`Series.__getitem__` and :meth:`Series.__setitem__` slicing with an integer :class:`Index`; this will remain positional (:issue:`49612`)
51+
- A ``FutureWarning`` raised when attempting to set values inplace with :meth:`DataFrame.loc` or :meth:`DataFrame.loc` has been changed to a ``DeprecationWarning`` (:issue:`48673`)
5152
-
5253

5354
.. ---------------------------------------------------------------------------

pandas/core/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
20262026
"array. To retain the old behavior, use either "
20272027
"`df[df.columns[i]] = newvals` or, if columns are non-unique, "
20282028
"`df.isetitem(i, newvals)`",
2029-
FutureWarning,
2029+
DeprecationWarning,
20302030
stacklevel=find_stack_level(),
20312031
)
20322032
# TODO: how to get future behavior?

pandas/tests/extension/base/setitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_setitem_frame_2d_values(self, data):
400400
warn = None
401401
if has_can_hold_element and not isinstance(data.dtype, PandasDtype):
402402
# PandasDtype excluded because it isn't *really* supported.
403-
warn = FutureWarning
403+
warn = DeprecationWarning
404404

405405
with tm.assert_produces_warning(warn, match=msg):
406406
df.iloc[:] = df

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def test_getitem_setitem_float_labels(self, using_array_manager):
785785
assert len(result) == 5
786786

787787
cp = df.copy()
788-
warn = FutureWarning if using_array_manager else None
788+
warn = DeprecationWarning if using_array_manager else None
789789
msg = "will attempt to set the values inplace"
790790
with tm.assert_produces_warning(warn, match=msg):
791791
cp.loc[1.0:5.0] = 0

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def test_setitem_frame_length_0_str_key(self, indexer):
408408

409409
def test_setitem_frame_duplicate_columns(self, using_array_manager):
410410
# GH#15695
411-
warn = FutureWarning if using_array_manager else None
411+
warn = DeprecationWarning if using_array_manager else None
412412
msg = "will attempt to set the values inplace"
413413

414414
cols = ["A", "B", "C"] * 2

pandas/tests/frame/indexing/test_where.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def test_where_datetime(self, using_array_manager):
384384
expected = df.copy()
385385
expected.loc[[0, 1], "A"] = np.nan
386386

387-
warn = FutureWarning if using_array_manager else None
387+
warn = DeprecationWarning if using_array_manager else None
388388
msg = "will attempt to set the values inplace"
389389
with tm.assert_produces_warning(warn, match=msg):
390390
expected.loc[:, "C"] = np.nan
@@ -571,7 +571,7 @@ def test_where_axis_multiple_dtypes(self, using_array_manager):
571571

572572
d2 = df.copy().drop(1, axis=1)
573573
expected = df.copy()
574-
warn = FutureWarning if using_array_manager else None
574+
warn = DeprecationWarning if using_array_manager else None
575575
msg = "will attempt to set the values inplace"
576576
with tm.assert_produces_warning(warn, match=msg):
577577
expected.loc[:, 1] = np.nan

pandas/tests/frame/methods/test_dropna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_dropna_with_duplicate_columns(self):
221221
df.iloc[0, 0] = np.nan
222222
df.iloc[1, 1] = np.nan
223223
msg = "will attempt to set the values inplace instead"
224-
with tm.assert_produces_warning(FutureWarning, match=msg):
224+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
225225
df.iloc[:, 3] = np.nan
226226
expected = df.dropna(subset=["A", "B", "C"], how="all")
227227
expected.columns = ["A", "A", "B", "C"]

pandas/tests/frame/methods/test_rename.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ def test_rename_nocopy(self, float_frame, using_copy_on_write):
178178

179179
# TODO(CoW) this also shouldn't warn in case of CoW, but the heuristic
180180
# checking if the array shares memory doesn't work if CoW happened
181-
with tm.assert_produces_warning(FutureWarning if using_copy_on_write else None):
181+
with tm.assert_produces_warning(
182+
DeprecationWarning if using_copy_on_write else None
183+
):
182184
# This loc setitem already happens inplace, so no warning
183185
# that this will change in the future
184186
renamed.loc[:, "foo"] = 1.0

pandas/tests/frame/methods/test_shift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def test_shift_duplicate_columns(self, using_array_manager):
372372

373373
warn = None
374374
if using_array_manager:
375-
warn = FutureWarning
375+
warn = DeprecationWarning
376376

377377
shifted = []
378378
for columns in column_lists:

pandas/tests/frame/test_constructors.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2604,7 +2604,9 @@ def check_views(c_only: bool = False):
26042604

26052605
# FIXME(GH#35417): until GH#35417, iloc.setitem into EA values does not preserve
26062606
# view, so we have to check in the other direction
2607-
with tm.assert_produces_warning(FutureWarning, match="will attempt to set"):
2607+
with tm.assert_produces_warning(
2608+
DeprecationWarning, match="will attempt to set"
2609+
):
26082610
df.iloc[:, 2] = pd.array([45, 46], dtype=c.dtype)
26092611
assert df.dtypes.iloc[2] == c.dtype
26102612
if not copy and not using_copy_on_write:

0 commit comments

Comments
 (0)