Bill Software Engineer
Bill Software Engineer

Reputation: 7782

Check if a date index exist in Pandas dataframe

I already checked similar answer but it seems what work for string index doesn't work for date index:

print("Current ending date is "+str(endDate.date()))
if(endDate.date() not in data.index.values):
     print("Last date does not exist for "+ticker)
     print(data.tail(1))

Printout

Current ending date is 2021-04-27
Last date does not exist for DAX
            DAX_movement  DAX_movement_5days  DAX_movement_21days  DAX_diff_SP500
Date                                                                             
2021-04-27     -0.003087            0.007853             0.041176             NaN

Also tried:

if(endDate not in data.index.values):
if(endDate.date() not in data.index):
if(endDate not in data.index):

The row clearly exist but it's not finding it, how do I check if date index exist?

Upvotes: 1

Views: 2127

Answers (1)

BENY
BENY

Reputation: 323226

First make the sure the index is datetime format

df.index = pd.to_datetime(df.index)

Then,

pd.to_datetime(endDate) not in data.index

Upvotes: 3

Related Questions