Why are attributes lost after copying a Pandas DataFrame

前端 未结 4 1519
野的像风
野的像风 2021-02-19 02:07

Why is it not possible to pass attributes of an instance through a copy? I want to pass the name attribute to another dataframe.

import copy
df = pd         


        
4条回答
  •  长情又很酷
    2021-02-19 02:12

    This code is worked:

    >>> class test():
    ...     @property
    ...     def name(self):
    ...         return self._name
    ...     @name.setter
    ...     def name(self, value):
    ...         self._name = value
    ...
    >>>
    >>> a = test()
    >>> a.name = 'Test123'
    >>> import copy
    >>> a2 = copy.deepcopy(a)
    >>> print(a2.name)
    Test123
    

    so I think that behavior is defined by pd.DataFrame

    I found that pandas define the function __deepcopy__, but I cannot totally understand the reason.

    pandas/core/indexes/base.py#L960

提交回复
热议问题