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 <garcia.marc@gmail.com> Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
1 parent d9dec94 commit 4ea0840

17 files changed

+47
-32
lines changed

doc/source/whatsnew/v1.5.3.rst

+1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+1-1
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

+3-1
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

+1-1
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

+3-1
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:

pandas/tests/frame/test_nonunique_indexes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ def test_dup_columns_across_dtype(self):
323323
def test_set_value_by_index(self, using_array_manager):
324324
# See gh-12344
325325
warn = (
326-
FutureWarning if using_array_manager and not is_platform_windows() else None
326+
DeprecationWarning
327+
if using_array_manager and not is_platform_windows()
328+
else None
327329
)
328330
msg = "will attempt to set the values inplace"
329331

pandas/tests/frame/test_stack_unstack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
class TestDataFrameReshape:
2525
def test_stack_unstack(self, float_frame, using_array_manager):
26-
warn = FutureWarning if using_array_manager else None
26+
warn = DeprecationWarning if using_array_manager else None
2727
msg = "will attempt to set the values inplace"
2828

2929
df = float_frame.copy()

pandas/tests/indexing/multiindex/test_loc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ def test_loc_setitem_single_column_slice():
541541
)
542542
expected = df.copy()
543543
msg = "will attempt to set the values inplace instead"
544-
with tm.assert_produces_warning(FutureWarning, match=msg):
544+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
545545
df.loc[:, "B"] = np.arange(4)
546-
with tm.assert_produces_warning(FutureWarning, match=msg):
546+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
547547
expected.iloc[:, 2] = np.arange(4)
548548
tm.assert_frame_equal(df, expected)
549549

pandas/tests/indexing/test_iloc.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key, using_array_manage
8484
overwrite = isinstance(key, slice) and key == slice(None)
8585
warn = None
8686
if overwrite:
87-
warn = FutureWarning
87+
warn = DeprecationWarning
8888
msg = "will attempt to set the values inplace instead"
8989
with tm.assert_produces_warning(warn, match=msg):
9090
indexer(df)[key, 0] = cat
@@ -108,7 +108,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key, using_array_manage
108108
frame = DataFrame({0: np.array([0, 1, 2], dtype=object), 1: range(3)})
109109
df = frame.copy()
110110
orig_vals = df.values
111-
with tm.assert_produces_warning(FutureWarning, match=msg):
111+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
112112
indexer(df)[key, 0] = cat
113113
expected = DataFrame({0: cat, 1: range(3)})
114114
tm.assert_frame_equal(df, expected)
@@ -904,7 +904,7 @@ def test_iloc_setitem_categorical_updates_inplace(self, using_copy_on_write):
904904

905905
# This should modify our original values in-place
906906
msg = "will attempt to set the values inplace instead"
907-
with tm.assert_produces_warning(FutureWarning, match=msg):
907+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
908908
df.iloc[:, 0] = cat[::-1]
909909

