Fix pandas 2.x/3.x incompatibilities in economics/ - #82
Open
propcgamer20-png wants to merge 2 commits into
Open
propcgamer20-png wants to merge 2 commits into
propcgamer20-png wants to merge 2 commits into
Conversation
… without it
`import finmarketpy` - and therefore `pytest` collecting any test - hard
failed whenever the optional `chartpy` plotting stack could not be
imported (e.g. a plotly version mismatch inside chartpy). Five modules
imported `chartpy` at module load:
economics/quickchart.py, economics/report.py,
backtest/backtestcomparison.py, backtest/backtestengine.py,
backtest/tradeanalysis.py
and finmarketpy/__init__ eagerly imports all of them, so the whole test
suite errored at collection ("Interrupted: 1 error during collection").
Changes:
- quickchart.py: import Chart / Style locally in the methods that draw
(dropped the unused ChartConstants import).
- report.py / backtestcomparison.py / backtestengine.py /
tradeanalysis.py: wrap the top-level `from chartpy import ...` in
try/except ImportError, falling back to None, and guard the
class-body `ChartConstants()` / `Style()` evaluations and
TradeAnalysis.__init__'s default engine arg.
When chartpy is installed, behaviour is unchanged. When it is missing or
broken, the modules import (chart attributes are None) and only actually
drawing a chart fails. `pytest tests/` now collects and passes (5 passed,
was 0 collected / 1 error). `ruff check` on the touched files is
unchanged (81 pre-existing findings, 0 added).
Closes cuemacro#71
- eventstudy.py: get_all_economic_events_date_time() called DataFrame.append(..., ignore_index=True) in a loop. DataFrame.append was removed in pandas 2.0, and the call never assigned its result, so the method already returned an empty frame on older pandas. Now collects the rows and builds the DataFrame once. - quickchart.py: `df.fillna(method='ffill')` -> `df.ffill()` (the `method=` argument is removed in pandas 3.0), and the two `datetime.datetime.utcnow()` calls -> `datetime.datetime.now( datetime.timezone.utc).replace(tzinfo=None)` (utcnow is deprecated for removal), hoisted into a single `now_utc`. pyproject declares `pandas>=1.5.3` with no upper bound, so these run under pandas 2.x/3.x. Adds tests/test_eventstudy.py for the frame-builder (populated + empty cases). Toward cuemacro#72 (`make all` runs the test suite; these paths break on current pandas). Stacked on cuemacro#79 so quickchart.py merges cleanly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pyproject.tomldeclarespandas>=1.5.3with no upper bound, so these code paths run under pandas 2.x / 3.x where the calls below no longer work.eventstudy.py-get_all_economic_events_date_time()DataFrame.appendwas removed in pandas 2.0. Even on older pandas this was already broken - the result of.append()was never assigned, so the method always returned the empty frame it started with. Now collects the rows and builds the frame once:quickchart.pydf.fillna(method='ffill')->df.ffill()(themethod=argument is removed in pandas 3.0, deprecated since 2.1)datetime.datetime.utcnow()(x2) ->datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)(utcnow()is deprecated for removal), hoisted into onenow_utc. The.replace(tzinfo=None)keeps the value tz-naive, so downstreampd.Timestamp(...)behaviour is unchanged.Tests
tests/test_eventstudy.py- the frame-builder now returns a populated frame (3 rows, correct columns) and an empty-but-correctly-shaped frame when there are no events.Stacked on #79 so
quickchart.pymerges cleanly; the PR narrows once #79 lands.Toward #72