【问题标题】:deep copy of StyleFrame objectStyleFrame 对象的深拷贝
【发布时间】:2019-11-17 09:13:30
【问题描述】:

如何制作 StyleFrame 对象的浅拷贝或深层拷贝? 当我使用 copy.copy(sf) 或时, copy.deepcopy(sf) 出现错误: "RecursionError: 调用 Python 对象时超出最大递归深度"

import copy
from StyleFrame import StyleFrame
import pandas as pd
df=pd.DataFrame([list('abc')])
sf=StyleFrame(df)
copy.copy(sf)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python\python373\lib\copy.py", line 106, in copy
    return _reconstruct(x, None, *rv)
  File "C:\python\python373\lib\copy.py", line 281, in _reconstruct
    if hasattr(y, '__setstate__'):
  File "C:\python\python373_vm1\lib\site-packages\StyleFrame\style_frame.py", line 121, in __getattr__
    if attr in self.data_df.columns:
  File "C:\python\python373_vm1\lib\site-packages\StyleFrame\style_frame.py", line 121, in __getattr__
    if attr in self.data_df.columns:
  File "C:\python\python373_vm1\lib\site-packages\StyleFrame\style_frame.py", line 121, in __getattr__
    if attr in self.data_df.columns:
  [Previous line repeated 495 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

【问题讨论】:

    标签: python copy styleframe


    【解决方案1】:

    只需将原始 StyleFrame 对象传递给StyleFrame。在内部,它会深度复制底层数据帧,也会复制一些内部属性。

    sf = StyleFrame({'a': [1, 2]})
    print(id(sf))
    new_sf = StyleFrame(sf)
    print(id(new_sf))
    

    输出

    1971232017152
    1971267198144
    

    【讨论】:

    • 在直接对data_df进行深度复制时,我有点担心内部属性。但是用现有的 styleframe 对象实例化 StyleFrame 消除了这种担忧。
    • StyleFrame(sf) 似乎可以正常工作,但在以下情况下会引发错误:sf=StyleFrame.read_excel(r'C:\temp\test.xlsx', sheet_name='Sheet1', read_style=True, use_openpyxl_styles=True)sf2=StyleFrame(sf)sf3=StyleFrame(sf2)TypeError: __init__() missing 1 required positional argument: 'worksheet'`
    猜你喜欢
    • 2015-09-14
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多