-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path013_sort_fundamentals
executable file
·34 lines (28 loc) · 1.33 KB
/
013_sort_fundamentals
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
#!/usr/bin/env python2
import fnmatch, re
import os
import pandas as pd
import dateutil
import modules.config_details as conf_info
def sort_dataframe_by_date(df, date_column_name):
df[date_column_name] = df[date_column_name].apply(dateutil.parser.parse)
df[date_column_name] = pd.to_datetime(df[date_column_name])
df = df.sort([date_column_name])
df.index = range(0,len(df))
return df
def sort_fundamentals(files_dir, fn_regex, fn_glob):
END_DATE_COLUMN = 'end_date'
fundamentals_pattern = re.compile(fn_regex)
for file_name in os.listdir(files_dir):
if fnmatch.fnmatch(file_name, fn_glob):
current_ticker = fundamentals_pattern.search(file_name).group(1)
print 'Processing file:', file_name
# print 'Processing ticker:', current_ticker
try:
current_ticker_fundamentals_df = pd.read_csv(files_dir + file_name)
# making sure the dataframe is sorted by date
current_ticker_fundamentals_df = sort_dataframe_by_date(current_ticker_fundamentals_df, END_DATE_COLUMN)
current_ticker_fundamentals_df.to_csv(files_dir + file_name, index=False)
except Exception, e:
print '~~~ Failed to process fundamentals for ticker: ' + current_ticker + ' ~~~', str(e)
sort_fundamentals(conf_info.fundamentals_dir, conf_info.fundamentals_filename_regex, conf_info.fundamentals_filename_glob)