Spaces:
Sleeping
Sleeping
pd.merge replace pd.concat
Browse files- performance.py +13 -6
performance.py
CHANGED
|
@@ -14,7 +14,7 @@ Calculate annual, trailing, cumumlative, and CAGR returns for multiple stocks.
|
|
| 14 |
|
| 15 |
Author: Gang Luo
|
| 16 |
'''
|
| 17 |
-
script_version = '(2024-01-24.
|
| 18 |
import gradio as gr
|
| 19 |
import yfinance as yf
|
| 20 |
import pandas as pd
|
|
@@ -36,10 +36,10 @@ tickers_lists = [["spy", "vfv.to","xiu.to"], #0
|
|
| 36 |
"slf.to", "gwo.to", "bce.to", "t.to", "rci-b.to", "enb.to", "trp.to", "zlb.to", "cp.to"], #1
|
| 37 |
["spy","vfv.to", "xiu.to", "zeb.to", "xfn.to", "na.to","ry.to", "bmo.to","bns.to", "td.to", "cm.to", "cwb.to",
|
| 38 |
"slf.to", "gwo.to", "bce.to", "t.to", "rci-b.to", "enb.to", "trp.to", "xdv.to","cdz.to","vdy.to"], #2
|
| 39 |
-
["
|
| 40 |
["^GSPC","spy","voo","ivv", "vfv.to", "zsp.to","xus.to", "xsp.to","^IXIC","qqq","hxq.to","^GSPTSE","xic.to","xiu.to","xfn.to", "fie.to"], #4
|
| 41 |
["^IXIC","ONEQ","CIBR","QQJG", "qqq", "spy", "vfv.to", "HXQ.to", "ZQQ.to", "XQQ.to", "QQC.to"], #5
|
| 42 |
-
["goog", "msft", "
|
| 43 |
]
|
| 44 |
|
| 45 |
#==============================================================================
|
|
@@ -135,9 +135,15 @@ def get_annual_returns_tickers_common_df(tickers, calculation_end_date_str, annu
|
|
| 135 |
if all_tickers_returns_df.empty:
|
| 136 |
all_tickers_returns_df = ticker_returns_df
|
| 137 |
else:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
else:
|
| 142 |
# New column with NaN values
|
| 143 |
new_column_name = ticker
|
|
@@ -473,3 +479,4 @@ def calculation_response(message, history):
|
|
| 473 |
|
| 474 |
demo = gr.ChatInterface(calculation_response)
|
| 475 |
demo.launch(debug=False, share=False)
|
|
|
|
|
|
| 14 |
|
| 15 |
Author: Gang Luo
|
| 16 |
'''
|
| 17 |
+
script_version = '(2024-01-24.7)'
|
| 18 |
import gradio as gr
|
| 19 |
import yfinance as yf
|
| 20 |
import pandas as pd
|
|
|
|
| 36 |
"slf.to", "gwo.to", "bce.to", "t.to", "rci-b.to", "enb.to", "trp.to", "zlb.to", "cp.to"], #1
|
| 37 |
["spy","vfv.to", "xiu.to", "zeb.to", "xfn.to", "na.to","ry.to", "bmo.to","bns.to", "td.to", "cm.to", "cwb.to",
|
| 38 |
"slf.to", "gwo.to", "bce.to", "t.to", "rci-b.to", "enb.to", "trp.to", "xdv.to","cdz.to","vdy.to"], #2
|
| 39 |
+
["spy", "vfv.to", "vgg.to", "zlu.to","goog", "msft", "meta", "tsla","AMZN", "AAPL", "shop.to"], #3
|
| 40 |
["^GSPC","spy","voo","ivv", "vfv.to", "zsp.to","xus.to", "xsp.to","^IXIC","qqq","hxq.to","^GSPTSE","xic.to","xiu.to","xfn.to", "fie.to"], #4
|
| 41 |
["^IXIC","ONEQ","CIBR","QQJG", "qqq", "spy", "vfv.to", "HXQ.to", "ZQQ.to", "XQQ.to", "QQC.to"], #5
|
| 42 |
+
["goog", "msft", "^GSPC"]
|
| 43 |
]
|
| 44 |
|
| 45 |
#==============================================================================
|
|
|
|
| 135 |
if all_tickers_returns_df.empty:
|
| 136 |
all_tickers_returns_df = ticker_returns_df
|
| 137 |
else:
|
| 138 |
+
'''
|
| 139 |
+
When running in huggingface, pd.concat changed the index order of ticker_returns_df
|
| 140 |
+
when ticker_returns_df has more rows than all_tickers_returns_df. However, it is ok
|
| 141 |
+
running in colab. Therefore, use pd.merge to replace pd.concat.
|
| 142 |
+
all_tickers_returns_df = pd.concat([all_tickers_returns_df, ticker_returns_df],axis=1,join='outer') # Concatenate DataFrames
|
| 143 |
+
all_tickers_returns_df.sort_index() # index may be changed when running in huggingface
|
| 144 |
+
'''
|
| 145 |
+
all_tickers_returns_df = pd.merge(all_tickers_returns_df, ticker_returns_df,
|
| 146 |
+
left_index=True, right_index=True, how='outer')
|
| 147 |
else:
|
| 148 |
# New column with NaN values
|
| 149 |
new_column_name = ticker
|
|
|
|
| 479 |
|
| 480 |
demo = gr.ChatInterface(calculation_response)
|
| 481 |
demo.launch(debug=False, share=False)
|
| 482 |
+
|