admin管理员组

文章数量:1023764

A function gets three dataframes as an argument. Depending on it being empty or not it should copy or merge the data with an internal variable/dataframe.

    def updateMaster(self, NewLines:pd.DataFrame, Updated:pd.DataFrame, Removed:pd.DataFrame) -> bool:
                
        self.__New = self._update_dataframe(self.__New, NewLines)
        self.__Updated = self._update_dataframe(self.__Updated, Updated)
        self.__Removed = self._update_dataframe(self.__Removed, Removed)

        return True
    
    def _update_dataframe(self, dest:pd.DataFrame, source:pd.DataFrame):
        try:
            if "somecolumn" not in source.columns:
                raise ValueError("'somecolumn' as primary key is not present in data")
            #add other checks
        
        except ValueError as e:
            print(f"{e}")
            return False
        
        else:
            if dest.empty:
                dest = source.copy()
            else:
                dest = dest.merge(source, how="inner", on="somecolumn")

in no case self.__New will contain the data.

dest is self._New  # -> True

Ok so dest has the correct reference.

id(dest)
129630572847344
id(self._New)
129630572847344

Yes

dest = source.copy()
dest is self._New  # -> False

And obviously not containing the data. dest now points to another address.

Gemini suggested using copy.deepcopy() but that doesn't solve my issue.

What are my options for this problem?

A function gets three dataframes as an argument. Depending on it being empty or not it should copy or merge the data with an internal variable/dataframe.

    def updateMaster(self, NewLines:pd.DataFrame, Updated:pd.DataFrame, Removed:pd.DataFrame) -> bool:
                
        self.__New = self._update_dataframe(self.__New, NewLines)
        self.__Updated = self._update_dataframe(self.__Updated, Updated)
        self.__Removed = self._update_dataframe(self.__Removed, Removed)

        return True
    
    def _update_dataframe(self, dest:pd.DataFrame, source:pd.DataFrame):
        try:
            if "somecolumn" not in source.columns:
                raise ValueError("'somecolumn' as primary key is not present in data")
            #add other checks
        
        except ValueError as e:
            print(f"{e}")
            return False
        
        else:
            if dest.empty:
                dest = source.copy()
            else:
                dest = dest.merge(source, how="inner", on="somecolumn")

in no case self.__New will contain the data.

dest is self._New  # -> True

Ok so dest has the correct reference.

id(dest)
129630572847344
id(self._New)
129630572847344

Yes

dest = source.copy()
dest is self._New  # -> False

And obviously not containing the data. dest now points to another address.

Gemini suggested using copy.deepcopy() but that doesn't solve my issue.

What are my options for this problem?

本文标签: pythonCopy data to a referencecopy or deepcopy not workingStack Overflow