1
$\begingroup$

The C++ version of QuantLib has a function called years:

Real years(const &Period p);

I don't see this in QuantLib-Python version 1.18. Is it supposed to be there or was it not SWIGed?

$\endgroup$
1
$\begingroup$

It is not swigged AFAIK. As a workaround, you can retrieve this information by using p.units() and p.length() that are available (if. p is your period):

For example:

p = ql.Period("1Y3M")
p.units()
# returns 2 (i.e. ql.Months)
p.length()
# returns 15

Your function will look something like:

def ql_period_in_years(period):
    units = period.units()
    if units == ql.Days:
        denominator = 365
    elif units == ql.Weeks:
        denominator = 52
    elif units == ql.Months:
        denominator = 12
    ...
    return period.length() / denominator

For the example above:

   ql_period_in_years(p)
   # returns 1.25
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.