import pandas as pd
import datetime
a = '2011-09-06'
b = pd.to_datetime(a)
c = b.date()
d = b.strftime('%Y-%m-%d')
e = datetime.date(*map(int,'2011-09-06'.split('-')))
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(a == b) # FALSE
print(b == e) # True 会有警告,建议使用 b.date() 即 c
结果:
<class 'str'>
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
<class 'datetime.date'>
<class 'str'>
<class 'datetime.date'>
False
True
C:\Users\turui\AppData\Local\Temp\ipykernel_17208\4147900523.py:16: FutureWarning: Comparison of Timestamp with datetime.date is deprecated in order to match the standard library behavior. In a future version these will be considered non-comparable. Use 'ts == pd.Timestamp(date)' or 'ts.date() == date' instead.