@@ -24,6 +24,8 @@ cnp.import_array()
24
24
25
25
from pandas._libs.algos import ensure_int64
26
26
from pandas.errors import ChainedAssignmentError
27
+ from pandas.errors.cow import _chained_assignment_msg
28
+
27
29
28
30
from pandas._libs.util cimport (
29
31
is_array,
@@ -1005,10 +1007,7 @@ cdef class BlockValuesRefs:
1005
1007
cdef extern from " Python.h" :
1006
1008
"""
1007
1009
#if PY_VERSION_HEX < 0x030E0000
1008
- int __Pyx_PyUnstable_Object_IsUniqueReferencedTemporary(PyObject *ref)
1009
- {
1010
- return 0;
1011
- }
1010
+ int __Pyx_PyUnstable_Object_IsUniqueReferencedTemporary(PyObject *ref);
1012
1011
#else
1013
1012
#define __Pyx_PyUnstable_Object_IsUniqueReferencedTemporary \
1014
1013
PyUnstable_Object_IsUniqueReferencedTemporary
@@ -1018,40 +1017,25 @@ cdef extern from "Python.h":
1018
1017
" __Pyx_PyUnstable_Object_IsUniqueReferencedTemporary" (object o) except - 1
1019
1018
1020
1019
1020
+ # Python version compatibility for PyUnstable_Object_IsUniqueReferencedTemporary
1021
1021
cdef inline bint _is_unique_referenced_temporary(object obj) except - 1 :
1022
1022
if PY_VERSION_HEX >= 0x030E0000 :
1023
+ # Python 3.14+ has PyUnstable_Object_IsUniqueReferencedTemporary
1023
1024
return PyUnstable_Object_IsUniqueReferencedTemporary(obj)
1024
1025
else :
1026
+ # Fallback for older Python versions using sys.getrefcount
1025
1027
return sys.getrefcount(obj) <= 1
1026
1028
1027
1029
1028
- # # Python version compatibility for PyUnstable_Object_IsUniqueReferencedTemporary
1029
- # IF PY_VERSION_HEX >= 0x030E0000:
1030
- # # Python 3.14+ has PyUnstable_Object_IsUniqueReferencedTemporary
1031
- # cdef inline bint _is_unique_referenced_temporary(object obj) except -1:
1032
- # return PyUnstable_Object_IsUniqueReferencedTemporary(obj)
1033
- # ELSE:
1034
- # # Fallback for older Python versions using sys.getrefcount
1035
- # cdef inline bint _is_unique_referenced_temporary(object obj) except -1:
1036
- # # sys.getrefcount includes the reference from getrefcount itself
1037
- # # So if refcount is 2, it means only one external reference exists
1038
- # return sys.getrefcount(obj) == 2
1039
-
1040
-
1041
- # @cython.auto_pickle(False)
1042
1030
cdef class SetitemMixin:
1031
+ # class used in DataFrame and Series for checking for chained assignment
1043
1032
1044
- def __setitem__ (self , key , value ):
1033
+ def __setitem__ (self , key , value ) -> None :
1045
1034
cdef bint is_unique = _is_unique_referenced_temporary(self )
1046
- # print("Refcount self: ", sys.getrefcount(self))
1047
- # print("Is unique referenced temporary: ", is_unique)
1048
1035
if is_unique:
1049
1036
warnings.warn(
1050
- " A value is trying to be set on a copy of a DataFrame or Series "
1051
- " through chained assignment." ,
1052
- ChainedAssignmentError,
1053
- stacklevel = 1 ,
1054
- )
1037
+ _chained_assignment_msg , ChainedAssignmentError , stacklevel = 1
1038
+ )
1055
1039
self._setitem(key , value )
1056
1040
1057
1041
def __delitem__(self , key ) -> None:
0 commit comments