Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Series.shift does not always preserve input dtype, but is typed as if it does #1111

Closed
MarcoGorelli opened this issue Feb 8, 2025 · 2 comments · Fixed by #1108
Closed

Series.shift does not always preserve input dtype, but is typed as if it does #1111

MarcoGorelli opened this issue Feb 8, 2025 · 2 comments · Fixed by #1108

Comments

@MarcoGorelli
Copy link
Member

Describe the bug
``

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
  2. Indicate which type checker you are using (mypy or pyright).
  3. Show the error message received from that type checker while checking your example.
import pandas as pd


reveal_type(pd.Series([1,2,3]).shift())
#  t.py:4: note: Revealed type is "pandas.core.series.Series[builtins.int]"

however, the result is actually Series['float']

It's not clear to me if/how pandas-stubs distinguishes ints and nullable ints #1110

Please complete the following information:

  • OS: [e.g. Windows, Linux, MacOS] linux
  • OS Version [e.g. 22] 22.04
  • python version 3.12.5
  • version of type checker mypy 1.15
  • version of installed pandas-stubs 2.2.3.241126

Additional context
noticed looking into #1108

@MarcoGorelli
Copy link
Member Author

This one might quite complex to type correctly?

  • shift(n) changes from 'int' to 'float' (and from 'bool' to 'object'), unless:
    • n is 0
    • fill_value is specified as a compatible type
    • freq is also specified

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 10, 2025

As I stated here: #1108 (comment) , I don't think we can do this because the value of freq and the underlying index could change the result.

I think we have to just return Series, since we can't infer the subtype.

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 a pull request may close this issue.

2 participants