Coverage for statistic/adjust_time.py: 94%
65 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-21 18:48 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-21 18:48 +0000
1import pandas as pd
2import sys
3import os
5# Определите путь до корня проекта
6project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
7if project_root not in sys.path:
8 sys.path.append(project_root)
11from src.settings import Settings
15def adjust_time1(df):
16 """ adjust time in the dataframe if there are more than 2 orders in the same time
17 if two orders adds 10 minutes to the second order
18 if three orders removes 10 minutes from the first order and adds 10 minutes to the third order
19 if four orders removes 20 minutes from the first order and 10 minutes from the second order
20 and adds 10 minutes to the fourth order
21 ...
22 Args:
23 df (DataFrame pandas): dataframe with the orders
25 Returns:
26 DataFrame: dataframe with the adjusted time
27 """
28 max_iterations = 10 # max number of iterations to avoid infinite loop
29 iteration = 0
31 while iteration < max_iterations:
32 # group by time and wenz
33 time_groups = df.groupby(['time', 'wenz'])
35 any_changes = False
37 result = []
38 for _ , group in time_groups:
39 if len(group) == 2:
40 group.iat[0, group.columns.get_loc('time')] -= pd.Timedelta(minutes=5 + iteration)
41 any_changes = True
42 elif len(group) == 3:
43 group.iat[0, group.columns.get_loc('time')] -= pd.Timedelta(minutes=5 + iteration)
44 group.iat[2, group.columns.get_loc('time')] += pd.Timedelta(minutes=5 + iteration)
45 any_changes = True
46 elif len(group) == 4:
47 group.iat[0, group.columns.get_loc('time')] -= pd.Timedelta(minutes=10 + iteration)
48 group.iat[1, group.columns.get_loc('time')] -= pd.Timedelta(minutes=5 + iteration)
49 group.iat[3, group.columns.get_loc('time')] += pd.Timedelta(minutes=5 + iteration)
50 any_changes = True
51 elif len(group) >= 5:
52 group.iat[0, group.columns.get_loc('time')] -= pd.Timedelta(minutes=10 + iteration)
53 group.iat[1, group.columns.get_loc('time')] -= pd.Timedelta(minutes=5 + iteration)
54 group.iat[-2, group.columns.get_loc('time')] += pd.Timedelta(minutes=5 + iteration)
55 group.iat[-1, group.columns.get_loc('time')] += pd.Timedelta(minutes=10 + iteration)
56 any_changes = True
57 result.append(group)
59 df = pd.concat(result)
61 if not any_changes:
62 break # if no changes were made, exit the loop
64 iteration += 1
66 return df
69min_interval = pd.Timedelta(minutes=7)
71def adjust_time2(df):
72 """adjust time in the dataframe if interval between the orders is less tha settings.min_interval
73 if the interval is less than settings.min_interval set it equal to settings.min_interval
75 Args:
76 df (DataFrame): dataframe with the orders
78 Returns:
79 DataFrame: dataframe with the adjusted time
80 """
81 min_interval = pd.Timedelta(minutes=Settings.min_interval)
83 df = df.sort_values(by='time').reset_index(drop=True)
84 for i in range(1, len(df)):
85 prev_time = df.loc[i - 1, 'time']
86 curr_time = df.loc[i, 'time']
88 if curr_time < prev_time:
89 curr_time = prev_time
91 df.loc[i, 'time'] = curr_time + min_interval
93 else:
94 interval = curr_time - prev_time
96 if interval < min_interval:
97 df.loc[i, 'time'] = curr_time + (min_interval - interval)
98 return df
100def adjust_times(df):
101 """combination of adjust_time1 and adjust_time2 and sort the dataframe by time,
102 reset the index and add 1 to the index
104 Args:
105 df (DataFrame): dataframe with the orders
107 Returns:
108 DataFrame: dataframe with the adjusted time and sorted by time, with index reset and 1 added to the index
109 """
111 # Spread the corses by 10 minutes if they are at the same time
112 df = adjust_time1(df)
113 df.sort_values("time", inplace=True) # type: ignore
115 # Spread the shipments so that the interval between them is not less than Settings.min_interval
116 df = df.groupby('wenz')[df.columns].apply(adjust_time2).reset_index(drop=True)
117 df.sort_values("time", inplace=True) # type: ignore
118 df.reset_index(drop=True, inplace=True)
119 df.index=df.index+1
121 return df
126if __name__ == "__main__":
127 # import sys, os
128 # from pathlib import Path
129 # project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
130 # if project_root not in sys.path:
131 # sys.path.append(project_root)
134 data = {
135 'time': [pd.Timestamp('2023-01-01 10:00:00')] * 15,
136 'wenz': ['A', 'A', 'A', 'B', 'B', 'A', 'A', 'A', 'B', 'B', 'A', 'B', 'B', 'A', 'A',]
137 }
138 df = pd.DataFrame(data)
139 print(adjust_time1(df)['time'])