Skip to content

Backport PR #42633 on branch 1.3.x (Fixed regression for SettingWithCopyWarning showing incorrect stacklevel) #42678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.isin` and :meth:`Series.isin` raising ``TypeError`` with nullable data containing at least one missing value (:issue:`42405`)
- Regression in :func:`concat` between objects with bool dtype and integer dtype casting to object instead of to integer (:issue:`42092`)
- Bug in :class:`Series` constructor not accepting a ``dask.Array`` (:issue:`38645`)
- Fixed regression for ``SettingWithCopyWarning`` displaying incorrect stacklevel (:issue:`42570`)
- Fixed regression in :func:`to_datetime` returning pd.NaT for inputs that produce duplicated values, when ``cache=True`` (:issue:`42259`)


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3754,7 +3754,7 @@ def _set_item_mgr(self, key, value: ArrayLike) -> None:
# try to set first as we want an invalid
# value exception to occur first
if len(self):
self._check_setitem_copy()
self._check_setitem_copy(stacklevel=5)

def _iset_item(self, loc: int, value) -> None:
arraylike = self._sanitize_column(value)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ def test_detect_chained_assignment_warnings_filter_and_dupe_cols(self):
)
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("rhs", [3, DataFrame({0: [1, 2, 3, 4]})])
def test_detect_chained_assignment_warning_stacklevel(self, rhs):
# GH#42570
df = DataFrame(np.arange(25).reshape(5, 5))
chained = df.loc[:3]
with option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(com.SettingWithCopyWarning) as t:
chained[2] = rhs
assert t[0].filename == __file__

# TODO(ArrayManager) fast_xs with array-like scalars is not yet working
@td.skip_array_manager_not_yet_implemented
def test_chained_getitem_with_lists(self):
Expand Down