Skip to content

Commit b77f396

Browse files
normalize ref_count usage
1 parent a469656 commit b77f396

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

pandas/compat/_constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
PYPY = platform.python_implementation() == "PyPy"
1919
WASM = (sys.platform == "emscripten") or (platform.machine() in ["wasm32", "wasm64"])
2020
ISMUSL = "musl" in (sysconfig.get_config_var("HOST_GNU_TYPE") or "")
21-
REF_COUNT = 3 if PY314 else 2
21+
# the refcount for self in a chained __setitem__/.(i)loc indexing/method call
22+
REF_COUNT = 2 if PY314 else 3
23+
REF_COUNT_IDX = 2
24+
REF_COUNT_METHOD = 1 if PY314 else 2
2225
CHAINED_WARNING_DISABLED = PYPY
2326

2427

pandas/core/frame.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from pandas.compat import CHAINED_WARNING_DISABLED
5353
from pandas.compat._constants import (
5454
REF_COUNT,
55+
REF_COUNT_METHOD,
5556
)
5657
from pandas.compat._optional import import_optional_dependency
5758
from pandas.compat.numpy import function as nv
@@ -9365,9 +9366,9 @@ def update(
93659366
2 3 6.0
93669367
"""
93679368
if not CHAINED_WARNING_DISABLED:
9368-
if sys.getrefcount(self) < REF_COUNT and not lib.is_local_in_caller_frame(
9369+
if sys.getrefcount(
93699370
self
9370-
):
9371+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
93719372
warnings.warn(
93729373
_chained_assignment_method_msg,
93739374
ChainedAssignmentError,

pandas/core/generic.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
)
8585
from pandas.compat import CHAINED_WARNING_DISABLED
8686
from pandas.compat._constants import (
87-
REF_COUNT,
87+
REF_COUNT_METHOD,
8888
)
8989
from pandas.compat._optional import import_optional_dependency
9090
from pandas.compat.numpy import function as nv
@@ -7084,7 +7084,7 @@ def fillna(
70847084
if not CHAINED_WARNING_DISABLED:
70857085
if sys.getrefcount(
70867086
self
7087-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
7087+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
70887088
warnings.warn(
70897089
_chained_assignment_method_msg,
70907090
ChainedAssignmentError,
@@ -7333,7 +7333,7 @@ def ffill(
73337333
if not CHAINED_WARNING_DISABLED:
73347334
if sys.getrefcount(
73357335
self
7336-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
7336+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
73377337
warnings.warn(
73387338
_chained_assignment_method_msg,
73397339
ChainedAssignmentError,
@@ -7475,7 +7475,7 @@ def bfill(
74757475
if not CHAINED_WARNING_DISABLED:
74767476
if sys.getrefcount(
74777477
self
7478-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
7478+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
74797479
warnings.warn(
74807480
_chained_assignment_method_msg,
74817481
ChainedAssignmentError,
@@ -7562,7 +7562,7 @@ def replace(
75627562
if not CHAINED_WARNING_DISABLED:
75637563
if sys.getrefcount(
75647564
self
7565-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
7565+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
75667566
warnings.warn(
75677567
_chained_assignment_method_msg,
75687568
ChainedAssignmentError,
@@ -7927,7 +7927,7 @@ def interpolate(
79277927
if not CHAINED_WARNING_DISABLED:
79287928
if sys.getrefcount(
79297929
self
7930-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
7930+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
79317931
warnings.warn(
79327932
_chained_assignment_method_msg,
79337933
ChainedAssignmentError,
@@ -8584,7 +8584,7 @@ def clip(
85848584
if not CHAINED_WARNING_DISABLED:
85858585
if sys.getrefcount(
85868586
self
8587-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
8587+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
85888588
warnings.warn(
85898589
_chained_assignment_method_msg,
85908590
ChainedAssignmentError,
@@ -10221,7 +10221,7 @@ def where(
1022110221
if not CHAINED_WARNING_DISABLED:
1022210222
if sys.getrefcount(
1022310223
self
10224-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
10224+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
1022510225
warnings.warn(
1022610226
_chained_assignment_method_msg,
1022710227
ChainedAssignmentError,
@@ -10287,7 +10287,7 @@ def mask(
1028710287
if not CHAINED_WARNING_DISABLED:
1028810288
if sys.getrefcount(
1028910289
self
10290-
) < REF_COUNT and not lib.is_local_in_caller_frame(self):
10290+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
1029110291
warnings.warn(
1029210292
_chained_assignment_method_msg,
1029310293
ChainedAssignmentError,

pandas/core/indexing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pandas._libs.indexing import NDFrameIndexerBase
1717
from pandas._libs.lib import item_from_zerodim
1818
from pandas.compat import CHAINED_WARNING_DISABLED
19-
from pandas.compat._constants import REF_COUNT
19+
from pandas.compat._constants import REF_COUNT_IDX
2020
from pandas.errors import (
2121
AbstractMethodError,
2222
ChainedAssignmentError,
@@ -918,7 +918,7 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:
918918
@final
919919
def __setitem__(self, key, value) -> None:
920920
if not CHAINED_WARNING_DISABLED:
921-
if sys.getrefcount(self.obj) <= REF_COUNT:
921+
if sys.getrefcount(self.obj) <= REF_COUNT_IDX:
922922
warnings.warn(
923923
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
924924
)
@@ -2586,7 +2586,7 @@ def __getitem__(self, key):
25862586

25872587
def __setitem__(self, key, value) -> None:
25882588
if not CHAINED_WARNING_DISABLED:
2589-
if sys.getrefcount(self.obj) <= REF_COUNT:
2589+
if sys.getrefcount(self.obj) <= REF_COUNT_IDX:
25902590
warnings.warn(
25912591
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
25922592
)
@@ -2617,7 +2617,7 @@ def _convert_key(self, key):
26172617

26182618
def __setitem__(self, key, value) -> None:
26192619
if not CHAINED_WARNING_DISABLED:
2620-
if sys.getrefcount(self.obj) <= REF_COUNT:
2620+
if sys.getrefcount(self.obj) <= REF_COUNT_IDX:
26212621
warnings.warn(
26222622
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
26232623
)

pandas/core/series.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from pandas.compat import CHAINED_WARNING_DISABLED
3838
from pandas.compat._constants import (
3939
REF_COUNT,
40+
REF_COUNT_METHOD,
4041
)
4142
from pandas.compat._optional import import_optional_dependency
4243
from pandas.compat.numpy import function as nv
@@ -3354,9 +3355,9 @@ def update(self, other: Series | Sequence | Mapping) -> None:
33543355
dtype: int64
33553356
"""
33563357
if not CHAINED_WARNING_DISABLED:
3357-
if sys.getrefcount(self) < REF_COUNT and not lib.is_local_in_caller_frame(
3358+
if sys.getrefcount(
33583359
self
3359-
):
3360+
) <= REF_COUNT_METHOD and not lib.is_local_in_caller_frame(self):
33603361
warnings.warn(
33613362
_chained_assignment_method_msg,
33623363
ChainedAssignmentError,

0 commit comments

Comments
 (0)