简体   繁体   English

为什么我会收到此 Python 日期时间错误?

[英]Why am I getting this Python datetime error?

In the following code, I'm trying to download earnings data using Python and the Yahoo Earnings Calendar library.在以下代码中,我尝试使用 Python 和 Yahoo Earnings Calendar 库下载收益数据。 However, I'm having problems with dates.但是,我在日期方面遇到了问题。 I'm trying to create a date range with the start date being the date the code is executed and the end date the following day.我正在尝试创建一个日期范围,开始日期是代码执行的日期,结束日期是第二天。 Here is my code:这是我的代码:

# Set date range
DAYS_AHEAD = 1

# This works
# start_date = dt.date(2021, 4, 14)

# This doesn't work
start_date = dt.datetime.today()
# This doesn't work either
start_date = dt.datetime.now()

# This works for the end date when I use the first start_date declared above
end_date = dt.date(2021, 4, 14) + timedelta(days=DAYS_AHEAD)

# Download earnings calendar
yec = YahooEarningsCalendar()
earnings_list = yec.earnings_between(start_date, end_date)

The last statement which fetches the list of earnings is raising the following error:获取收入列表的最后一条语句引发了以下错误:

    TypeError: When either from_date or to_date is not a datetime.date object.
    127         """
--> 128         if from_date > to_date:
    129             raise ValueError(
    130                 'From-date should not be after to-date')

TypeError: can't compare datetime.datetime to datetime.date

If I type the code above that sets the start and end dates in the Python REPL I see the following:如果我在 Python REPL 中键入上面设置开始和结束日期的代码,我会看到以下内容:

>>> type(start_date)
<class 'datetime.datetime'>
>>>type(end_date)
<class 'datetime.datetime'>

Also if I run commands below in order to tell which variable has which type so I can figure out which one I need to convert to the type of the other variable, they all return "True".此外,如果我运行下面的命令以判断哪个变量具有哪种类型,以便我可以找出我需要将哪个变量转换为另一个变量的类型,它们都会返回“True”。

>>> isinstance(start_date, datetime.datetime)
>>> isinstance(start_date, datetime.date)
>>> isinstance(end_date, datetime.datetime)
>>> isinstance(end_date, datetime.date)

They both seem to be of the same class and type, so why can't I compare them to each other?它们似乎都是相同的 class 和类型,那么为什么我不能将它们相互比较呢? How do I convert one of the dates to the same type as the other so that the comparison will work?如何将其中一个日期转换为与另一个日期相同的类型,以便进行比较?

datetime s inherit from both date and time (makes sense, yeah?), so that's why the isinstance checks return true. datetime继承自datetime (有道理,是吗?),所以这就是isinstance检查返回 true 的原因。

If you don't need a time component, only play with date s:如果你不需要时间组件,只玩date s:

import datetime

DAYS_AHEAD = 1
start_date = datetime.date.today()
end_date = start_date + datetime.timedelta(days=DAYS_AHEAD)

# Download earnings calendar
yec = YahooEarningsCalendar()
earnings_list = yec.earnings_between(start_date, end_date)

If you already have a datetime.datetime object, you can extract the date with .date() :如果您已经有一个datetime.datetime object,您可以使用.date()提取日期:

>>> import datetime
>>> right_now = datetime.datetime.now()
datetime.datetime(2021, 4, 13, 22, 20, 38, 257608)
>>> today = right_now.date()
datetime.date(2021, 4, 13)

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

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