-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
68 lines (55 loc) · 2.07 KB
/
utils.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
"""
import re
import pandas as pd
import numpy as np
from typing import Union, Optional, Tuple
FrameOrSeries = Union[pd.DataFrame, pd.Series]
def parse_duration(duration: str) -> Tuple[float, str]:
"""parse a pandas duration string into int and str identifier
Args:
duration (str): pandas recognizable duration string.
e.g. '1H'= 1 hour, '30T' = 30 minutes
Returns:
Tuple[int, str]: integer number
"""
num = re.findall(r'\d\.\d', duration)
if not num:
num = re.findall(r'\d', duration)
if not num:
raise ValueError(
f'duration string {duration} not recognized'
)
unit = re.findall(r'\D', duration)
return num[0], unit[0]
def c_rolling(data: FrameOrSeries,
window: str,
window_func='mean',
min_periods: Optional[int] = None,
win_type: Optional[str] = None
) -> FrameOrSeries:
"""apply and center datetime index of pandas rolling window function
Args:
data (pd.Series, pd.DataFrame): pandas datetime index obj.
window (str): pandas recognized time string
window_func (str): rolling window function to apply.
Defaults to mean (rolling average)
min_periods (int): Minimum number of observations in window
required to have a value. Defaults to None.
win_type (str): Window type for rolling window calculation.
If None, all points are evenly weighted. Defaults to None.
Returns:
Series or DataFrame: rolling window function with centered index
"""
rolled = getattr(data.rolling(
window, min_periods=min_periods, win_type=win_type), window_func)()
rolled.index = rolled.index - (pd.Timedelta(window) / 2)
return rolled
def add_in_quadrature(*num):
total = 0
for n in num:
total += n ** 2
return np.sqrt(total)
def DatetimeIndex_to_doy(idx):
"""Convert datetime index to decimal day of year."""
return idx.dayofyear + idx.hour/24 + idx.minute/(60*24) + idx.second/(60*60*24)