Skip to content

Commit f687f49

Browse files
add warning mode as well for 2.3
1 parent b2ab83a commit f687f49

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

pandas/core/indexing.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,11 +2575,21 @@ def __getitem__(self, key):
25752575
return super().__getitem__(key)
25762576

25772577
def __setitem__(self, key, value) -> None:
2578-
if not PYPY:
2578+
if not PYPY and using_copy_on_write():
25792579
if sys.getrefcount(self.obj) <= 2:
25802580
warnings.warn(
25812581
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
25822582
)
2583+
elif not PYPY and not using_copy_on_write():
2584+
ctr = sys.getrefcount(self.obj)
2585+
ref_count = 2
2586+
if not warn_copy_on_write() and _check_cacher(self.obj):
2587+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
2588+
ref_count += 1
2589+
if ctr <= ref_count:
2590+
warnings.warn(
2591+
_chained_assignment_warning_msg, FutureWarning, stacklevel=2
2592+
)
25832593

25842594
if self.ndim == 2 and not self._axes_are_unique:
25852595
# GH#33041 fall back to .loc
@@ -2606,11 +2616,21 @@ def _convert_key(self, key):
26062616
return key
26072617

26082618
def __setitem__(self, key, value) -> None:
2609-
if not PYPY:
2619+
if not PYPY and using_copy_on_write():
26102620
if sys.getrefcount(self.obj) <= 2:
26112621
warnings.warn(
26122622
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
26132623
)
2624+
elif not PYPY and not using_copy_on_write():
2625+
ctr = sys.getrefcount(self.obj)
2626+
ref_count = 2
2627+
if not warn_copy_on_write() and _check_cacher(self.obj):
2628+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
2629+
ref_count += 1
2630+
if ctr <= ref_count:
2631+
warnings.warn(
2632+
_chained_assignment_warning_msg, FutureWarning, stacklevel=2
2633+
)
26142634

26152635
return super().__setitem__(key, value)
26162636

pandas/tests/copy_view/test_chained_assignment_deprecation.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ def test_frame_setitem(indexer, using_copy_on_write):
177177
@pytest.mark.parametrize(
178178
"indexer", [0, [0, 1], slice(0, 2), np.array([True, False, True])]
179179
)
180-
def test_series_iloc_setitem(indexer, using_copy_on_write):
180+
def test_series_iloc_setitem(indexer):
181181
df = DataFrame({"a": [1, 2, 3], "b": 1})
182182

183-
if using_copy_on_write:
183+
with option_context("chained_assignment", "warn"):
184184
with tm.raises_chained_assignment_error():
185185
df["a"].iloc[indexer] = 0
186186

@@ -191,18 +191,20 @@ def test_series_iloc_setitem(indexer, using_copy_on_write):
191191
def test_frame_iloc_setitem(indexer, using_copy_on_write):
192192
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})
193193

194-
if using_copy_on_write:
195-
with tm.raises_chained_assignment_error():
194+
extra_warnings = () if using_copy_on_write else (SettingWithCopyWarning,)
195+
196+
with option_context("chained_assignment", "warn"):
197+
with tm.raises_chained_assignment_error(extra_warnings=extra_warnings):
196198
df[0:3].iloc[indexer] = 10
197199

198200

199201
@pytest.mark.parametrize(
200202
"indexer", [0, [0, 1], slice(0, 2), np.array([True, False, True])]
201203
)
202-
def test_series_loc_setitem(indexer, using_copy_on_write):
204+
def test_series_loc_setitem(indexer):
203205
df = DataFrame({"a": [1, 2, 3], "b": 1})
204206

205-
if using_copy_on_write:
207+
with option_context("chained_assignment", "warn"):
206208
with tm.raises_chained_assignment_error():
207209
df["a"].loc[indexer] = 0
208210

@@ -213,38 +215,40 @@ def test_series_loc_setitem(indexer, using_copy_on_write):
213215
def test_frame_loc_setitem(indexer, using_copy_on_write):
214216
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})
215217

216-
if using_copy_on_write:
217-
with tm.raises_chained_assignment_error():
218+
extra_warnings = () if using_copy_on_write else (SettingWithCopyWarning,)
219+
220+
with option_context("chained_assignment", "warn"):
221+
with tm.raises_chained_assignment_error(extra_warnings=extra_warnings):
218222
df[0:3].loc[indexer] = 10
219223

220224

221-
def test_series_at_setitem(using_copy_on_write):
225+
def test_series_at_setitem():
222226
df = DataFrame({"a": [1, 2, 3], "b": 1})
223227

224-
if using_copy_on_write:
228+
with option_context("chained_assignment", "warn"):
225229
with tm.raises_chained_assignment_error():
226230
df["a"].at[0] = 0
227231

228232

229-
def test_frame_at_setitem(using_copy_on_write):
233+
def test_frame_at_setitem():
230234
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})
231235

232-
if using_copy_on_write:
236+
with option_context("chained_assignment", "warn"):
233237
with tm.raises_chained_assignment_error():
234238
df[0:3].at[0, "a"] = 10
235239

236240

237-
def test_series_iat_setitem(using_copy_on_write):
241+
def test_series_iat_setitem():
238242
df = DataFrame({"a": [1, 2, 3], "b": 1})
239243

240-
if using_copy_on_write:
244+
with option_context("chained_assignment", "warn"):
241245
with tm.raises_chained_assignment_error():
242246
df["a"].iat[0] = 0
243247

244248

245-
def test_frame_iat_setitem(using_copy_on_write):
249+
def test_frame_iat_setitem():
246250
df = DataFrame({"a": [1, 2, 3, 4, 5], "b": 1})
247251

248-
if using_copy_on_write:
252+
with option_context("chained_assignment", "warn"):
249253
with tm.raises_chained_assignment_error():
250254
df[0:3].iat[0, 0] = 10

0 commit comments

Comments
 (0)