Skip to content

Fix generic-unit timedelta DeprecationWarning for sub-day intervals (#2882)#2891

Open
DogInfantry wants to merge 1 commit into
ranaroussi:devfrom
DogInfantry:fix-2882-timedelta-warning
Open

Fix generic-unit timedelta DeprecationWarning for sub-day intervals (#2882)#2891
DogInfantry wants to merge 1 commit into
ranaroussi:devfrom
DogInfantry:fix-2882-timedelta-warning

Conversation

@DogInfantry

@DogInfantry DogInfantry commented Jul 8, 2026

Copy link
Copy Markdown

@

Summary

Fixes #2882.

_interval_to_timedelta fell through to _pd.Timedelta(interval) for sub-day intervals ("1m", "30m", "1h", …). Passing a bare interval string makes numpy >= 2.5 emit:

DeprecationWarning: The generic unit for NumPy timedelta is deprecated, and will raise an error in the future.

flooding the console on any intraday download and set to become a hard error in a future numpy/pandas release.

Changes

  • yfinance/utils.py: parse minute intervals as Timedelta(minutes=...) and hour intervals as Timedelta(hours=...) with explicit units in _interval_to_timedelta.
  • Route the second warning site, _dts_in_same_interval (was calling _pd.Timedelta(interval) inline), through _interval_to_timedelta so both reported lines (484 and 667 in the issue) are covered by one fix.
  • Values are unchanged (pd.Timedelta("1m") already parsed as 1 minute); only the code path changes.

Tests

Added to tests/test_utils.py:

  • test_no_generic_timedelta_deprecation_warning - asserts no generic-unit DeprecationWarning for 1m/2m/5m/15m/30m/60m/90m/1h.
  • test_interval_to_timedelta_values - locks the parsed values.

python -m pytest tests/test_utils.py → 22 passed, 1 xfailed (pre-existing).

Note: my local env has numpy 2.4.6 (< 2.5), so the warning does not fire there; the fix removes the bare-string path that warns on numpy >= 2.5 and the new test is version-agnostic.
@

pd.Timedelta() was called with bare interval strings (e.g. "1h", "30m"),
which numpy>=2.5 flags with 'The generic unit for NumPy timedelta is
deprecated'. Parse minute/hour intervals with explicit units in
_interval_to_timedelta and route _dts_in_same_interval through it so both
warning sites are covered.

Fixes ranaroussi#2882

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.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.

1 participant