pythoncopystyleframe

deep copy of StyleFrame object


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

Solution

  • 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