我想在两个日期以下进行比较,
publicationDate contains Timestamp('2018-05-25 00:00:00')
Type: pandas._libs.tslibs.timestamps.Timestamp
从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)
错误:
TypeError: Cannot compare type 'Timestamp' with type 'date'
答案 0 :(得分:2)
使用to_pydatetime()来将时间戳转换为python日期时间
答案 1 :(得分:2)
那是一个诚实的错误。
您似乎正在尝试比较类似的事物,但事实并非如此。 一个是包含时间信息的TS,另一个是仅包含日期信息的数据对象。
datetime.date.today() returns datetime.date(2019, 3, 4)
如果您将代码更改为:
if 'W' in Frequency:
while(publicationDate < pd.to_datetime(datetime.date.today())):
publicationDate = publicationDate + relativedelta(weeks=+1)
或:
if 'W' in Frequency:
while(publicationDate.date() < datetime.date.today()):
publicationDate = publicationDate + relativedelta(weeks=+1)
应该可以!
希望有帮助!