Convert datetime64[ns] Index to date pandas for comparison

Viewed 824

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?

1 Answers

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

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