Skip to content

Commit

Permalink
BUG: Change FutureWarning to DeprecationWarning for inplace setitem w…
Browse files Browse the repository at this point in the history
…ith DataFrame.(i)loc (#50044)

Co-authored-by: Marc Garcia <garcia.marc@gmail.com>
Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 18, 2023
1 parent d9dec94 commit 4ea0840
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 32 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.3.rst
Expand Up @@ -48,6 +48,7 @@ Other
as pandas works toward compatibility with SQLAlchemy 2.0.

- 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`)
- 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`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Expand Up @@ -2026,7 +2026,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
"array. To retain the old behavior, use either "
"`df[df.columns[i]] = newvals` or, if columns are non-unique, "
"`df.isetitem(i, newvals)`",
FutureWarning,
DeprecationWarning,
stacklevel=find_stack_level(),
)
# TODO: how to get future behavior?
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/setitem.py
Expand Up @@ -400,7 +400,7 @@ def test_setitem_frame_2d_values(self, data):
warn = None
if has_can_hold_element and not isinstance(data.dtype, PandasDtype):
# PandasDtype excluded because it isn't *really* supported.
warn = FutureWarning
warn = DeprecationWarning

with tm.assert_produces_warning(warn, match=msg):
df.iloc[:] = df
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Expand Up @@ -785,7 +785,7 @@ def test_getitem_setitem_float_labels(self, using_array_manager):
assert len(result) == 5

cp = df.copy()
warn = FutureWarning if using_array_manager else None
warn = DeprecationWarning if using_array_manager else None
msg = "will attempt to set the values inplace"
with tm.assert_produces_warning(warn, match=msg):
cp.loc[1.0:5.0] = 0
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Expand Up @@ -408,7 +408,7 @@ def test_setitem_frame_length_0_str_key(self, indexer):

def test_setitem_frame_duplicate_columns(self, using_array_manager):
# GH#15695
warn = FutureWarning if using_array_manager else None
warn = DeprecationWarning if using_array_manager else None
msg = "will attempt to set the values inplace"

cols = ["A", "B", "C"] * 2
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/indexing/test_where.py
Expand Up @@ -384,7 +384,7 @@ def test_where_datetime(self, using_array_manager):
expected = df.copy()
expected.loc[[0, 1], "A"] = np.nan

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

d2 = df.copy().drop(1, axis=1)
expected = df.copy()
warn = FutureWarning if using_array_manager else None
warn = DeprecationWarning if using_array_manager else None
msg = "will attempt to set the values inplace"
with tm.assert_produces_warning(warn, match=msg):
expected.loc[:, 1] = np.nan
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_dropna.py
Expand Up @@ -221,7 +221,7 @@ def test_dropna_with_duplicate_columns(self):
df.iloc[0, 0] = np.nan
df.iloc[1, 1] = np.nan
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.iloc[:, 3] = np.nan
expected = df.dropna(subset=["A", "B", "C"], how="all")
expected.columns = ["A", "A", "B", "C"]
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/frame/methods/test_rename.py
Expand Up @@ -178,7 +178,9 @@ def test_rename_nocopy(self, float_frame, using_copy_on_write):

# TODO(CoW) this also shouldn't warn in case of CoW, but the heuristic
# checking if the array shares memory doesn't work if CoW happened
with tm.assert_produces_warning(FutureWarning if using_copy_on_write else None):
with tm.assert_produces_warning(
DeprecationWarning if using_copy_on_write else None
):
# This loc setitem already happens inplace, so no warning
# that this will change in the future
renamed.loc[:, "foo"] = 1.0
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_shift.py
Expand Up @@ -372,7 +372,7 @@ def test_shift_duplicate_columns(self, using_array_manager):

warn = None
if using_array_manager:
warn = FutureWarning
warn = DeprecationWarning

shifted = []
for columns in column_lists:
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/frame/test_constructors.py
Expand Up @@ -2604,7 +2604,9 @@ def check_views(c_only: bool = False):

# FIXME(GH#35417): until GH#35417, iloc.setitem into EA values does not preserve
# view, so we have to check in the other direction
with tm.assert_produces_warning(FutureWarning, match="will attempt to set"):
with tm.assert_produces_warning(
DeprecationWarning, match="will attempt to set"
):
df.iloc[:, 2] = pd.array([45, 46], dtype=c.dtype)
assert df.dtypes.iloc[2] == c.dtype
if not copy and not using_copy_on_write:
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/frame/test_nonunique_indexes.py
Expand Up @@ -323,7 +323,9 @@ def test_dup_columns_across_dtype(self):
def test_set_value_by_index(self, using_array_manager):
# See gh-12344
warn = (
FutureWarning if using_array_manager and not is_platform_windows() else None
DeprecationWarning
if using_array_manager and not is_platform_windows()
else None
)
msg = "will attempt to set the values inplace"

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_stack_unstack.py
Expand Up @@ -23,7 +23,7 @@

