Skip to content

Fix Python 3.12 / NumPy 2.0 / pandas 2.x compatibility and runtime bugs - #48

Draft
bbcho with Copilot wants to merge 8 commits into
masterfrom
copilot/fix-issue-in-risk-calculation
Draft

Fix Python 3.12 / NumPy 2.0 / pandas 2.x compatibility and runtime bugs#48
bbcho with Copilot wants to merge 8 commits into
masterfrom
copilot/fix-issue-in-risk-calculation

Conversation

Copilot AI commented Aug 30, 2026

Copy link
Copy Markdown

Several latent bugs surface on modern dependency versions (Python 3.12, NumPy 2.0, pandas 2.x): deprecated bitwise-NOT on isinstance, removed private NumPy submodule, unguarded division-by-zero, and bare exception handlers referencing unbound variables.

_pa.py

  • ~isinstance(x, T)not isinstance(x, T) throughout (bitwise NOT on bool is deprecated; becomes SyntaxError in future Python)
  • Fix operator-precedence bug: not A & B evaluated as not (A & B) — should be (not A) and B
  • Guard return_annualized against empty Series (n=0ZeroDivisionError)
  • Guard find_drawdowns against empty Series input; initialize all result keys before loop; move series_flag unwrap outside loop (was skipped by continue)

_multivariate.py

  • Same ~isinstance()not isinstance() fix in simulate_gbm_MV

_main_functions.py

  • _get_eia_df_v2 bare except used print(r.text) where r is unbound when requests.get() itself raises → except Exception as e
  • Return a properly-columned empty DataFrame when all EIA requests fail (downstream callers expect ["date", "value", "table_name", "series_id"] columns)
  • Guard trade_stats perc_win, perc_in_mkt against zero denominator; guard dd_length/dd_max against empty drawdown arrays

_charts.py

  • Replace cryptic KeyError/AttributeError in chart_eia_sd / chart_eia_steo with a clear ValueError when get_eia_df returns no data

pytest/test_risktools.py

  • Remove from numpy.linalg.linalg import eigvalsnumpy.linalg.linalg private submodule was removed in NumPy 2.0 (import was unused anyway)

Copilot AI and others added 6 commits August 30, 2026 18:11
Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
- Remove unused numpy.linalg.linalg import (removed in NumPy 2.0)
- Replace all ~isinstance() with not isinstance() in _pa.py and _multivariate.py
- Fix operator precedence bug in elif (not isinstance(...)) & isinstance(...)
- Guard return_annualized against empty Series (ZeroDivisionError)
- Guard find_drawdowns against empty Series (IndexError/KeyError)
- Initialize all drawdown result keys before loop to handle empty input
- Move series_flag conversion outside for loop so it always runs
- Guard trade_stats perc_win/perc_in_mkt against zero denominator
- Guard dd_length/dd_max against empty drawdown arrays
- Fix UnboundLocalError in _get_eia_df_v2 exception handler
- Return typed empty DataFrame from _get_eia_df_v2 when all requests fail
- Raise clear ValueError in chart_eia_sd/chart_eia_steo when no data

Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Copilot AI changed the title Fix Python 3.12+ compatibility and runtime bugs Fix Python 3.12 / NumPy 2.0 / pandas 2.x compatibility and runtime bugs Aug 30, 2026
Copilot AI requested a review from bbcho August 30, 2026 18:34
Copilot AI and others added 2 commits August 30, 2026 19:00
…nal API tests

- Fix DatetimeIndex.isin() with string dates (broken in pandas 2.0): use pd.to_datetime()
  in test_prompt_beta to correctly filter out the 2020-04-20/21 negative oil price events.
  This was causing prompt_beta to compute incorrect betas (0.55 vs expected 0.97 for CL02).
- Add _network_available() helper and @requires_network decorator to test_risktools.py
- Mark test_ir_df_us, test_trade_stats, test_get_eia_df, test_chart_eia_sd,
  test_chart_eia_steo as @requires_network so they are skipped in sandboxed/CI environments
  without API credentials or internet access

Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Co-authored-by: bbcho <47279398+bbcho@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants