Skip to content

Commit 58e2446

Browse files
mpagejorisvandenbossche
authored andcommitted
Update chained assignment checks
1 parent 32b14c7 commit 58e2446

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

pandas/core/frame.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4405,8 +4405,8 @@ def __setitem__(self, key, value) -> None:
44054405
z 3 50
44064406
# Values for 'a' and 'b' are completely ignored!
44074407
"""
4408-
if not PYPY and not WARNING_CHECK_DISABLED:
4409-
if sys.getrefcount(self) <= REF_COUNT + 1:
4408+
if not PYPY:
4409+
if sys.getrefcount(self) <= REF_COUNT and not sys._is_local_in_caller_frame(self):
44104410
warnings.warn(
44114411
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
44124412
)
@@ -9363,8 +9363,8 @@ def update(
93639363
1 2 500.0
93649364
2 3 6.0
93659365
"""
9366-
if not PYPY and not WARNING_CHECK_DISABLED:
9367-
if sys.getrefcount(self) <= REF_COUNT:
9366+
if not PYPY:
9367+
if sys.getrefcount(self) < REF_COUNT and not sys._is_local_in_caller_frame(self):
93689368
warnings.warn(
93699369
_chained_assignment_method_msg,
93709370
ChainedAssignmentError,

pandas/core/generic.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
from pandas.compat import PYPY
8686
from pandas.compat._constants import (
8787
REF_COUNT,
88-
WARNING_CHECK_DISABLED,
8988
)
9089
from pandas.compat._optional import import_optional_dependency
9190
from pandas.compat.numpy import function as nv
@@ -7082,8 +7081,10 @@ def fillna(
70827081
"""
70837082
inplace = validate_bool_kwarg(inplace, "inplace")
70847083
if inplace:
7085-
if not PYPY and not WARNING_CHECK_DISABLED:
7086-
if sys.getrefcount(self) <= REF_COUNT:
7084+
if not PYPY:
7085+
if sys.getrefcount(
7086+
self
7087+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
70877088
warnings.warn(
70887089
_chained_assignment_method_msg,
70897090
ChainedAssignmentError,
@@ -7329,8 +7330,10 @@ def ffill(
73297330
"""
73307331
inplace = validate_bool_kwarg(inplace, "inplace")
73317332
if inplace:
7332-
if not PYPY and not WARNING_CHECK_DISABLED:
7333-
if sys.getrefcount(self) <= REF_COUNT:
7333+
if not PYPY:
7334+
if sys.getrefcount(
7335+
self
7336+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
73347337
warnings.warn(
73357338
_chained_assignment_method_msg,
73367339
ChainedAssignmentError,
@@ -7469,8 +7472,10 @@ def bfill(
74697472
"""
74707473
inplace = validate_bool_kwarg(inplace, "inplace")
74717474
if inplace:
7472-
if not PYPY and not WARNING_CHECK_DISABLED:
7473-
if sys.getrefcount(self) <= REF_COUNT:
7475+
if not PYPY:
7476+
if sys.getrefcount(
7477+
self
7478+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
74747479
warnings.warn(
74757480
_chained_assignment_method_msg,
74767481
ChainedAssignmentError,
@@ -7554,8 +7559,10 @@ def replace(
75547559

75557560
inplace = validate_bool_kwarg(inplace, "inplace")
75567561
if inplace:
7557-
if not PYPY and not WARNING_CHECK_DISABLED:
7558-
if sys.getrefcount(self) <= REF_COUNT:
7562+
if not PYPY:
7563+
if sys.getrefcount(
7564+
self
7565+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
75597566
warnings.warn(
75607567
_chained_assignment_method_msg,
75617568
ChainedAssignmentError,
@@ -7917,8 +7924,10 @@ def interpolate(
79177924
inplace = validate_bool_kwarg(inplace, "inplace")
79187925

79197926
if inplace:
7920-
if not PYPY and not WARNING_CHECK_DISABLED:
7921-
if sys.getrefcount(self) <= REF_COUNT:
7927+
if not PYPY:
7928+
if sys.getrefcount(
7929+
self
7930+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
79227931
warnings.warn(
79237932
_chained_assignment_method_msg,
79247933
ChainedAssignmentError,
@@ -8572,8 +8581,10 @@ def clip(
85728581
inplace = validate_bool_kwarg(inplace, "inplace")
85738582

85748583
if inplace:
8575-
if not PYPY and not WARNING_CHECK_DISABLED:
8576-
if sys.getrefcount(self) <= REF_COUNT:
8584+
if not PYPY:
8585+
if sys.getrefcount(
8586+
self
8587+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
85778588
warnings.warn(
85788589
_chained_assignment_method_msg,
85798590
ChainedAssignmentError,
@@ -10207,8 +10218,10 @@ def where(
1020710218
"""
1020810219
inplace = validate_bool_kwarg(inplace, "inplace")
1020910220
if inplace:
10210-
if not PYPY and not WARNING_CHECK_DISABLED:
10211-
if sys.getrefcount(self) <= REF_COUNT:
10221+
if not PYPY:
10222+
if sys.getrefcount(
10223+
self
10224+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
1021210225
warnings.warn(
1021310226
_chained_assignment_method_msg,
1021410227
ChainedAssignmentError,
@@ -10271,8 +10284,10 @@ def mask(
1027110284
) -> Self | None:
1027210285
inplace = validate_bool_kwarg(inplace, "inplace")
1027310286
if inplace:
10274-
if not PYPY and not WARNING_CHECK_DISABLED:
10275-
if sys.getrefcount(self) <= REF_COUNT:
10287+
if not PYPY:
10288+
if sys.getrefcount(
10289+
self
10290+
) < REF_COUNT and not sys._is_local_in_caller_frame(self):
1027610291
warnings.warn(
1027710292
_chained_assignment_method_msg,
1027810293
ChainedAssignmentError,

pandas/core/series.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from pandas.compat import PYPY
3838
from pandas.compat._constants import (
3939
REF_COUNT,
40-
WARNING_CHECK_DISABLED,
4140
)
4241
from pandas.compat._optional import import_optional_dependency
4342
from pandas.compat.numpy import function as nv
@@ -1058,8 +1057,10 @@ def _get_value(self, label, takeable: bool = False):
10581057
return self.iloc[loc]
10591058

10601059
def __setitem__(self, key, value) -> None:
1061-
if not PYPY and not WARNING_CHECK_DISABLED:
1062-
if sys.getrefcount(self) <= REF_COUNT + 1:
1060+
if not PYPY:
1061+
if sys.getrefcount(self) <= REF_COUNT and not sys._is_local_in_caller_frame(
1062+
self
1063+
):
10631064
warnings.warn(
10641065
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
10651066
)
@@ -3352,8 +3353,10 @@ def update(self, other: Series | Sequence | Mapping) -> None:
33523353
2 3
33533354
dtype: int64
33543355
"""
3355-
if not PYPY and not WARNING_CHECK_DISABLED:
3356-
if sys.getrefcount(self) <= REF_COUNT:
3356+
if not PYPY:
3357+
if sys.getrefcount(self) < REF_COUNT and not sys._is_local_in_caller_frame(
3358+
self
3359+
):
33573360
warnings.warn(
33583361
_chained_assignment_method_msg,
33593362
ChainedAssignmentError,

0 commit comments

Comments
 (0)