-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_tools.py
More file actions
40 lines (32 loc) · 1.21 KB
/
data_tools.py
File metadata and controls
40 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pandas as pd
import statsmodels.api as sm
class Lowess:
@classmethod
def get_lowess_trend(cls, x, y):
lowess_trend = sm.nonparametric.lowess(endog=y, exog=x)
df = pd.DataFrame(lowess_trend, columns=["x", "y"])
for i in range(1, len(df)):
is_decreasing = df.loc[i, 'y'] < df.loc[i - 1, 'y']
df.loc[i, 'slope'] = -1 if is_decreasing else 1
df.dropna(inplace=True)
df.reset_index(drop=True, inplace=True)
return df
@classmethod
def get_lowess_trendlines(cls, df):
index = -1
groups = []
current_state = None
for i in range(1, len(df)):
has_changed = current_state != df.loc[i, 'slope']
x, y = df.loc[i, 'x'], df.loc[i, 'y']
if has_changed:
current_state = df.loc[i, 'slope']
groups.append({'x': [x], 'y': [y], 'slope': current_state})
index += 1
else:
groups[index]['x'].append(x)
groups[index]['y'].append(y)
for trend in groups:
per_change = round(100*(1 - (min(trend['y'])/max(trend['y']))), 2)
trend["change"] = per_change
return groups