1

I have the following class in its own .py file:

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

When I call:

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

I get:

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

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

What is going wrong, and what am I missing here?

This class is exceptionally primitive, and I thought __copy__ should exist in the object methods.

Thank you

1
  • To call .copy() on csf, or CashFlowSchedule, you would need to define a method called copy Commented Aug 13, 2018 at 1:08

1 Answer 1

2

The problem is that the class CashFlowSchedule does not have a copy() method.

Assuming you want to create a copy of CashFlowSchedule, you should use Python's copy library.

To create a shallow copy:

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

To create a deep copy, just replace the last line with this:

x_copy = copy.deepcopy(x)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.