Skip to content

Commit 85b7375

Browse files
phofldebnathshoham
authored andcommitted
Use find_stack_level for SettingWithCopyWarning (#42713)
1 parent c7b1ddb commit 85b7375

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
lines changed

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(stacklevel=5)
3748+
self._check_setitem_copy()
37493749

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

pandas/core/generic.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
doc,
6868
rewrite_axis_style_signature,
6969
)
70+
from pandas.util._exceptions import find_stack_level
7071
from pandas.util._validators import (
7172
validate_ascending,
7273
validate_bool_kwarg,
@@ -3506,7 +3507,7 @@ def _maybe_update_cacher(
35063507
"""
35073508

35083509
if verify_is_copy:
3509-
self._check_setitem_copy(stacklevel=5, t="referent")
3510+
self._check_setitem_copy(t="referent")
35103511

35113512
if clear:
35123513
self._clear_item_cache()
@@ -3853,26 +3854,21 @@ def _check_is_chained_assignment_possible(self) -> bool_t:
38533854
setting.
38543855
"""
38553856
if self._is_copy:
3856-
self._check_setitem_copy(stacklevel=4, t="referent")
3857+
self._check_setitem_copy(t="referent")
38573858
return False
38583859

38593860
@final
3860-
def _check_setitem_copy(self, stacklevel=4, t="setting", force=False):
3861+
def _check_setitem_copy(self, t="setting", force=False):
38613862
"""
38623863
38633864
Parameters
38643865
----------
3865-
stacklevel : int, default 4
3866-
the level to show of the stack when the error is output
38673866
t : str, the type of setting error
38683867
force : bool, default False
38693868
If True, then force showing an error.
38703869
38713870
validate if we are doing a setitem on a chained copy.
38723871
3873-
If you call this function, be sure to set the stacklevel such that the
3874-
user will see the error *at the level of setting*
3875-
38763872
It is technically possible to figure out that we are setting on
38773873
a copy even WITH a multi-dtyped pandas object. In other words, some
38783874
blocks may be views while other are not. Currently _is_view will ALWAYS
@@ -3931,7 +3927,7 @@ def _check_setitem_copy(self, stacklevel=4, t="setting", force=False):
39313927
if value == "raise":
39323928
raise com.SettingWithCopyError(t)
39333929
elif value == "warn":
3934-
warnings.warn(t, com.SettingWithCopyWarning, stacklevel=stacklevel)
3930+
warnings.warn(t, com.SettingWithCopyWarning, stacklevel=find_stack_level())
39353931

39363932
def __delitem__(self, key) -> None:
39373933
"""

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ def _check_is_chained_assignment_possible(self) -> bool:
12141214
if self._is_view and self._is_cached:
12151215
ref = self._get_cacher()
12161216
if ref is not None and ref._is_mixed_type:
1217-
self._check_setitem_copy(stacklevel=4, t="referent", force=True)
1217+
self._check_setitem_copy(t="referent", force=True)
12181218
return True
12191219
return super()._check_is_chained_assignment_possible()
12201220

0 commit comments

Comments
 (0)