在下面我得到了 datetime.date 和 datetime.datetime 類。
start=df.index[0]
start_input_1=datetime.strptime(start_input[:10],'%Y-%m-%d')
print(start_input[:10])
print(type(start))
print(type(start_input_1))
2021-09-01
<class 'datetime.date'>
<class 'datetime.datetime'>
現在如果我低于比較,那么我得到一個錯誤
無法將 datetime.datetime 與 datetime.date 進行比較
if start_input_1>=start:
start_plot=start_date_input_1
那么如何將 datetime.date 轉換為 datetime.datetime 以便比較兩個日期?
如果我使用 pd.to_datetime(),它將轉換為時間戳而不是日期時間。謝謝你的幫助。
start=pd.to_datetime(start)
print(start)
class 'pandas._libs.tslibs.timestamps.Timestamp'
uj5u.com熱心網友回復:
您可以通過從物件中獲取日期來檢查日期是否相同datetime,或者將默認時間 (00:00:00) 添加到date物件以使其datetime:
from datetime import datetime, date
dt = datetime.fromisoformat("2020-01-01T10:00:00")
d = date.fromisoformat("2020-01-01")
print(dt, d) # 2020-01-01 00:00:00 2020-01-01
print(type(dt), type(d)) # <class 'datetime.datetime'> <class 'datetime.date'>
# Check only by date, do not check time
if d == dt.date():
print("Same date")
# Check by date, but both have to be 00:00:00
d = datetime.fromisoformat(d.isoformat())
if d == dt:
print("Same date and 00:00:00")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490922.html
