Jeremy Live
commited on
Commit
·
8304537
1
Parent(s):
d5a1ba3
v1111
Browse files
app.py
CHANGED
@@ -219,22 +219,42 @@ PLOT_FILENAME = 'generated_plot.png'
|
|
219 |
|
220 |
# Helper functions for data processing
|
221 |
def safe_get_column(df, column):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
# Try exact match first
|
223 |
if column in df.columns:
|
224 |
return df[column]
|
|
|
225 |
# Try case-insensitive match
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
230 |
# If not found, try common variations
|
231 |
variations = {
|
232 |
-
'close': ['Close', 'Adj Close', 'close', 'adj close', 'CLOSE'],
|
233 |
-
'adj close': ['Adj Close', 'adj close', 'ADJ CLOSE', 'Close', 'close', 'CLOSE']
|
234 |
}
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
# If still not found, raise a helpful error
|
239 |
raise KeyError(f"Column '{column}' not found in DataFrame. Available columns: {list(df.columns)}")
|
240 |
|
|
|
219 |
|
220 |
# Helper functions for data processing
|
221 |
def safe_get_column(df, column):
|
222 |
+
# Handle case where column is a tuple (e.g., from multi-index)
|
223 |
+
if isinstance(column, tuple):
|
224 |
+
column = column[0] # Take the first element of the tuple
|
225 |
+
|
226 |
+
# Convert column to string in case it's not
|
227 |
+
column = str(column)
|
228 |
+
|
229 |
# Try exact match first
|
230 |
if column in df.columns:
|
231 |
return df[column]
|
232 |
+
|
233 |
# Try case-insensitive match
|
234 |
+
try:
|
235 |
+
col_lower = column.lower()
|
236 |
+
for col in df.columns:
|
237 |
+
if str(col).lower() == col_lower:
|
238 |
+
return df[col]
|
239 |
+
except (AttributeError, TypeError):
|
240 |
+
pass # Skip case-insensitive matching if not applicable
|
241 |
+
|
242 |
# If not found, try common variations
|
243 |
variations = {
|
244 |
+
'close': ['Close', 'Adj Close', 'close', 'adj close', 'CLOSE', 'Adj. Close'],
|
245 |
+
'adj close': ['Adj Close', 'adj close', 'ADJ CLOSE', 'Close', 'close', 'CLOSE', 'Adj. Close']
|
246 |
}
|
247 |
+
|
248 |
+
for var_list in variations.values():
|
249 |
+
for var in var_list:
|
250 |
+
if var in df.columns:
|
251 |
+
return df[var]
|
252 |
+
|
253 |
+
# If still not found, try to find any column containing 'close'
|
254 |
+
for col in df.columns:
|
255 |
+
if 'close' in str(col).lower():
|
256 |
+
return df[col]
|
257 |
+
|
258 |
# If still not found, raise a helpful error
|
259 |
raise KeyError(f"Column '{column}' not found in DataFrame. Available columns: {list(df.columns)}")
|
260 |
|