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
版权声明:本文标题:python - Copy data to a reference - copy or deepcopy not working - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745515475a2154024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论