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?


1 Answer 1


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")
# returns 2 (i.e. ql.Months)
# 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:

   # returns 1.25

Your Answer

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

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