How do I find multiple slopes for each hour where index is timestamp?

Viewed 184

So my df is per minute readings from sensor. I want to find slopes for every hour readings, meaning every 1 hour should have 1 slope value. How to I do that?

I've tried groupby.apply linregress , not working, also tried to groupby 60 rows, but that just gives mean values for the hour. Column 1 is T_a and Column 2 is Q_a. I want slope values for T_am vs Q_a for every hour.

df.head()
2019-01-09 17:03:00   3.09125   93.353877
2019-01-09 17:04:00   3.08575   89.513643
2019-01-09 17:05:00   3.10325   92.700350
2019-01-09 17:06:00   3.08075   91.089470
2019-01-09 17:07:00   3.08200   92.563898

df = df.groupby(index).apply(lambda df: linregress(df.T_am, df.Q_a)[0])

KeyError: Timestamp('2019-01-09 18:02:00')
1 Answers

You need to groupby on your index hours, if you have only one day of measurements this should work:

df.groupby(index.hour).apply(lambda df: linregress(df.T_am, df.Q_a)[0])

If you instead have multiple days I would first create a column containing your index rounded to the hour and then group on that index:

df['index_hour']=df.index.floor('h')
df.groupby('index_hour').apply(lambda df: linregress(df.T_am, df.Q_a)[0])

If you want to keep several parameters from the linear regression you can use the following script:

def extract_lr(x):
    lr = linregress(x['T_am'], x['Q_a'])
    return pd.Series([lr.slope, lr.intercept, lr.rvalue, lr.pvalue],
                     index=['slope', 'intercept', 'rvalue','pvalue'])

df.groupby('index_hour').apply(lambda x: extract_lr(x))

credits to this answer.

Related