Skip to content

Commit 1f135eb

Browse files
phofljreback
andauthored
Fixed regression for SettingWithCopyWarning showing incorrect stacklevel (#42633)
Co-authored-by: Jeff Reback <[email protected]>
1 parent 17f6cbb commit 1f135eb

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.3.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed regressions
2525
- Fixed regression in :meth:`DataFrame.isin` and :meth:`Series.isin` raising ``TypeError`` with nullable data containing at least one missing value (:issue:`42405`)
2626
- Regression in :func:`concat` between objects with bool dtype and integer dtype casting to object instead of to integer (:issue:`42092`)
2727
- Bug in :class:`Series` constructor not accepting a ``dask.Array`` (:issue:`38645`)
28+
- Fixed regression for ``SettingWithCopyWarning`` displaying incorrect stacklevel (:issue:`42570`)
2829
- Fixed regression in :func:`to_datetime` returning pd.NaT for inputs that produce duplicated values, when ``cache=True`` (:issue:`42259`)
2930

3031

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3745,7 +3745,7 @@ def _set_item_mgr(self, key, value: ArrayLike) -> None:
37453745
# try to set first as we want an invalid
37463746
# value exception to occur first
37473747
if len(self):
3748-
self._check_setitem_copy()
3748+
self._check_setitem_copy(stacklevel=5)
37493749

37503750
def _iset_item(self, loc: int, value) -> None:
37513751
arraylike = self._sanitize_column(value)

pandas/tests/indexing/test_chaining_and_caching.py

+10
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ def test_detect_chained_assignment_warnings_filter_and_dupe_cols(self):
435435
)
436436
tm.assert_frame_equal(df, expected)
437437

438+
@pytest.mark.parametrize("rhs", [3, DataFrame({0: [1, 2, 3, 4]})])
439+
def test_detect_chained_assignment_warning_stacklevel(self, rhs):
440+
# GH#42570
441+
df = DataFrame(np.arange(25).reshape(5, 5))
442+
chained = df.loc[:3]
443+
with option_context("chained_assignment", "warn"):
444+
with tm.assert_produces_warning(com.SettingWithCopyWarning) as t:
445+
chained[2] = rhs
446+
assert t[0].filename == __file__
447+
438448
# TODO(ArrayManager) fast_xs with array-like scalars is not yet working
439449
@td.skip_array_manager_not_yet_implemented
440450
def test_chained_getitem_with_lists(self):

0 commit comments

Comments
 (0)