# QuantLib-Python: where is the function to take Period to years?

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?

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