CustomClass复制错误:AttributeError:'CustomClass'对象没有属性'copy'

时间:2018-08-13 00:26:31

标签: python python-2.7

我在自己的.py文件中具有以下类:

import pandas as pd

class CashFlowSchedule(object):
    flows = {}
    annual_growth_rate = None
    df = None

    def __init__(self, daterange, months=range(1, 13), amount=0, growth=0, growth_month=1):

        self.annual_growth_rate = growth

        for dt in daterange:

            if dt.month == growth_month:
                amount *= (1. + self.annual_growth_rate)


            if dt.month in months:
                self.flows[dt] = amount
            else:
                self.flows[dt] = 0.

        self.df = pd.DataFrame([self.flows]).T

当我打电话时:

import cf_schedule as cfs
x=cfs.CashFlowSchedule(pd.date_range('20180101','20190101'))
x.copy()

我得到:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-c96b6d8d0ab0> in <module>()
----> 1 x.copy()

AttributeError: 'CashFlowSchedule' object has no attribute 'copy'

出了什么问题,我在这里想念什么?

此类非常原始,我认为__copy__方法中应存在object

谢谢

1 个答案:

答案 0 :(得分:1)

问题是类CashFlowSchedule没有copy()方法。

假设您要创建CashFlowSchedule的副本,则应使用Python的copy library

要创建浅表副本,请执行以下操作:

import copy
import cf_schedule as cfs
x=cfs.CashFlowSchedule(pd.date_range('20180101','20190101'))
x_copy = copy.copy(x)

要创建深层副本,只需用以下内容替换最后一行:

x_copy = copy.deepcopy(x)