KVA
KVA

Reputation: 49

multiplying part of dataframe based on time condition with specific value

I would like to multiply values within a specified time with a value

Tried the following which works when I do it once, however I think I cannot reassign multiple times. Only 1 line works, but more than one gives NaN. How to circumvent this while still applying the multiplications to the specific time ranges?

onpeak_increase  = 1.2
offpeak_decrease = 0.8
df2 = df2.between_time('07:00:00', '11:00:00', include_start=True, include_end=False) * onpeak_increase
df2 = df2.between_time('16:00:00', '20:00:00', include_start=True, include_end=False) * onpeak_increase
df2 = df2.between_time('20:00:00', '07:00:00', include_start=True, include_end=True) * offpeak_decrease
df2 = df2.between_time('11:00:00', '16:00:00', include_start=True, include_end=False) * offpeak_decrease

Upvotes: 0

Views: 81

Answers (1)

Thibault Bacqueyrisses
Thibault Bacqueyrisses

Reputation: 2331

The problem is that df.between_time extract the corresponding rows, so you are setting your dataframe to those rows at each step.
So if i understand well what you want,i will do something like that :

idx = df2.between_time('07:00:00', '11:00:00', include_start=True, include_end=False).index
df2.loc[idx] = df2.loc[idx].values * onpeak_increase

idx = df2.between_time('16:00:00', '20:00:00', include_start=True, include_end=False).index
df2.loc[idx] = df2.loc[idx].values * onpeak_increase

idx = df2.between_time('20:00:00', '07:00:00', include_start=True, include_end=True).index
df2.loc[idx] = df2.loc[idx].values * offpeak_decrease

idx = df2.between_time('11:00:00', '16:00:00', include_start=True, include_end=False).index
df2.loc[idx] = df2.loc[idx].values * offpeak_decrease

I take as supposition that you want to multiply the values of the row corresponding to that range

Upvotes: 1

Related Questions