简体   繁体   中英

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:

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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