BillyRay
BillyRay

Reputation: 413

Compare Pandas DatetimeIndex to a smaller DatetimeIndex with condition

I am trying to compare two Pandas DatetineIndexs with different lengths. I want to simply add a new column to my larger Dataframe (df) and place 1 where it matches the smaller DatetineIndex (events).

I tried

df['filter'] = np.where(df.index == tEvents, 1, np.nan)

but I get "ValueError: Lengths must match to compare"

I've been stuck here longer than I like to admit

Comparing different python datetime index with different lengths

Upvotes: 1

Views: 2856

Answers (1)

W Stokvis
W Stokvis

Reputation: 1439

tEvents and df.index are different lengths. df.index == tEvents looks to compare the two lists.

You want to check if an element in df.index is in tEvents. Thus replace df.index == tEvents with df.index.isin(tEvents)

To see add a True or false value if date matches, use DataFrame.isin()

Upvotes: 3

Related Questions