pandas-dev / pandas-stubs

Public type stubs for pandas
BSD 3-Clause "New" or "Revised" License
229 stars 123 forks source link

Check incorrect when using a TimestampSeries and YearEnd from pandas.tseries.offsets #969

Closed clemconv1 closed 2 days ago

clemconv1 commented 1 month ago

It seems type annotations are wrong between a Timestamp series and tools offered by the offsets library, using pyright

df["year_end"] = df["date"] + pd.tseries.offsets.YearEnd(0) is flagged with Operator "+" not supported for types "TimestampSeries" and "YearEnd"

Dr-Irv commented 1 month ago

Can you provide a more complete example?

loicdiridollou commented 4 days ago

I digged into that one as I remember having similar issues on a project but I was not able to fully reproduce with TimestampSeries. Instead here is a short example that can reproduce a similar error, @Dr-Irv let me know if this is something that we thrive to fix through an overload (I assume, haven't looked at the code in details).

import pandas as pd

df = pd.DataFrame({"date": pd.bdate_range("2024-09-01", "2024-09-10")})
df["date"] = pd.to_datetime(df["date"])
vv = pd.bdate_range("2024-09-01", "2024-09-10")

vv + pd.tseries.offsets.YearEnd(0)

which will raise the error: Operator "+" not supported for types "DatetimeIndex" and "YearEnd"

loicdiridollou commented 4 days ago

https://github.com/pandas-dev/pandas-stubs/blob/5177d67a3189aee4037031ffd503f0af997c617b/pandas-stubs/core/indexes/datetimes.pyi#L58-L61 Likely could be added here as pd.tseries.offsets.YearEnd is a class from the BaseOffset base class and not Timedelta. Open to discussing if this is the correct approach.

alippai commented 4 days ago

Might be the same / similar as https://github.com/pandas-dev/pandas-stubs/issues/755

twoertwein commented 4 days ago

I digged into that one as I remember having similar issues on a project but I was not able to fully reproduce with TimestampSeries. Instead here is a short example that can reproduce a similar error, @Dr-Irv let me know if this is something that we thrive to fix through an overload (I assume, haven't looked at the code in details).

import pandas as pd

df = pd.DataFrame({"date": pd.bdate_range("2024-09-01", "2024-09-10")})
df["date"] = pd.to_datetime(df["date"])
vv = pd.bdate_range("2024-09-01", "2024-09-10")

vv + pd.tseries.offsets.YearEnd(0)

which will raise the error: Operator "+" not supported for types "DatetimeIndex" and "YearEnd"

Feel free to open a MR - I think this should work!

loicdiridollou commented 3 days ago

Might be the same / similar as #755

Not sure it is exactly the same, maybe under the hood they are related but I could not find obvious ways to tackle those two at once. I see that you have already opened a PR for your issue so I will leave that to you, you surely have more context on it!