简体   繁体   English

两种不同类型的日期比较,Python 3.6

[英]Two different type date compare, Python 3.6

I want to compare below two dates, 我想比较以下两个日期,

publicationDate contains Timestamp('2018-05-25 00:00:00')

Type: pandas._libs.tslibs.timestamps.Timestamp

publicationDate getting from API Ressult: 从API Ressult获取的publicationDate:

publicationDate = pd.to_datetime(Json_Data_1['publicationDate'])

datetime.date.today() returns datetime.date(2019, 3, 4)

Type: datetime.date


if 'W' in Frequency:
    while(publicationDate < datetime.date.today()):
        publicationDate = publicationDate + relativedelta(weeks=+1)

Error: 错误:

TypeError: Cannot compare type 'Timestamp' with type 'date'

使用to_pydatetime()可以将时间戳转换为python datetime

Thats an honest mistake. 那是一个诚实的错误。

It looks like you are trying to compare similar things, but you are not. 看来您正在尝试比较类似的事物,但事实并非如此。 One is a TS which includes time information, the other one is a data object, which contains only date information. 一个是包含时间信息的TS,另一个是仅包含日期信息的数据对象。

datetime.date.today() returns datetime.date(2019, 3, 4)

if you change your code to: 如果将代码更改为:

if 'W' in Frequency:
    while(publicationDate < pd.to_datetime(datetime.date.today())):
        publicationDate = publicationDate + relativedelta(weeks=+1)

or to: 或者:

if 'W' in Frequency:
    while(publicationDate.date() < datetime.date.today()):
        publicationDate = publicationDate + relativedelta(weeks=+1)

it should work! 它应该工作!

hope it helps! 希望能帮助到你!

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

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