简体   繁体   English

Python Quantlib 将 Quantlib 日期转换为日期时间

[英]Python Quantlib convert Quantlib Date to datetime

I have a list of dates defined in the Date format of quantlib.我有一个以 quantlib 的日期格式定义的日期列表。 How can I convert these into datetime format.如何将这些转换为日期时间格式。 The reason I am asking is, that I would like to plot it and I received the follwoing error:我问的原因是,我想绘制它并且我收到了以下错误:

TypeError: float() argument must be a string or a number, not 'Date' TypeError:float() 参数必须是字符串或数字,而不是“日期”

when doing the following:执行以下操作时:

plt.plot(dates,rates, linewidth=2.0) # Without date plotting works out.

dates looks like the following:日期如下所示:

[
  Date(11,12,2012), 
  Date(12,12,2012), 
  Date(13,12,2012),
  Date(14,12,2012), 
  Date(15,12,2012), 
  Date(16,12,2012),
  Date(17,12,2012), 
  Date(18,12,2012), 
  Date(19,12,2012),
  Date(20,12,2012)
]

There's no predefined conversion, so you'll have to extract the information from the QuantLib date and use it to build a datetime instance.没有预定义的转换,因此您必须从 QuantLib 日期中提取信息并使用它来构建datetime时间实例。 Eg, define something like例如,定义类似

def ql_to_datetime(d):
    return datetime.datetime(d.year(), d.month(), d.dayOfMonth())

after which you can use ql_to_datetime(d) for a single date, or [ ql_to_datetime(d) for d in dates ] for a list.之后,您可以将ql_to_datetime(d)用于单个日期,或[ ql_to_datetime(d) for d in dates ]用于列表。 (You can also define another function taking a list, of course.) (当然,您也可以定义另一个获取列表的函数。)

Update: in recent versions of QuantLib-Python, a predefined conversion was added.更新:在最新版本的 QuantLib-Python 中,添加了预定义的转换。 You can now say d.to_date() to convert a date, or [d.to_date() for d in dates] for a list.您现在可以说d.to_date()来转换日期,或者说[d.to_date() for d in dates]来转换列表。

There is a faster way.有一种更快的方法。 Since datetime.datetime has methods fromordinal() and toordinal() to convert between datetime.datetime and an integer that represents the date.由于datetime.datetime具有fromordinal()toordinal()方法来在datetime.datetime和表示日期的整数之间进行转换。 Similarly, ql.Date also has the method serialNumber() to convert ql.Date to integer and its constructor accepts the integer as well.同样, ql.Date也有方法serialNumber()ql.Date转换为整数,并且它的构造函数也接受整数。 However, datetime.datetime uses 0001-01-01 as 1 with increments in days while ql.Date uses 1899-12-31 as 0. Hence, to convert between the two, we can use the following function:但是, datetime.datetime使用0001-01-01作为 1,增量为天,而ql.Date使用1899-12-31作为 0。因此,要在两者之间进行转换,我们可以使用以下函数:

def ql_to_datetime_new(d):
    return datetime.datetime.fromordinal(d.serialNumber() + 693594)

where 693594 is equal to datetime.datetime(1899,12,31).toordinal() - 1. To test the speed, I also use the function from Luigi's answer:其中693594等于datetime.datetime(1899,12,31).toordinal() - 1。为了测试速度,我还使用了 Luigi 回答中的函数:

def ql_to_datetime(d):
    return datetime.datetime(d.year(), d.month(), d.dayOfMonth())

Then, test the speed as follows:然后,按如下方式测试速度:

d = ql.Date(31, 12, 2000)
%timeit ql_to_datetime(d)
%timeit ql_to_datetime_new(d)

On my computer, the results are在我的电脑上,结果是

796 ns ± 18.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
436 ns ± 0.938 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Hence, the one that I proposed is faster.因此,我提出的那个更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM