0

Let's say one of the datetime column as below

PORT['ISSUE_DT']

0 2019-01-31 1 2018-10-24 2 2018-11-16 3 2018-11-16 4 2018-11-16

How can I convert it to Quanlib Date quickly? I can convert one element as below, are there any methods without using 'for'?

#DATA_DT
d = PORT['ISSUE_DT'][0]
#convert datetime to qt Date
d_qt =Date(d.day, d.month, d.year)

I know there is apply function like R, but I don't know how to deal with apply with the class attribute .

Logica
  • 977
  • 4
  • 16
Coty Huang
  • 11
  • 2

1 Answers1

0

QuantLib Python has 3 ways to construct the Date object:

  • ql.Date(serialNumber), where the serial is the same as excel
  • ql.Date(day, month, year)
  • ql.Date(dateString, formatString)

If you want to convert the whole column you can use the apply method in pandas and use the construction method you prefer.

For example:

PORT.ISSUE_DT = PORT.ISSUE_DT.astype(str).apply(lambda d: ql.Date(d, '%Y-%m-%d'))

This will convert the column from strings to ql.Date

David Duarte
  • 644
  • 4
  • 11
  • Thanks, @David Durate. It works. I was trying some stupid non-working thing like below ```python dd=lambda x:x.day mm=lambda x:x.month yy=lambda x:x.year apply(Date, PORT['ISSUE_DT'].apply(dd),PORT['ISSUE_DT'].apply(mm),yy=lambda x:x.year) ``` – Coty Huang Mar 11 '20 at 09:43