Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison between datetime and datetime64[ns] in pandas

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

like image 767
JesusMonroe Avatar asked Aug 13 '18 16:08

JesusMonroe


People also ask

What is the difference between timestamp and datetime in pandas?

Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas. Value to be converted to Timestamp.

What is datetime format pandas?

By default pandas datetime format is YYYY-MM-DD ( %Y-%m-%d ).

Is time series supported in pandas?

pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.


2 Answers

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
like image 166
Tobias Funke Avatar answered Oct 08 '22 01:10

Tobias Funke


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 mroe.

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()
like image 22
piRSquared Avatar answered Oct 07 '22 23:10

piRSquared