-
-
Notifications
You must be signed in to change notification settings - Fork 140
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[Timestamp].diff()
should return Series[Timedelta]
#821
Comments
Thanks for the report. I think we have to add |
looking at series.diff documentation it looks like diff of Series[int] returns Series[float] and I made a test to confirm this. Current stubs returns Series[int]. I am not sure this is a correct behavior of pandas? |
Yes, it seems that's an error in the stub for |
We have to manage objects also from
Any other object people can think of before starting to code ? |
You'd have to look at the definition of You might want to look at how we handle Thanks for looking into this. It's non-trivial. |
For info, if it is ok, I'll need to create new classes in |
You shouldn't have to do this. Just follow the pattern used for |
Almost finished fixing this issue in https://github.com/mutricyl/pandas-stubs/tree/821-series_diff-timestamp2 however I have got an issue with > check(
assert_type(
pd.Series(
[datetime.datetime.now().date(), datetime.datetime.now().date()]
).diff(),
"TimedeltaSeries",
),
pd.Series,
pd.Timedelta,
) This test fails in pytest with:
>>> pd.Series( [datetime.datetime.now().date(), datetime.datetime.now().date()] ).diff()
0 NaT
1 0 days
dtype: timedelta64[ns] Looking at if isinstance(actual, pd.Series):
value = actual.iloc[0] first item of Any advise on how to solve this particular case @Dr-Irv ? |
Very interesting use case where how we designed Do the following: Introduce a parameter to |
* Adding tests for #821 * finish to add tests from pd._types.S1 definition * start fixing stubs * date le lt ge gt fixing * new diff overloads * adding a few Never * fix: check function updated for series.diff * clean comments and update period test * cover windows specific RuntimeWarning * PeriodSeries.diff is OffsetSeries * remove comments * try tables 3.9.2 to fix macos github test --------- Co-authored-by: Laurent Mutricy <[email protected]>
#907 closed this, but I am still seeing issues with the code example above. I minimized the code into this example: # python bug.py & mypy bug.py & pyright bug.py
from __future__ import annotations
from typing import reveal_type
import pandas as pd
def fun(stamps: pd.Series[pd.Timestamp]) -> pd.Series[pd.Timedelta]:
reveal_type(stamps)
print(type(stamps[1]))
deltas = stamps.diff()
reveal_type(deltas)
print(type(deltas[1]))
return deltas
times = pd.Series([pd.Timestamp(0), pd.Timestamp(0)])
fun(times)
So runtime behavior is as expected. However,
This is with version |
As a workaround, I could replace Before (working): times: pd.Series[pd.Timestamp] = df[TIME_COL] After (not working): times: TimestampSeries = df[TIME_COL]
|
That's the right workaround for your example
We had to introduce
The reason that works is that we can't track the types of columns within a
This is again related to how the type checker is handling the generic aspect of In general, whenever you refer to a column of a times = cast("TimestampSeries", df[TIME_COL]) |
So one cast needs to be necessary at least. Accepting that, it makes sense doing it as early as possible to catch errors in the processing. Thanks a lot! |
Describe the bug
Series[Timestamp].diff()
returnsSeries[Timedelta]
but stubs indicate it'sSeries[Timestamp]
instead.To Reproduce
Output:
Please complete the following information:
mypy==1.7.1
,pyright==1.1.337
,pandas-stubs==2.1.1.230928
Additional context
I tried both
Series[Timestamp]
andTimestampSeries
due to #673 (comment)Possibly related: #718
The text was updated successfully, but these errors were encountered: