Skip to content

Commit 044ee06

Browse files
committed
Merge pull request #5581 from jtratner/fix-stacklevel-for-basic-setting
CLN/BUG: Fix stacklevel on setting with copy warning.
2 parents 476b6e3 + a9627c4 commit 044ee06

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pandas/core/generic.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,11 @@ def _setitem_copy(self, copy):
10151015
self._is_copy = copy
10161016
return self
10171017

1018-
def _check_setitem_copy(self):
1019-
""" validate if we are doing a settitem on a chained copy """
1018+
def _check_setitem_copy(self, stacklevel=4):
1019+
""" validate if we are doing a settitem on a chained copy.
1020+
1021+
If you call this function, be sure to set the stacklevel such that the
1022+
user will see the error *at the level of setting*"""
10201023
if self._is_copy:
10211024
value = config.get_option('mode.chained_assignment')
10221025

@@ -1026,7 +1029,7 @@ def _check_setitem_copy(self):
10261029
if value == 'raise':
10271030
raise SettingWithCopyError(t)
10281031
elif value == 'warn':
1029-
warnings.warn(t, SettingWithCopyWarning)
1032+
warnings.warn(t, SettingWithCopyWarning, stacklevel=stacklevel)
10301033

10311034
def __delitem__(self, key):
10321035
"""

0 commit comments

Comments
 (0)