python - Copy data to a reference - copy or deepcopy not working - Stack Overflow

A function gets three dataframes as an argument. Depending on it being empty or not it should copy or m

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745515476a4630969.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信