user1880615
user1880615

Reputation: 31

find minimum in DataFrame.index with condition

there are two DataFrames df1 and df2 and both have timestamps as indices and I am trying to find the nearest index of df2 to df1 under condition.

reference_index = df1.index[0]
index_differences = reference_index - df2.index
cond1 = index_diff >= timedelta(hours=0)
cond2 = index_diff <= timedelta(hours=1)
combined_cond = cond1 & cond2

how can I find the minimum in index_differences under condition combined_cond?

Upvotes: 2

Views: 601

Answers (2)

vks
vks

Reputation: 67968

x = [1, 2, 3, 4 ,5, 6]
y = [False, False, True, True, False, False]


print x.index(min([i for i in x if y[x.index(i)]]))

You can try this.

Upvotes: 1

itzy
itzy

Reputation: 11755

x = Series([1, 2, 3, 4 ,5, 6])
y = Series([False, False, True, True, False, False])
x[y].argmin()

Upvotes: 1

Related Questions