-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatting.py
More file actions
77 lines (73 loc) · 2.28 KB
/
formatting.py
File metadata and controls
77 lines (73 loc) · 2.28 KB
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
import pandas as pd
from datetime import datetime, time, timedelta, date
import calendar
def closest(df):
today = pd.to_datetime(date.today())
if today.weekday() == 5:
today = today - timedelta(1)
if today.weekday() == 6:
today = today - timedelta(2)
try:
end_idx = df[df['Date'] == today].index[0]
except IndexError:
end_idx = len(df)
today = df.iloc[len(df)-1]['Date']
return end_idx, today
def find_idx(df, months):
default_space = months * 21
end_idx, today = closest(df)
prev_month = today.month - months
prev_year = today.year
if prev_month <= 0:
prev_month += 12
prev_year -= 1
if today.day <= calendar.monthrange(prev_year, prev_month)[1]:
prev_today = pd.to_datetime(datetime(prev_year, prev_month, today.day))
else:
prev_today = pd.to_datetime(datetime(prev_year, prev_month, calendar.monthrange(prev_year, prev_month)[1]))
if prev_today.weekday() == 5:
prev_today = prev_today + timedelta(2)
if prev_today.weekday() == 6:
prev_today = prev_today + timedelta(1)
try:
start_idx = df[df['Date'] == prev_today].index[0]
except IndexError:
start_idx = end_idx - default_space
if start_idx < 0:
start_idx = 0
return start_idx, end_idx, today
def write_label(df):
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
labels = []
indexes = []
if len(df) > 30:
if len(df) > 100:
step = 10
else:
step = 6
else:
step = 3
i = 0
while i < len(df):
indexes.append(i)
date1 = df.iloc[i]['Date']
i += step
month = months[date1.month - 1]
if date1.day < 10:
label = "0" + str(date1.day) + "-" + month
else:
label = str(date1.day) + "-" + month
labels.append(label)
return indexes, labels
def format_vol(volume):
if volume < 1000:
return str(volume)
elif volume < 1000000:
vol = volume/1000
return str(round(vol,3)) + 'K'
elif volume < 1000000000:
vol = volume / 1000000
return str(round(vol,3)) + 'M'
else:
vol = volume / 1000000000
return str(round(vol, 3)) + 'B'