1

How can I make a shallow or deep copy of StyleFrame object? When I use copy.copy(sf) or , copy.deepcopy(sf) I get error: "RecursionError: maximum recursion depth exceeded while calling a Python object"

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
Jorge
  • 83
  • 10

1 Answers1

2

Just pass the original StyleFrame object to StyleFrame. Internally it will deep copy the underlying dataframe and will also copy some internal attributes.

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

Outputs

1971232017152
1971267198144
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • I was bit worried about internal attributes while directly deepcopying the data_df. But Instantiating StyleFrame with existing styleframe object removes that concern. – Jorge Jul 08 '19 at 13:33
  • The StyleFrame(sf) seems to works generally but it for throws an error in below case: `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'` ` – Jorge Jul 13 '19 at 23:27