我正试图找到一种方法来检查item_date是否包含今天的日期。但即使我硬编码,打印真的永远不会发生。有谁知道如何解决这个问题?
for item_date in buy_crossing_dates:
print item_date
print type(item_date)
if item_date == '2015-03-25 00:00:00':
print 'True'
结果:
2015-03-25 00:00:00
<class 'pandas.tslib.Timestamp'>
答案 0 :(得分:4)
在熊猫系列时间戳中检查今天日期的两个选项...
import pandas as pd
# option 1 - compare using python datetime.date objects
dates = pd.Series(pd.date_range('2015-01-01', '2016-12-31')) # Timestamps
python_dates = pd.Series([x.date() for x in dates]) # datetime.date
today = pd.Timestamp('now').date() # datetime.date
print(python_dates[python_dates == today])
# option 2 - compare pandas.Timestamp objects using Series.dt accessor
dates = pd.Series(pd.date_range('2015-01-01', '2016-12-31')) # Timestamps
today = pd.Timestamp('now') # Timestamp
print(dates[(dates.dt.year == today.year) &
(dates.dt.month == today.month) &
(dates.dt.day == today.day)])
注意:选项一使用列表推导将pandas Series of Timestamps转换为一系列datetime.date对象(使用pandas.Timestamp.date()方法)。