forked from FinanceData/FinanceDataReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_utils.py
More file actions
55 lines (42 loc) · 1.38 KB
/
_utils.py
File metadata and controls
55 lines (42 loc) · 1.38 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
import re
from datetime import datetime
from pandas import DataFrame, to_datetime
def _convert_letter_to_num(str_num):
powers = {'B': 10 ** 9, 'M': 10 ** 6, 'K': 10 ** 3, '': 1}
m = re.search("([0-9\.]+)(M|B|K|)", str_num)
if m:
val = m.group(1)
mag = m.group(2)
return float(val) * powers[mag]
return 0.0
def _validate_dates(start, end):
start = to_datetime(start)
end = to_datetime(end)
if start is None:
start = datetime(1970, 1, 1)
if end is None:
end = datetime.today()
return start, end
def _filter_by_date(df: DataFrame, start, end):
"""
Retrieves a dataframe based on a given start and end date.
:param df: an original dataframe
:param start: start date
:param end: end date
"""
date_query = 'index>=%r and index<=%r'
filtered_df = df.query(date_query % (start, end))
return filtered_df
def _replace_ohl_0_with_c(df: DataFrame):
"""
Due to events such as stock split,
if there is a row with no trading volume
and the closing price of the previous day is maintained,
it is replaced with an ohl value.
:param df: an original dataframe
"""
is_trading_holiday = df['Volume'] == 0
replace_target_columns = ['Open', 'High', 'Low']
replace_value = df['Close']
df.loc[is_trading_holiday, replace_target_columns] = replace_value
return df