GeezerVison is showing github.com — TYP: allow floor division of Series[Any] by a timedelta scalar by Blizzeq · Pull Request #1968 · pandas-dev/pandas-stubs
Conversation
Blizzeq
commented
Oct 4, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Series[Any] // pd.Timedelta(...) was rejected by mypy, pyright, and pyrefly because timedelta scalars were only handled through Supports_ProtoFloorDiv, which cannot resolve type variables on Series[Any]. Instead of adding Series[Never] overloads, this widens other on the existing self: Series[Timedelta] overloads of floordiv and floordiv with timedelta | np.timedelta64. That lets Series[Any] // timedelta resolve to Series[int] while preserving invalid-operand diagnostics on concrete numeric types like Series[int]. Two invalid-usage assertions on untyped Series[Any] in test_floordiv_py_scalar are commented out since left_i is annotated pd.Series (Series[Any]).
AI Implementation Plan
Motivation
#1904 requires (data[0] - data[1]) // pd.Timedelta(seconds=1) (Series[Any] // Timedelta) to type-check cleanly across mypy, pyright, pyrefly, and ty without relying on Series[Never] overloads (which broke or risked breaking across type checker updates, see python/mypy#22011).
Solution and Alternatives
- Chosen approach. Add
timedelta | np.timedelta64 to other on Series.__floordiv__(self: Series[Timedelta], ...) and Series.floordiv(self: Series[Timedelta], ...) in pandas-stubs/core/series.pyi. Gradual typing matches self: Series[Any] against Series[Timedelta], returning Series[int], while concrete Series[int] (verified in tests/series/int/test_floordiv.py) does not match self: Series[Timedelta] and still fails static type checking.
- Alternative rejected.
Series[Never] overloads, which maintainers asked to avoid.
Verification
mypy pandas-stubs tests --no-incremental --strict passed (386 source files, 0 issues)
pyright passed (0 errors, 0 warnings)
poe pyrefly passed (0 diagnostics, 2,209 suppressed)
poe pytest_architecture passed (62 passed)
poe style passed (ruff check, codespell, isort, black, check-ignore-spacing, guard-dunders-posonly)
pytest tests/series/test_floordiv.py tests/series/int/test_floordiv.py tests/series/timedelta/test_floordiv.py passed (17 passed)
- Caveat. Local
.venv has ty 0.0.89 while pyproject.toml now requires ty>=0.0.100, so full poe ty awaits CI.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thank you @Blizzeq . I feel python/mypy#22106 may change how we design the overloads soon. Let's wait for the decision there.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
cmp0xff
commented
Oct 14, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Yes, I think we will have to do that. It might be that we just have to make the methods allow wider arguments than we do now with the [Never] hack.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Makes sense, closing this. Happy to help with the wider signatures once the direction is settled.
|
cmp0xff reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 3, 2026
|
Hi @Blizzeq , if you have the capacity to make it work without the Never trick, you may reopen this PR / open a new PR. Otherwise @hector2394 would probably propose a solution also without the Never trick.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Blizzeq
changed the title
TYP: allow floor division of Series[Any] by a timedelta scalar
TYP: allow floor division of Series[Any] by a timedelta scalar
Nov 14, 2026
|
Thanks for pinging me, @cmp0xff, and thanks @hector2394 for raising this. I merged main and updated the branch to add timedelta | np.timedelta64 directly to the existing self: Series[Timedelta] overloads on __floordiv__ and floordiv without any Series[Never] overloads, so concrete Series[int] rejection (tested in tests/series/int/test_floordiv.py) stays intact. Reopening this PR now.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
cmp0xff
cmp0xff approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|