Skip to content

Fix: _dts_in_same_interval("1mo") ignored year - #2780

Merged
ValueRaider merged 1 commit into
ranaroussi:devfrom
gottostartsomewhere:fix/dts-same-interval-1mo-year
Apr 25, 2026
Merged

ValueRaider merged 1 commit into
ranaroussi:devfrom
gottostartsomewhere:fix/dts-same-interval-1mo-year

Conversation

@gottostartsomewhere

Copy link
Copy Markdown
Contributor

Summary

_dts_in_same_interval(dt1, dt2, "1mo") in yfinance/utils.py compared only dt1.month == dt2.month, so two timestamps that share the same calendar month but are in different years were treated as falling in the same monthly interval. For example, (2024-12-15, 2025-12-15) returned True.

The "3mo" branch already handles cross-year correctly (see the year_diff calculation just below). This PR brings the "1mo" branch in line by also requiring dt1.year == dt2.year.

# before
elif interval == "1mo":
    last_rows_same_interval = dt1.month == dt2.month
# after
elif interval == "1mo":
    last_rows_same_interval = dt1.month == dt2.month and dt1.year == dt2.year

The function is called from fix_Yahoo_returning_live_separate (yfinance/utils.py), which merges the last two rows of a price series when they fall in the same interval, so the old behaviour could merge rows that are actually a full year apart in 1-month-interval data.

Test plan

Added test_same_month_different_year in tests/test_utils.py covering (Dec 2024, Dec 2025) and (Jun 2020, Jun 2025). It fails on the old logic and passes on the fix.

  • python -m unittest tests.test_utils.TestDateIntervalCheck.test_same_month_different_year — fails before the fix, passes after.
  • python -m unittest tests.test_utils -v — all 21 tests pass (20 pre-existing + 1 new), one pre-existing expected failure unchanged.
  • Diff limited to the two intended lines of logic + the new test.

…n different years as the same interval

The "1mo" branch compared only `dt1.month == dt2.month`, so e.g.
(2024-12-15, 2025-12-15) returned True. The "3mo" branch already
handles year differences via `year_diff`; mirror that by also
requiring `dt1.year == dt2.year`.

Added a test (`test_same_month_different_year`) that fails on the
old logic and passes on the fix. All existing tests still pass.
@ValueRaider
ValueRaider merged commit af8faa0 into ranaroussi:dev Apr 25, 2026
2 checks passed
@ValueRaider ValueRaider mentioned this pull request May 20, 2026
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