Skip to content

Series[Timestamp].diff() should return Series[Timedelta] #821

Closed
@bersbersbers

Description

@bersbersbers

Describe the bug
Series[Timestamp].diff() returns Series[Timedelta] but stubs indicate it's Series[Timestamp] instead.

To Reproduce

# python bug.py & mypy bug.py & pyright bug.py
from __future__ import annotations

from typing import TYPE_CHECKING, reveal_type

import pandas as pd

if TYPE_CHECKING:
    from pandas.core.series import TimedeltaSeries, TimestampSeries

def fun1(times: pd.Series[pd.Timestamp]) -> pd.Series[pd.Timedelta]:
    reveal_type(times)
    reveal_type(times.diff())
    print(type(times[1]))
    print(type(times.diff()[1]))
    return times.diff()

def fun2(times: TimestampSeries) -> TimedeltaSeries:
    reveal_type(times)
    reveal_type(times.diff())
    print(type(times[1]))
    print(type(times.diff()[1]))
    return times.diff()

times = pd.Series([pd.Timestamp(0), pd.Timestamp(0)])
fun1(times)
fun2(times)

Output:

Runtime type is 'Series'
Runtime type is 'Series'
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
<class 'pandas._libs.tslibs.timedeltas.Timedelta'>
Runtime type is 'Series'
Runtime type is 'Series'
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
<class 'pandas._libs.tslibs.timedeltas.Timedelta'>
bug.py:13: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
bug.py:14: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
bug.py:17: error: Incompatible return value type (got "Series[Timestamp]", expected "Series[Timedelta]")  [return-value]
bug.py:21: note: Revealed type is "pandas.core.series.TimestampSeries"
bug.py:22: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
bug.py:25: error: Incompatible return value type (got "Series[Timestamp]", expected "TimedeltaSeries")  [return-value]
Found 2 errors in 1 file (checked 1 source file)
C:\Build\project\bug.py
  C:\Build\project\bug.py:13:17 - information: Type of "times" is "Series[Timestamp]"
  C:\Build\project\bug.py:14:17 - information: Type of "times.diff()" is "Series[Timestamp]"
  C:\Build\project\bug.py:17:12 - error: Expression of type "Series[Timestamp]" cannot be assigned to return type "Series[Timedelta]"
    "Series[Timestamp]" is incompatible with "Series[Timedelta]"
      Type parameter "S1@Series" is invariant, but "Timestamp" is not the same as "Timedelta" (reportGeneralTypeIssues)
  C:\Build\project\bug.py:21:17 - information: Type of "times" is "TimestampSeries"
  C:\Build\project\bug.py:22:17 - information: Type of "times.diff()" is "Series[Timestamp]"
  C:\Build\project\bug.py:25:12 - error: Expression of type "Series[Timestamp]" cannot be assigned to return type "TimedeltaSeries"
    "Series[Timestamp]" is incompatible with "TimedeltaSeries" (reportGeneralTypeIssues)
2 errors, 0 warnings, 4 informations

Please complete the following information:

  • Windows 10 22H2
  • Python 3.12.0
  • mypy==1.7.1, pyright==1.1.337, pandas-stubs==2.1.1.230928

Additional context
I tried both Series[Timestamp] and TimestampSeries due to #673 (comment)
Possibly related: #718

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions