Passed
Pull Request — dev (#1226)
by Uwe
01:53
created

create_timeseries   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A reshape_unevenly() 0 23 4
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