Skip to content

Commit aef9fb7

Browse files
committed
feat: comments
1 parent 69cb0cb commit aef9fb7

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

pandas-stubs/core/series.pyi

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ from typing import (
2727
Literal,
2828
NoReturn,
2929
Protocol,
30+
TypeVar,
3031
final,
3132
overload,
3233
type_check_only,
@@ -210,10 +211,16 @@ from pandas.core.dtypes.dtypes import CategoricalDtype
210211

211212
from pandas.plotting import PlotAccessor
212213

214+
_T_INTERVAL_NP = TypeVar("_T_INTERVAL_NP", bound=np.bytes_ | np.str_)
215+
213216
@type_check_only
214217
class _SupportsAdd(Protocol[_T_co]):
215218
def __add__(self, value: Self, /) -> _T_co: ...
216219

220+
@type_check_only
221+
class SupportsSelfSub(Protocol[_T_co]):
222+
def __sub__(self, x: Self, /) -> _T_co: ...
223+
217224
@type_check_only
218225
class _SupportsMul(Protocol[_T_co]):
219226
def __mul__(self, value: Self, /) -> _T_co: ...
@@ -832,22 +839,19 @@ class Series(IndexOpsMixin[S1], NDFrame):
832839
self, other: Series[S1], min_periods: int | None = None, ddof: int = 1
833840
) -> float: ...
834841
@overload
835-
def diff(self: Series[_bool], periods: int = ...) -> Series[type[object]]: ... # type: ignore[overload-overlap]
836-
@overload
837-
def diff(self: Series[complex], periods: int = ...) -> Series[complex]: ... # type: ignore[overload-overlap]
838-
@overload
839-
def diff(
840-
self: Series[bytes] | Series[type] | Series[_str] | Series[Interval],
841-
periods: int = ...,
842-
) -> Never: ...
842+
def diff( # type: ignore[overload-overlap]
843+
self: Series[Never] | Series[int], periods: int = ...
844+
) -> Series[float]: ...
843845
@overload
844-
def diff(self: Series[Timestamp], periods: int = ...) -> Series[Timedelta]: ... # type: ignore[overload-overlap]
846+
def diff(self: Series[_bool], periods: int = ...) -> Series: ...
845847
@overload
846-
def diff(self: Series[Timedelta], periods: int = ...) -> Series[Timedelta]: ... # type: ignore[overload-overlap]
848+
def diff(self: Series[Period], periods: int = ...) -> OffsetSeries: ...
847849
@overload
848-
def diff(self: Series[Period], periods: int = ...) -> OffsetSeries: ... # type: ignore[overload-overlap]
850+
def diff(self: Series[Interval], periods: int = ...) -> Never: ...
849851
@overload
850-
def diff(self, periods: int = ...) -> Series[float]: ...
852+
def diff(
853+
self: SupportsGetItem[Scalar, SupportsSelfSub[S1_CO]], periods: int = ...
854+
) -> Series[S1_CO]: ...
851855
def autocorr(self, lag: int = 1) -> float: ...
852856
@overload
853857
def dot(self, other: Series[S1]) -> Scalar: ...
@@ -4665,11 +4669,11 @@ class Series(IndexOpsMixin[S1], NDFrame):
46654669
@overload
46664670
def to_numpy(
46674671
self: Series[Interval],
4668-
dtype: type[np.bytes_],
4672+
dtype: type[_T_INTERVAL_NP],
46694673
copy: bool = False,
46704674
na_value: Scalar = ...,
46714675
**kwargs,
4672-
) -> np_1darray[np.bytes_]: ...
4676+
) -> np_1darray[_T_INTERVAL_NP]: ...
46734677
@overload
46744678
def to_numpy( # pyright: ignore[reportIncompatibleMethodOverride]
46754679
self,

tests/series/test_series.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
Never,
4141
Self,
4242
TypeAlias,
43-
assert_never,
4443
assert_type,
4544
)
4645
import xarray as xr
@@ -3503,47 +3502,39 @@ def test_diff() -> None:
35033502
BaseOffset,
35043503
index_to_check_for_type=-1,
35053504
)
3506-
# bool -> object
3505+
# bool -> Any
35073506
check(
35083507
assert_type(
3509-
pd.Series([True, True, False, False, True]).diff(),
3510-
"pd.Series[type[object]]",
3508+
pd.Series([True, True, False, False, True]).diff(), "pd.Series[Any]"
35113509
),
35123510
pd.Series,
35133511
object,
35143512
)
3515-
# object -> object
3516-
check(
3517-
assert_type(s.astype(object).diff(), "pd.Series[type[object]]"),
3518-
pd.Series,
3519-
object,
3520-
)
3513+
# Any -> float
3514+
s_o = s.astype(object)
3515+
assert_type(s_o, "pd.Series[Any]")
3516+
check(assert_type(s_o.diff(), "pd.Series[float]"), pd.Series, float)
35213517
# complex -> complex
35223518
check(
35233519
assert_type(s.astype(complex).diff(), "pd.Series[complex]"), pd.Series, complex
35243520
)
3525-
if TYPE_CHECKING_INVALID_USAGE:
3526-
# interval -> TypeError: IntervalArray has no 'diff' method. Convert to a suitable dtype prior to calling 'diff'.
3527-
assert_never(pd.Series([pd.Interval(0, 2), pd.Interval(1, 4)]).diff())
35283521

