Passed
Pull Request — dev (#1226)
by Patrik
01:47
created

create_timeseries.reshape_unevenly()   A

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 23
rs 9.6
c 0
b 0
f 0
cc 4
nop 1
1
import pandas as pd
2
3
4
def reshape_unevenly(df):
5
6
    def to_bucket(ts: pd.Timestamp) -> pd.Timestamp:
7
        h = ts.hour
8
        d = ts.normalize()
9
        if 5 <= h <= 21:
10
            return ts
11
        if h in (21, 22, 23):
12
            return d + pd.Timedelta(hours=21)
13
        if h in (0,):
14
            return (d - pd.Timedelta(days=1)) + pd.Timedelta(hours=21)
15
        # h in (1, 2, 3, 4, 5)
16
        return d + pd.Timedelta(hours=3)
17
18
    buckets = df.index.map(to_bucket)
19
    buckets = buckets.where(buckets >= df.index[0], df.index[0])
20
21
    df_mean = df.groupby(buckets).mean().sort_index()
22
    # df_sum = df.groupby(buckets).sum().sort_index()
23
    df_mean.index.name = 'timestamp'
24
    # df_sum.index.name = 'timestamp'
25
26
    return df_mean
27