最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论