class TestDataFrameReshape:
def test_stack_unstack(self, float_frame, using_array_manager):
warn = FutureWarning if using_array_manager else None
warn = DeprecationWarning if using_array_manager else None
msg = "will attempt to set the values inplace"

df = float_frame.copy()
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/multiindex/test_loc.py
Expand Up @@ -541,9 +541,9 @@ def test_loc_setitem_single_column_slice():
)
expected = df.copy()
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, "B"] = np.arange(4)
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
expected.iloc[:, 2] = np.arange(4)
tm.assert_frame_equal(df, expected)

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexing/test_iloc.py
Expand Up @@ -84,7 +84,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key, using_array_manage
overwrite = isinstance(key, slice) and key == slice(None)
warn = None
if overwrite:
warn = FutureWarning
warn = DeprecationWarning
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(warn, match=msg):
indexer(df)[key, 0] = cat
Expand All @@ -108,7 +108,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key, using_array_manage
frame = DataFrame({0: np.array([0, 1, 2], dtype=object), 1: range(3)})
df = frame.copy()
orig_vals = df.values
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
indexer(df)[key, 0] = cat
expected = DataFrame({0: cat, 1: range(3)})
tm.assert_frame_equal(df, expected)
Expand Down Expand Up @@ -904,7 +904,7 @@ def test_iloc_setitem_categorical_updates_inplace(self, using_copy_on_write):

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

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

expected_df = DataFrame(
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexing/test_indexing.py
Expand Up @@ -550,15 +550,15 @@ def test_astype_assignment(self):

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

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

# GH5702 (loc)
df = df_orig.copy()
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, "A"] = df.loc[:, "A"].astype(np.int64)
expected = DataFrame(
[[1, "2", "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
)
tm.assert_frame_equal(df, expected)

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

df = DataFrame({"A": [1.0, 2.0, 3.0, 4.0]})
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, "A"] = df["A"].astype(np.int64)
expected = DataFrame({"A": [1, 2, 3, 4]})
tm.assert_frame_equal(df, expected)
Expand Down
22 changes: 15 additions & 7 deletions pandas/tests/indexing/test_loc.py
Expand Up @@ -368,7 +368,7 @@ def test_loc_setitem_dtype(self):
df = DataFrame({"id": ["A"], "a": [1.2], "b": [0.0], "c": [-2.5]})
cols = ["a", "b", "c"]
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, cols] = df.loc[:, cols].astype("float32")

expected = DataFrame(
Expand Down Expand Up @@ -633,11 +633,11 @@ def test_loc_setitem_consistency_slice_column_len(self):
df = DataFrame(values, index=mi, columns=cols)

msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, ("Respondent", "StartDate")] = to_datetime(
df.loc[:, ("Respondent", "StartDate")]
)
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, ("Respondent", "EndDate")] = to_datetime(
df.loc[:, ("Respondent", "EndDate")]
)
Expand Down Expand Up @@ -720,7 +720,7 @@ def test_loc_setitem_frame_with_reindex_mixed(self):
df = DataFrame(index=[3, 5, 4], columns=["A", "B"], dtype=float)
df["B"] = "string"
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[[4, 3, 5], "A"] = np.array([1, 2, 3], dtype="int64")
ser = Series([2, 3, 1], index=[3, 5, 4], dtype="int64")
expected = DataFrame({"A": ser})
Expand All @@ -732,7 +732,7 @@ def test_loc_setitem_frame_with_inverted_slice(self):
df = DataFrame(index=[1, 2, 3], columns=["A", "B"], dtype=float)
df["B"] = "string"
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[slice(3, 0, -1), "A"] = np.array([1, 2, 3], dtype="int64")
expected = DataFrame({"A": [3, 2, 1], "B": "string"}, index=[1, 2, 3])
tm.assert_frame_equal(df, expected)
Expand Down Expand Up @@ -909,7 +909,7 @@ def test_loc_setitem_missing_columns(self, index, box, expected):

warn = None
if isinstance(index[0], slice) and index[0] == slice(None):
warn = FutureWarning
warn = DeprecationWarning

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

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

result = df["Alpha"]
Expand Down Expand Up @@ -3211,3 +3211,11 @@ def test_getitem_loc_str_periodindex(self):
index = pd.period_range(start="2000", periods=20, freq="B")
series = Series(range(20), index=index)
assert series.loc["2000-01-14"] == 9

def test_deprecation_warnings_raised_loc(self):
# GH#48673
with tm.assert_produces_warning(DeprecationWarning):
values = np.arange(4).reshape(2, 2)
df = DataFrame(values, columns=["a", "b"])
new = np.array([10, 11]).astype(np.int16)
df.loc[:, "a"] = new
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_partial.py
Expand Up @@ -312,7 +312,7 @@ def test_partial_setting_frame(self, using_array_manager):
df = df_orig.copy()
df["B"] = df["B"].astype(np.float64)
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.assert_produces_warning(DeprecationWarning, match=msg):
df.loc[:, "B"] = df.loc[:, "A"]
tm.assert_frame_equal(df, expected)

Expand Down

0 comments on commit 4ea0840

Please sign in to comment.