Spaces:
Runtime error
Runtime error
Commit
·
c8b5a38
1
Parent(s):
3718ec9
refactor add_details_to_stock_df to unitls.py
Browse files
api.py
CHANGED
@@ -10,6 +10,8 @@ from typing import List, Optional
|
|
10 |
from sqlalchemy import create_engine
|
11 |
import table_schema as ts
|
12 |
import os
|
|
|
|
|
13 |
db_url = 'sqlite:///instance/local.db'
|
14 |
load_dotenv()
|
15 |
user_name = os.environ.get('JQDATA_USER')
|
@@ -266,6 +268,7 @@ def fetch_stocks_price(**params):
|
|
266 |
return stocks_df
|
267 |
|
268 |
|
|
|
269 |
@auth_api
|
270 |
def update_benchmark_profile(start_date: datetime,
|
271 |
end_date: datetime,
|
@@ -338,9 +341,9 @@ def update_benchmark_profile(start_date: datetime,
|
|
338 |
subset=['ticker', 'date'], keep='last', inplace=True)
|
339 |
|
340 |
# update deail
|
341 |
-
|
342 |
-
error_message.extend(incoming_error)
|
343 |
|
344 |
-
return
|
345 |
|
346 |
# get_all_stocks_detail()
|
|
|
10 |
from sqlalchemy import create_engine
|
11 |
import table_schema as ts
|
12 |
import os
|
13 |
+
import utils
|
14 |
+
|
15 |
db_url = 'sqlite:///instance/local.db'
|
16 |
load_dotenv()
|
17 |
user_name = os.environ.get('JQDATA_USER')
|
|
|
268 |
return stocks_df
|
269 |
|
270 |
|
271 |
+
|
272 |
@auth_api
|
273 |
def update_benchmark_profile(start_date: datetime,
|
274 |
end_date: datetime,
|
|
|
341 |
subset=['ticker', 'date'], keep='last', inplace=True)
|
342 |
|
343 |
# update deail
|
344 |
+
merged_df = utils.add_details_to_stock_df(update_df)
|
345 |
+
# error_message.extend(incoming_error)
|
346 |
|
347 |
+
return merged_df, error_message
|
348 |
|
349 |
# get_all_stocks_detail()
|
utils.py
CHANGED
@@ -2,16 +2,45 @@ import pytz
|
|
2 |
import datetime
|
3 |
import io
|
4 |
import pandas as pd
|
|
|
|
|
5 |
|
|
|
6 |
|
7 |
-
def time_in_beijing():
|
8 |
'''
|
9 |
return current time in Beijing as datetime object
|
10 |
'''
|
11 |
tz = pytz.timezone('Asia/Shanghai')
|
12 |
dt = datetime.datetime.now(tz)
|
|
|
|
|
13 |
return dt
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def convert_string_to_datetime(date_string, time_zone="Asia/Shanghai"):
|
17 |
'''
|
@@ -108,3 +137,5 @@ def create_share_changes_report(df):
|
|
108 |
markdown += '{} | {} | {}\n'.format(row['ticker'],
|
109 |
row['display_name'], share_changes_str)
|
110 |
return markdown
|
|
|
|
|
|
2 |
import datetime
|
3 |
import io
|
4 |
import pandas as pd
|
5 |
+
import table_schema as ts
|
6 |
+
from sqlalchemy import create_engine
|
7 |
|
8 |
+
db_url = 'sqlite:///instance/local.db'
|
9 |
|
10 |
+
def time_in_beijing(strip_time_zone=True):
|
11 |
'''
|
12 |
return current time in Beijing as datetime object
|
13 |
'''
|
14 |
tz = pytz.timezone('Asia/Shanghai')
|
15 |
dt = datetime.datetime.now(tz)
|
16 |
+
if strip_time_zone:
|
17 |
+
dt = dt.replace(tzinfo=None)
|
18 |
return dt
|
19 |
|
20 |
+
def add_details_to_stock_df(stock_df):
|
21 |
+
'''return df adding sector, aggregate sector, display_name, name to it
|
22 |
+
|
23 |
+
Parameters
|
24 |
+
----------
|
25 |
+
stock_df: pd.DataFrame
|
26 |
+
the dataframe contain ticker columns
|
27 |
+
|
28 |
+
Returns
|
29 |
+
-------
|
30 |
+
merged_df: pd.DataFrame
|
31 |
+
the dataframe with sector, aggregate sector, display_name and name added
|
32 |
+
|
33 |
+
'''
|
34 |
+
with create_engine(db_url).connect() as conn:
|
35 |
+
detail_df = pd.read_sql(ts.STOCKS_DETAILS_TABLE, con=conn)
|
36 |
+
merged_df = pd.merge(stock_df, detail_df[
|
37 |
+
['sector', 'name',
|
38 |
+
'aggregate_sector',
|
39 |
+
'display_name',
|
40 |
+
'ticker']
|
41 |
+
], on='ticker', how='left')
|
42 |
+
merged_df['aggregate_sector'].fillna('其他', inplace=True)
|
43 |
+
return merged_df
|
44 |
|
45 |
def convert_string_to_datetime(date_string, time_zone="Asia/Shanghai"):
|
46 |
'''
|
|
|
137 |
markdown += '{} | {} | {}\n'.format(row['ticker'],
|
138 |
row['display_name'], share_changes_str)
|
139 |
return markdown
|
140 |
+
|
141 |
+
|