@@ -24,6 +24,8 @@ cnp.import_array()
2424
2525from pandas._libs.algos import ensure_int64
2626from pandas.errors import ChainedAssignmentError
27+ from pandas.errors.cow import _chained_assignment_msg
28+
2729
2830from pandas._libs.util cimport (
2931 is_array,
@@ -1005,10 +1007,7 @@ cdef class BlockValuesRefs:
10051007cdef extern from " Python.h" :
10061008 """
10071009 #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);
10121011 #else
10131012 #define __Pyx_PyUnstable_Object_IsUniqueReferencedTemporary \
10141013 PyUnstable_Object_IsUniqueReferencedTemporary
@@ -1018,40 +1017,25 @@ cdef extern from "Python.h":
10181017 " __Pyx_PyUnstable_Object_IsUniqueReferencedTemporary" (object o) except - 1
10191018
10201019
1020+ # Python version compatibility for PyUnstable_Object_IsUniqueReferencedTemporary
10211021cdef inline bint _is_unique_referenced_temporary(object obj) except - 1 :
10221022 if PY_VERSION_HEX >= 0x030E0000 :
1023+ # Python 3.14+ has PyUnstable_Object_IsUniqueReferencedTemporary
10231024 return PyUnstable_Object_IsUniqueReferencedTemporary(obj)
10241025 else :
1026+ # Fallback for older Python versions using sys.getrefcount
10251027 return sys.getrefcount(obj) <= 1
10261028
10271029
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)
10421030cdef class SetitemMixin:
1031+ # class used in DataFrame and Series for checking for chained assignment
10431032
1044- def __setitem__ (self , key , value ):
1033+ def __setitem__ (self , key , value ) -> None :
10451034 cdef bint is_unique = _is_unique_referenced_temporary(self )
1046- # print("Refcount self: ", sys.getrefcount(self))
1047- # print("Is unique referenced temporary: ", is_unique)
10481035 if is_unique:
10491036 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+ )
10551039 self._setitem(key , value )
10561040
10571041 def __delitem__(self , key ) -> None:
0 commit comments