-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
31 lines (27 loc) · 872 Bytes
/
utils.py
File metadata and controls
31 lines (27 loc) · 872 Bytes
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
import pickle as pkl
import pandas as pd
def open_pkl(path: str):
with open(path, 'rb') as o:
pkl_file = pkl.load(o)
return pkl_file
def aggregate(df,
key='time',
freq='h',
how='mean'):
"""
function aggregating a Pandas dataframe, it needs to have a time column
input:
- df: pandas DataFrame
- key: name of time column
- freq: time aggregation
- how: 'mean', 'max', 'min'
"""
if how == 'mean':
agg_df = df.groupby([pd.Grouper(key=key, freq=freq)]).mean()
elif how == 'max':
agg_df = df.groupby([pd.Grouper(key=key, freq=freq)]).max()
elif how == 'min':
agg_df = df.groupby([pd.Grouper(key=key, freq=freq)]).min()
elif how == 'count':
agg_df = df.groupby([pd.Grouper(key=key, freq=freq)]).count()
return agg_df