Spaces:
Sleeping
Sleeping
Update geo_macro.py
Browse files- geo_macro.py +14 -13
geo_macro.py
CHANGED
|
@@ -304,7 +304,7 @@ class UnifiedMarketDataDownloader:
|
|
| 304 |
close_data.columns = [ticker_to_name.get(col, col) for col in close_data.columns]
|
| 305 |
|
| 306 |
# Remove any columns that failed to download
|
| 307 |
-
close_data = close_data
|
| 308 |
|
| 309 |
print(f" ✅ Successfully downloaded {len(close_data.columns)} series")
|
| 310 |
return close_data
|
|
@@ -359,7 +359,7 @@ class UnifiedMarketDataDownloader:
|
|
| 359 |
df = pd.DataFrame(data['observations'])
|
| 360 |
df['value'] = pd.to_numeric(df['value'], errors='coerce')
|
| 361 |
df['date'] = pd.to_datetime(df['date'])
|
| 362 |
-
series = df.set_index('date')['value']
|
| 363 |
|
| 364 |
if len(series) > 10:
|
| 365 |
economic_data[name] = series
|
|
@@ -372,31 +372,32 @@ class UnifiedMarketDataDownloader:
|
|
| 372 |
continue
|
| 373 |
|
| 374 |
return economic_data
|
| 375 |
-
|
| 376 |
def _merge_all_data(self, market_data, economic_data, start_date, end_date):
|
| 377 |
-
"""Merge all data sources into single DataFrame with
|
| 378 |
|
| 379 |
-
# Create
|
| 380 |
date_range = pd.date_range(start=start_date, end=end_date, freq='D')
|
| 381 |
|
| 382 |
-
#
|
| 383 |
-
unified =
|
| 384 |
-
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
-
# Add economic data
|
| 387 |
for name, series in economic_data.items():
|
| 388 |
unified[name] = series.reindex(date_range)
|
| 389 |
|
| 390 |
-
# Forward
|
| 391 |
unified = unified.ffill().bfill()
|
| 392 |
|
| 393 |
-
#
|
| 394 |
unified = unified.dropna(axis=1, how='all')
|
| 395 |
|
| 396 |
return unified
|
| 397 |
|
| 398 |
-
|
| 399 |
-
|
| 400 |
'''
|
| 401 |
|
| 402 |
# ======================
|
|
|
|
| 304 |
close_data.columns = [ticker_to_name.get(col, col) for col in close_data.columns]
|
| 305 |
|
| 306 |
# Remove any columns that failed to download
|
| 307 |
+
close_data = close_data(axis=1, how='all')
|
| 308 |
|
| 309 |
print(f" ✅ Successfully downloaded {len(close_data.columns)} series")
|
| 310 |
return close_data
|
|
|
|
| 359 |
df = pd.DataFrame(data['observations'])
|
| 360 |
df['value'] = pd.to_numeric(df['value'], errors='coerce')
|
| 361 |
df['date'] = pd.to_datetime(df['date'])
|
| 362 |
+
series = df.set_index('date')['value']
|
| 363 |
|
| 364 |
if len(series) > 10:
|
| 365 |
economic_data[name] = series
|
|
|
|
| 372 |
continue
|
| 373 |
|
| 374 |
return economic_data
|
| 375 |
+
|
| 376 |
def _merge_all_data(self, market_data, economic_data, start_date, end_date):
|
| 377 |
+
"""Merge all data sources into single DataFrame with ffill + bfill"""
|
| 378 |
|
| 379 |
+
# Create a full daily date range (including weekends/holidays)
|
| 380 |
date_range = pd.date_range(start=start_date, end=end_date, freq='D')
|
| 381 |
|
| 382 |
+
# Initialize unified DataFrame with full date index
|
| 383 |
+
unified = pd.DataFrame(index=date_range)
|
| 384 |
+
|
| 385 |
+
# Add market data (already date-indexed from yfinance)
|
| 386 |
+
for col in market_data.columns:
|
| 387 |
+
unified[col] = market_data[col].reindex(date_range)
|
| 388 |
|
| 389 |
+
# Add FRED economic data
|
| 390 |
for name, series in economic_data.items():
|
| 391 |
unified[name] = series.reindex(date_range)
|
| 392 |
|
| 393 |
+
# Forward-fill, then backward-fill to handle leading/trailing NaNs
|
| 394 |
unified = unified.ffill().bfill()
|
| 395 |
|
| 396 |
+
# Remove any columns that are still entirely NaN (e.g., failed downloads)
|
| 397 |
unified = unified.dropna(axis=1, how='all')
|
| 398 |
|
| 399 |
return unified
|
| 400 |
|
|
|
|
|
|
|
| 401 |
'''
|
| 402 |
|
| 403 |
# ======================
|