forked from devAmoghS/Python-Interview-Problems-for-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeseries.py
77 lines (57 loc) · 1.56 KB
/
timeseries.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
69
70
71
72
73
74
75
76
77
# Libraries Included:
# Numpy, Scipy, Scikit, Pandas
import pandas as pd
#print("Hello, world!")
def get_merged(series1, series2):
merged = []
temp = {}
for i in series1:
if i[0] not in temp.keys():
temp[i[0]] = i[1]
else:
curr = temp.get(i[0])
temp[i[0]] = (curr + i[1]) / 2
for i in series2:
if i[0] not in temp.keys():
temp[i[0]] = i[1]
else:
curr = temp.get(i[0])
temp[i[0]] = (curr + i[1]) / 2
#print(temp)
ordered = sorted(temp.items(), key=temp.items[1], reverse=False)
#print(ordered)
#for k,v in ordered.items():
# merged.append((k,v))
return ordered
# timeseries data
series1 = [
('2010-01-01', 34),
('2010-01-02', 27),
('2010-01-04', 58),
('2010-01-05', 22)]
series2 = [
('2010-01-01', 15),
('2010-01-03', 39),
('2010-01-05', 23),
('2010-01-06', 47)]
res = get_merged(series1, series2)
print(res)
# merged = [
# ('2010-01-01', 34),
# ('2010-01-02', 27),
# ('2010-01-03', 39),
# ('2010-01-04', 58),
# ('2010-01-05', 22.5),
# ('2010-01-06', 47)]
# input array of series
# subfunc(inp_ser, temp) -> updated_temp
# keep track of series encounteered
# freq_dict -> (date, counter)
for i in series1:
if i[0] not in temp.keys():
temp[i[0]] = i[1]
freq_dict[i[0]] = 1
else:
curr = temp.get(i[0])
freq_dict[i[0]] += 1
temp[i[0]] = (curr + i[1]) / freq_dict.get(i[0])