910910
if not using_copy_on_write:
@@ -1314,7 +1314,7 @@ def test_iloc_setitem_dtypes_duplicate_columns(
13141314
# GH#22035
13151315
df = DataFrame([[init_value, "str", "str2"]], columns=["a", "b", "b"])
13161316
msg = "will attempt to set the values inplace instead"
1317-
with tm.assert_produces_warning(FutureWarning, match=msg):
1317+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
13181318
df.iloc[:, 0] = df.iloc[:, 0].astype(dtypes)
13191319

13201320
expected_df = DataFrame(

pandas/tests/indexing/test_indexing.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -550,15 +550,15 @@ def test_astype_assignment(self):
550550

551551
df = df_orig.copy()
552552
msg = "will attempt to set the values inplace instead"
553-
with tm.assert_produces_warning(FutureWarning, match=msg):
553+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
554554
df.iloc[:, 0:2] = df.iloc[:, 0:2].astype(np.int64)
555555
expected = DataFrame(
556556
[[1, 2, "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
557557
)
558558
tm.assert_frame_equal(df, expected)
559559

560560
df = df_orig.copy()
561-
with tm.assert_produces_warning(FutureWarning, match=msg):
561+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
562562
df.iloc[:, 0:2] = df.iloc[:, 0:2]._convert(datetime=True, numeric=True)
563563
expected = DataFrame(
564564
[[1, 2, "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
@@ -567,15 +567,15 @@ def test_astype_assignment(self):
567567

568568
# GH5702 (loc)
569569
df = df_orig.copy()
570-
with tm.assert_produces_warning(FutureWarning, match=msg):
570+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
571571
df.loc[:, "A"] = df.loc[:, "A"].astype(np.int64)
572572
expected = DataFrame(
573573
[[1, "2", "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
574574
)
575575
tm.assert_frame_equal(df, expected)
576576

577577
df = df_orig.copy()
578-
with tm.assert_produces_warning(FutureWarning, match=msg):
578+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
579579
df.loc[:, ["B", "C"]] = df.loc[:, ["B", "C"]].astype(np.int64)
580580
expected = DataFrame(
581581
[["1", 2, 3, ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
@@ -586,13 +586,13 @@ def test_astype_assignment_full_replacements(self):
586586
# full replacements / no nans
587587
df = DataFrame({"A": [1.0, 2.0, 3.0, 4.0]})
588588
msg = "will attempt to set the values inplace instead"
589-
with tm.assert_produces_warning(FutureWarning, match=msg):
589+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
590590
df.iloc[:, 0] = df["A"].astype(np.int64)
591591
expected = DataFrame({"A": [1, 2, 3, 4]})
592592
tm.assert_frame_equal(df, expected)
593593

594594
df = DataFrame({"A": [1.0, 2.0, 3.0, 4.0]})
595-
with tm.assert_produces_warning(FutureWarning, match=msg):
595+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
596596
df.loc[:, "A"] = df["A"].astype(np.int64)
597597
expected = DataFrame({"A": [1, 2, 3, 4]})
598598
tm.assert_frame_equal(df, expected)

pandas/tests/indexing/test_loc.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def test_loc_setitem_dtype(self):
368368
df = DataFrame({"id": ["A"], "a": [1.2], "b": [0.0], "c": [-2.5]})
369369
cols = ["a", "b", "c"]
370370
msg = "will attempt to set the values inplace instead"
371-
with tm.assert_produces_warning(FutureWarning, match=msg):
371+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
372372
df.loc[:, cols] = df.loc[:, cols].astype("float32")
373373

374374
expected = DataFrame(
@@ -633,11 +633,11 @@ def test_loc_setitem_consistency_slice_column_len(self):
633633
df = DataFrame(values, index=mi, columns=cols)
634634

635635
msg = "will attempt to set the values inplace instead"
636-
with tm.assert_produces_warning(FutureWarning, match=msg):
636+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
637637
df.loc[:, ("Respondent", "StartDate")] = to_datetime(
638638
df.loc[:, ("Respondent", "StartDate")]
639639
)
640-
with tm.assert_produces_warning(FutureWarning, match=msg):
640+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
641641
df.loc[:, ("Respondent", "EndDate")] = to_datetime(
642642
df.loc[:, ("Respondent", "EndDate")]
643643
)
@@ -720,7 +720,7 @@ def test_loc_setitem_frame_with_reindex_mixed(self):
720720
df = DataFrame(index=[3, 5, 4], columns=["A", "B"], dtype=float)
721721
df["B"] = "string"
722722
msg = "will attempt to set the values inplace instead"
723-
with tm.assert_produces_warning(FutureWarning, match=msg):
723+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
724724
df.loc[[4, 3, 5], "A"] = np.array([1, 2, 3], dtype="int64")
725725
ser = Series([2, 3, 1], index=[3, 5, 4], dtype="int64")
726726
expected = DataFrame({"A": ser})
@@ -732,7 +732,7 @@ def test_loc_setitem_frame_with_inverted_slice(self):
732732
df = DataFrame(index=[1, 2, 3], columns=["A", "B"], dtype=float)
733733
df["B"] = "string"
734734
msg = "will attempt to set the values inplace instead"
735-
with tm.assert_produces_warning(FutureWarning, match=msg):
735+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
736736
df.loc[slice(3, 0, -1), "A"] = np.array([1, 2, 3], dtype="int64")
737737
expected = DataFrame({"A": [3, 2, 1], "B": "string"}, index=[1, 2, 3])
738738
tm.assert_frame_equal(df, expected)
@@ -909,7 +909,7 @@ def test_loc_setitem_missing_columns(self, index, box, expected):
909909

910910
warn = None
911911
if isinstance(index[0], slice) and index[0] == slice(None):
912-
warn = FutureWarning
912+
warn = DeprecationWarning
913913

914914
msg = "will attempt to set the values inplace instead"
915915
with tm.assert_produces_warning(warn, match=msg):
@@ -1425,7 +1425,7 @@ def test_loc_setitem_single_row_categorical(self):
14251425
categories = Categorical(df["Alpha"], categories=["a", "b", "c"])
14261426

14271427
msg = "will attempt to set the values inplace instead"
1428-
with tm.assert_produces_warning(FutureWarning, match=msg):
1428+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
14291429
df.loc[:, "Alpha"] = categories
14301430

14311431
result = df["Alpha"]
@@ -3211,3 +3211,11 @@ def test_getitem_loc_str_periodindex(self):
32113211
index = pd.period_range(start="2000", periods=20, freq="B")
32123212
series = Series(range(20), index=index)
32133213
assert series.loc["2000-01-14"] == 9
3214+
3215+
def test_deprecation_warnings_raised_loc(self):
3216+
# GH#48673
3217+
with tm.assert_produces_warning(DeprecationWarning):
3218+
values = np.arange(4).reshape(2, 2)
3219+
df = DataFrame(values, columns=["a", "b"])
3220+
new = np.array([10, 11]).astype(np.int16)
3221+
df.loc[:, "a"] = new

pandas/tests/indexing/test_partial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def test_partial_setting_frame(self, using_array_manager):
312312
df = df_orig.copy()
313313
df["B"] = df["B"].astype(np.float64)
314314
msg = "will attempt to set the values inplace instead"
315-
with tm.assert_produces_warning(FutureWarning, match=msg):
315+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
316316
df.loc[:, "B"] = df.loc[:, "A"]
317317
tm.assert_frame_equal(df, expected)
318318

0 commit comments

Comments
 (0)