Skip to content

Fix DeprecationWarning on every import: Timedelta generic unit - #2357

Open
Krishna Anubhav (heykav) wants to merge 1 commit into
microsoft:mainfrom
heykav:fix/timedelta-generic-unit-deprecation
Open

Krishna Anubhav (heykav) wants to merge 1 commit into
microsoft:mainfrom
heykav:fix/timedelta-generic-unit-deprecation

Conversation

@heykav

Copy link
Copy Markdown

qlib/constant.py builds ONE_DAY/ONE_MIN/EPS_T via pd.Timedelta("1day")/("1min")/("1s") string parsing. On current pandas, that path hits NumPy's deprecated "generic" timedelta unit and warns:

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

Since these are module-level constants evaluated at import time, this fires on every single import of qlib — and per pandas's own warning text, it "will raise an error in the future," not just warn indefinitely.

Fix

pd.Timedelta(<n>, <unit>) (the two-argument form) takes pandas's explicit-unit construction path instead of string parsing, producing byte-identical values without hitting the deprecated path:

ONE_DAY = pd.Timedelta(1, "D")
ONE_MIN = pd.Timedelta(1, "min")
EPS_T = pd.Timedelta(1, "s")

Checked for the same string-parsing pattern (Timedelta("1day") etc.) elsewhere in qlib/ — this was the only site.

Verification

  • python -W error::DeprecationWarning -c "import qlib.constant" raises before this fix, is clean after.
  • ONE_DAY/ONE_MIN/EPS_T values unchanged: 1 days 00:00:00 / 0 days 00:01:00 / 0 days 00:00:01.
  • tests/test_register_ops.py + tests/test_structured_cov_estimator.py: 6/6, same as before the change.

qlib/constant.py builds ONE_DAY/ONE_MIN/EPS_T via pd.Timedelta("1day")
/("1min")/("1s") string parsing. On current pandas, that path hits
NumPy's deprecated 'generic' timedelta unit and warns - which fires on
every single import of qlib, since these are module-level constants
evaluated at import time, and 'will raise an error in the future' per
pandas's own warning text.

pd.Timedelta(<n>, <unit>) (the two-argument form) takes pandas's
explicit-unit construction path instead, producing byte-identical
values without the deprecated path. Checked for the same
string-parsing pattern elsewhere in qlib/ - this was the only site.

Verified: import qlib.constant with DeprecationWarning promoted to an
error raises before the fix, is clean after; ONE_DAY/ONE_MIN/EPS_T
values unchanged (1 days 00:00:00 / 0 days 00:01:00 / 0 days
00:00:01); tests/test_register_ops.py and
tests/test_structured_cov_estimator.py still 6/6.

Co-Authored-By: Claude Sonnet 5 <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