Skip to content

pd.Series gets converted to pd.TimestampSeries after isinstance check #877

@tvdboom

Description

@tvdboom

Describe the bug
A pd.Series variable changes type after an isinstance check.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
df1: pd.Series | pd.DataFrame = pd.Series()
reveal_type(df1)  # <- As expected
if isinstance(df1, pd.Series):
    reveal_type(df1)  # <- ???
  1. Indicate which type checker you are using (mypy or pyright).
    mypy
  2. Show the error message received from that type checker while checking your example.
note: Revealed type is "Union[pandas.core.series.Series[Any], pandas.core.frame.DataFrame]"
note: Revealed type is "pandas.core.series.TimestampSeries"

Please complete the following information:

  • OS: Windows
  • OS Version: 10
  • python version: 3.11
  • version of type checker: 1.8.0
  • version of installed pandas-stubs: 2.2.0.240218

Activity

Dr-Irv

Dr-Irv commented on Feb 25, 2024

@Dr-Irv
Collaborator

pyright 1.1.351 gets it right:

issue877.py:4:13 - information: Type of "df1" is "Series[Unknown]"
issue877.py:5:4 - error: Unnecessary isinstance call; "Series[Unknown]" is always an instance of "Series[Unknown]" (reportUnnecessaryIsInstance)
issue877.py:6:17 - information: Type of "df1" is "Series[Unknown]"

I think this is a mypy bug. See python/mypy#15322

I'll keep this open in case mypy ever fixes that issue.

loicdiridollou

loicdiridollou commented on Nov 11, 2024

@loicdiridollou
Member

I am unable to reproduce this one, used the same example as @tvdboom above and this is what mypy returns to me:

===========================================
Beginning: 'Run mypy on 'tests' (using the local stubs) and on the local stubs'
===========================================

tests/test_frame.py:3584: note: Revealed type is "Union[pandas.core.series.Series[Any], pandas.core.frame.DataFrame]"
tests/test_frame.py:3586: note: Revealed type is "pandas.core.series.Series[builtins.float]"

Not sure exactly where it got fixed but it seems like it does not map to TimestampSeries anymore.

Dr-Irv

Dr-Irv commented on Nov 11, 2024

@Dr-Irv
Collaborator

It's still an issue, because what happens now is that it is picking up the first overload for Series.__new__(), which returns a Series[float]. The first reveal_type() showed that the Union contained Series[Any], and you'd expect the same from the second reveal_type().

So mypy still has to address the linked issue.

loicdiridollou

loicdiridollou commented on Nov 11, 2024

@loicdiridollou
Member

Ohh I understand, thanks a lot for all the details!

loicdiridollou

loicdiridollou commented on Aug 7, 2025

@loicdiridollou
Member

With the release of 1.17 for mypy I checked this issue again and seems like it is fixed.
With pyright we get pd.Series for each of the reveal_type:

./Code/pandas-stubs/main/test_file.py
  ./Code/pandas-stubs/main/test_file.py:6:13 - information: Type of "df1" is "Series[Any]"
  ./Code/pandas-stubs/main/test_file.py:7:4 - error: Unnecessary isinstance call; "Series[Any]" is always an instance of "Series[Unknown]" (reportUnnecessaryIsInstance)
  ./Code/pandas-stubs/main/test_file.py:8:17 - information: Type of "df1" is "Series[Any]"
1 error, 0 warnings, 2 informations

And for mypy I get the Union for the first reveal_type and Series[Any] for the latter:

Poe => mypy

===========================================
Beginning: 'Run mypy on 'tests' (using the local stubs) and on the local stubs'
===========================================

tests/test_file.py:6: note: Revealed type is "pandas.core.series.Series[Any] | pandas.core.frame.DataFrame"
tests/test_file.py:8: note: Revealed type is "pandas.core.series.Series[Any]"
Success: no issues found in 221 source files

Is this the result that we are expecting?

Dr-Irv

Dr-Irv commented on Aug 7, 2025

@Dr-Irv
Collaborator

With the release of 1.17 for mypy I checked this issue again and seems like it is fixed. With pyright we get pd.Series for each of the reveal_type:

./Code/pandas-stubs/main/test_file.py
  ./Code/pandas-stubs/main/test_file.py:6:13 - information: Type of "df1" is "Series[Any]"
  ./Code/pandas-stubs/main/test_file.py:7:4 - error: Unnecessary isinstance call; "Series[Any]" is always an instance of "Series[Unknown]" (reportUnnecessaryIsInstance)
  ./Code/pandas-stubs/main/test_file.py:8:17 - information: Type of "df1" is "Series[Any]"
1 error, 0 warnings, 2 informations

And for mypy I get the Union for the first reveal_type and Series[Any] for the latter:

Poe => mypy

===========================================
Beginning: 'Run mypy on 'tests' (using the local stubs) and on the local stubs'
===========================================

tests/test_file.py:6: note: Revealed type is "pandas.core.series.Series[Any] | pandas.core.frame.DataFrame"
tests/test_file.py:8: note: Revealed type is "pandas.core.series.Series[Any]"
Success: no issues found in 221 source files

Is this the result that we are expecting?

Yes. I will close as a result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    mypy bugRequires mypy to fix a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Dr-Irv@tvdboom@loicdiridollou

        Issue actions

          pd.Series gets converted to pd.TimestampSeries after isinstance check · Issue #877 · pandas-dev/pandas-stubs