得到奇怪的 yfinance ValueError 并且没有收到正确的输出

Getting strange yfinance ValueError and not receiving the correct outputs

我正在尝试显示特定到期日的看跌期权列表。出于某种原因,它返回了一个奇怪的错误,如下所示。

raise ValueError(
ValueError: Expiration `2022-03-18` cannot be found. Available expiration are: [2022-02-18, 2022-02-25, 2022-03-04, 2022-03-11, 2022-03-18, 2022-03-25, 2022-04-01, 2022-04-14, 2022-05-20, 2022-06-17, 2022-07-15, 2022-08-19, 2022-09-16, 2023-01-20, 2023-03-17, 2023-06-16, 2024-01-19]

Process finished with exit code 1

如您所见,我很困惑,因为提到的未找到日期显然在提供的可用到期列表中。

这是我的代码:

import yfinance as yf
import pandas as pd
import datetime
import calendar

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

msft = yf.Ticker("MSFT")


# Get the expiry date of closes Friday 30 days from today
today = datetime.date.today()
_30_days_after_today = today + datetime.timedelta(days=30)
next_friday = _30_days_after_today + datetime.timedelta(days=(calendar.FRIDAY - _30_days_after_today.weekday()))
print(type(next_friday))

# get option chain for specific expiration
opt = msft.option_chain(next_friday)
# data available via: opt.calls, opt.puts
print(opt.puts)

有人知道是什么原因造成的吗?

当我将输入转换为字符串时,似乎错误已解决。

import pandas as pd
import datetime
import calendar

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

msft = yf.Ticker("MSFT")


# Get the expiry date of closes Friday 30 days from today
today = datetime.date.today()
_30_days_after_today = today + datetime.timedelta(days=30)
next_friday = str(_30_days_after_today + datetime.timedelta(days=(calendar.FRIDAY - _30_days_after_today.weekday())))
print(next_friday)

# get option chain for specific expiration
opt = msft.option_chain(date=next_friday)
# data available via: opt.calls, opt.puts
print(opt.puts)