try except added for getting table
Browse files
app.py
CHANGED
|
@@ -104,9 +104,25 @@ if option == "Portfolio Management":
|
|
| 104 |
st.sidebar.success("Downloading latest stock data successfully!")
|
| 105 |
|
| 106 |
# Create a DataFrame object from the closing prices of all stocks
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Set the column names to be the stocks symbols
|
| 112 |
table.columns = stocks
|
|
|
|
| 104 |
st.sidebar.success("Downloading latest stock data successfully!")
|
| 105 |
|
| 106 |
# Create a DataFrame object from the closing prices of all stocks
|
| 107 |
+
try:
|
| 108 |
+
# Assuming list_of_stocks is the list of stock data (each item is a DataFrame with 'Close' column)
|
| 109 |
+
table_data = [list_of_stocks[j]["Close"] for j in range(len(list_of_stocks))]
|
| 110 |
+
|
| 111 |
+
# Check if the data is 2D (even though it's 1D data in each row, we need to check the overall shape)
|
| 112 |
+
if len(table_data) > 0 and isinstance(table_data[0], pd.Series):
|
| 113 |
+
table_data = np.array(table_data).T # Transpose to make sure it's 2D
|
| 114 |
+
|
| 115 |
+
# Create the DataFrame with proper shape
|
| 116 |
+
table = pd.DataFrame(table_data)
|
| 117 |
+
|
| 118 |
+
# Set the column names to be the stock symbols
|
| 119 |
+
table.columns = stocks
|
| 120 |
+
|
| 121 |
+
except ValueError as e:
|
| 122 |
+
st.error(f"Error while creating the DataFrame: {e}")
|
| 123 |
+
# Gracefully handle the error by showing a fallback or empty DataFrame
|
| 124 |
+
table = pd.DataFrame(columns=stocks)
|
| 125 |
+
st.warning("An issue occurred while processing stock data, defaulting to an empty table.")\
|
| 126 |
|
| 127 |
# Set the column names to be the stocks symbols
|
| 128 |
table.columns = stocks
|