77

I'm writing a program that checks an excel file and if today's date is in the excel file's date column, I parse it

I'm using:

cur_date = datetime.today()

for today's date. I'm checking if today is in the column with:

bool_val = cur_date in df['date'] #evaluates to false

I do know for a fact that today's date is in the file in question. The dtype of the series is datetime64[ns]

Also, I am only checking the date itself and not the timestamp afterwards, if that matters. I'm doing this to make the timestamp 00:00:00:

cur_date = datetime.strptime(cur_date.strftime('%Y_%m_%d'), '%Y_%m_%d')

And the type of that object after printing is datetime as well

JesusMonroe
  • 1,421
  • 3
  • 13
  • 20

3 Answers3

103

For anyone who also stumbled across this when comparing a dataframe date to a variable date, and this did not exactly answer your question; you can use the code below.

Instead of:

self.df["date"] = pd.to_datetime(self.df["date"])

You can import datetime and then add .dt.date to the end like:

self.df["date"] = pd.to_datetime(self.df["date"]).dt.date
Tobias Funke
  • 1,614
  • 3
  • 13
  • 23
52

You can use

pd.Timestamp('today')

or

pd.to_datetime('today')

But both of those give the date and time for 'now'.


Try this instead:

pd.Timestamp('today').floor('D')

or

pd.to_datetime('today').floor('D')

You could have also passed the datetime object to pandas.to_datetime but I like the other option more.

pd.to_datetime(datetime.datetime.today()).floor('D')

Pandas also has a Timedelta object

pd.Timestamp('now').floor('D') + pd.Timedelta(-3, unit='D')

Or you can use the offsets module

pd.Timestamp('now').floor('D') + pd.offsets.Day(-3)

To check for membership, try one of these

cur_date in df['date'].tolist()

Or

df['date'].eq(cur_date).any()
principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 1
    What if I was checking for 3 days before the current date? I did today = today - timedelta(3) when it was a datetime object – JesusMonroe Aug 13 '18 at 16:58
  • 3
    `pd.Timestamp('today').floor('D') - pd.offsets.Day(3)` – piRSquared Aug 13 '18 at 16:59
  • Is there a resource on what exactly `to_datetime` will accept? Does it just boil down to a case of trial-and-error/read the source? The docs just seem to say "arg : integer, float, string, datetime, list, tuple, 1-d array, Series" which is... kinda extensive. – roganjosh Aug 13 '18 at 17:01
  • That about sums it up. By resource, do you mean a doc that demonstrates all of the above? – piRSquared Aug 13 '18 at 17:03
  • @piRSquared yeah, maybe I was hoping for too much, but my search drew nothing so I threw in a last-ditch question to see if you knew a specific resource. – roganjosh Aug 13 '18 at 17:03
  • Unfortunately, I do not. Would make a decent canonical or pr though. – piRSquared Aug 13 '18 at 17:04
  • I'm still getting false when I try to check `cur_date = pd.Timestamp('today').floor('D') - pd.offsets.Day(3)` `bool_val = cur_date in df['date'] #evaluates false` – JesusMonroe Aug 13 '18 at 17:08
4

When converting datetime64 type using pd.Timestamp() it is important to note that you should compare it to another timestamp type. (not a datetime.date type)


Convert a date to numpy.datetime64

date = '2022-11-20 00:00:00'
date64 = np.datetime64(date)

Seven days ago - timestamp type

sevenDaysAgoTs = (pd.to_datetime('today')-timedelta(days=7))

convert date64 to Timestamp and see if it was in the last 7 days

print(pd.Timestamp(pd.to_datetime(date64)) >= sevenDaysAgoTs)

  • I added this answer because In future versions Timestamp and datetime.date will be considered non-comparable. `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.` – JerryMcDonald.dev Nov 23 '22 at 20:20