3522+
def _diff_invalid0(): # pyright: ignore[reportUnusedFunction]
3523+
# interval -> TypeError: IntervalArray has no 'diff' method. Convert to a suitable dtype prior to calling 'diff'.
3524+
assert_type(pd.Series([pd.Interval(0, 2), pd.Interval(1, 4)]).diff(), Never)
35293525

3530-
def test_diff_never1() -> None:
3531-
s = pd.Series([1, 1, 2, 3, 5, 8])
3532-
if TYPE_CHECKING_INVALID_USAGE:
3526+
def _diff_invalid1() -> None: # pyright: ignore[reportUnusedFunction]
3527+
s = pd.Series([1, 1, 2, 3, 5, 8])
35333528
# bytes -> numpy.core._exceptions._UFuncNoLoopError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('S21'), dtype('S21')) -> None
3534-
assert_never(s.astype(bytes).diff())
3535-
3529+
s.astype(bytes).diff() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue]
35363530

3537-
def test_diff_never2() -> None:
3538-
if TYPE_CHECKING_INVALID_USAGE:
3531+
def _diff_invalid2() -> None: # pyright: ignore[reportUnusedFunction]
35393532
# dtype -> TypeError: unsupported operand type(s) for -: 'type' and 'type'
3540-
assert_never(pd.Series([str, int, bool]).diff())
3541-
3533+
pd.Series([str, int, bool]).diff() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue]
35423534

3543-
def test_diff_never3() -> None:
3544-
if TYPE_CHECKING_INVALID_USAGE:
3535+
def _diff_invalid3() -> None: # pyright: ignore[reportUnusedFunction]
35453536
# str -> TypeError: unsupported operand type(s) for -: 'str' and 'str'
3546-
assert_never(pd.Series(["a", "b"]).diff())
3537+
pd.Series(["a", "b"]).diff() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue]
35473538

35483539

35493540
def test_operator_constistency() -> None:

tests/test_timefuncs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,11 @@ def test_series_types_to_numpy() -> None:
10631063
np_1darray,
10641064
dtype=np.bytes_,
10651065
)
1066+
check(
1067+
assert_type(i_s.to_numpy(dtype=np.str_), np_1darray[np.str_]),
1068+
np_1darray,
1069+
dtype=np.str_,
1070+
)
10661071

10671072

10681073
def test_index_types_to_numpy() -> None:

0 commit comments

Comments
 (0)