Marco
Marco

Reputation: 1235

Convert datetime64[ns] Index to date pandas for comparison

I have the following python code:

current_ts = datetime.datetime.now()
current_date = current_ts.date()

new_df = df[df.index >= current_date]

The df.index is a datetime64[ns] and when I run the code I get Invalid comparison between dtype=datetime64[ns] and date.

How can I convert the index into a date so that the comparison works?

Upvotes: 2

Views: 1293

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Pandas datetime64[ns] doesn't compare directly to datetime.date. You need to convert:

df[df.index >= pd.Timestamp(current_date)]

Upvotes: 1

Related Questions