Reputation: 226
I am trying to find a way to check if item_date contain today's date. But even I hard code it, print True never happen. Anyone know how to solve this?
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'
result:
2015-03-25 00:00:00
<class 'pandas.tslib.Timestamp'>
Upvotes: 3
Views: 3242
Reputation: 5121
Two options for checking for today's date in a pandas Series of Timestamps ...
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)])
Note: option one uses a list comprehension to convert a pandas Series of Timestamps to a Series of datetime.date objects (using the pandas.Timestamp.date() method).
Upvotes: 4