Skip to content

Fix pandas 2.x/3.x incompatibilities in economics/ - #82

Open
propcgamer20-png wants to merge 2 commits into
cuemacro:masterfrom
propcgamer20-png:pandas-compat-fixes-v2
Open

propcgamer20-png wants to merge 2 commits into
cuemacro:masterfrom
propcgamer20-png:pandas-compat-fixes-v2

Conversation

@propcgamer20-png

Copy link
Copy Markdown

pyproject.toml declares pandas>=1.5.3 with 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()

for time in event_times:
    data_frame.append({'event-name': event, 'release-date-time-full': time}, ignore_index=True)
return data_frame

DataFrame.append was 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:

rows = []
for event in event_names:
    for time in self.get_economic_event_date_time(event):
        rows.append({'event-name': event, 'release-date-time-full': time})
data_frame = pandas.DataFrame(rows, columns=columns)

quickchart.py

  • df.fillna(method='ffill') -> df.ffill() (the method= 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 one now_utc. The .replace(tzinfo=None) keeps the value tz-naive, so downstream pd.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.

$ python -m pytest tests/ -q
7 passed

Stacked on #79 so quickchart.py merges cleanly; the PR narrows once #79 lands.

Toward #72

… 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.
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.

1 participant