Skip to content

close out a few first issues #670

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

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pandas-stubs/core/indexes/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def interval_range(
start: _TimestampLike,
end: _TimestampLike = ...,
periods: int | None = ...,
freq: str | BaseOffset | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is pd.Timedelta a subclass of datetime.timedelta? Could simplify this union (or keep, if it is more explicit).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a subclass. We're inconsistent in the stubs. Some places we just say pd.Timedelta, others we have the union. I like the explicit aspect to make it clear that dt.timedelta is accepted.

name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timestamp]]: ...
Expand All @@ -381,7 +381,7 @@ def interval_range(
start: None = ...,
end: _TimestampLike,
periods: int | None = ...,
freq: str | BaseOffset | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timestamp]]: ...
Expand All @@ -391,7 +391,7 @@ def interval_range(
*,
end: None = ...,
periods: int | None = ...,
freq: str | BaseOffset | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timestamp]]: ...
Expand All @@ -400,7 +400,7 @@ def interval_range(
start: _TimedeltaLike,
end: _TimedeltaLike = ...,
periods: int | None = ...,
freq: str | BaseOffset | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timedelta]]: ...
Expand All @@ -410,7 +410,7 @@ def interval_range(
start: None = ...,
end: _TimedeltaLike,
periods: int | None = ...,
freq: str | BaseOffset | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timedelta]]: ...
Expand All @@ -420,7 +420,7 @@ def interval_range(
*,
end: None = ...,
periods: int | None = ...,
freq: str | BaseOffset | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timedelta]]: ...
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def timedelta_range(
start: TimedeltaConvertibleTypes = ...,
end: TimedeltaConvertibleTypes = ...,
periods: int | None = ...,
freq: str | DateOffset | None = ...,
freq: str | DateOffset | Timedelta | dt.timedelta | None = ...,
name: Hashable | None = ...,
closed: Literal["left", "right"] | None = ...,
) -> TimedeltaIndex: ...
10 changes: 8 additions & 2 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ class _LocIndexerSeries(_LocIndexer, Generic[S1]):
@overload
def __getitem__(
self,
idx: MaskType | Index | Sequence[float] | list[str] | slice | _IndexSliceTuple,
idx: MaskType
| Index
| Sequence[float]
| list[str]
| slice
| _IndexSliceTuple
| Callable,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to keep it as is - it is probably not worth to try to enumerate all allowed arguments/return values.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We have it this way in DataFrame.loc as well.

# _IndexSliceTuple is when having a tuple that includes a slice. Could just
# be s.loc[1, :], or s.loc[pd.IndexSlice[1, :]]
) -> Series[S1]: ...
Expand Down Expand Up @@ -775,7 +781,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
@overload
def apply(
self,
func: Callable[..., Scalar | Sequence | set | Mapping],
func: Callable[..., Scalar | Sequence | set | Mapping | None],
convertDType: _bool = ...,
args: tuple = ...,
**kwds,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,30 @@ def test_interval_range():
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.interval_range(
pd.Timestamp(2000, 1, 1),
pd.Timestamp(2010, 1, 1),
freq=pd.Timedelta(days=30),
),
"pd.IntervalIndex[pd.Interval[pd.Timestamp]]",
),
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.interval_range(
pd.Timestamp(2000, 1, 1),
pd.Timestamp(2010, 1, 1),
freq=dt.timedelta(days=30),
),
"pd.IntervalIndex[pd.Interval[pd.Timestamp]]",
),
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.interval_range(pd.Timestamp(2000, 1, 1), dt.datetime(2010, 1, 1), 5),
Expand All @@ -308,6 +332,26 @@ def test_interval_range():
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.interval_range(
pd.Timedelta("1D"), pd.Timedelta("10D"), freq=pd.Timedelta("2D")
),
"pd.IntervalIndex[pd.Interval[pd.Timedelta]]",
),
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.interval_range(
pd.Timedelta("1D"), pd.Timedelta("10D"), freq=dt.timedelta(days=2)
),
"pd.IntervalIndex[pd.Interval[pd.Timedelta]]",
),
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.interval_range(end=pd.Timedelta("10D"), periods=10, freq="D"),
Expand Down
12 changes: 12 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1820,3 +1820,15 @@ def test_convert_dtypes_dtype_backend() -> None:
s = pd.Series([1, 2, 3, 4])
s1 = s.convert_dtypes(dtype_backend="numpy_nullable")
check(assert_type(s1, pd.Series), pd.Series)


def test_apply_returns_none() -> None:
# GH 557
s = pd.Series([1, 2, 3])
check(assert_type(s.apply(lambda x: None), pd.Series), pd.Series)


def test_loc_callable() -> None:
# GH 586
s = pd.Series([1, 2])
check(assert_type(s.loc[lambda x: x > 1], pd.Series), pd.Series)
14 changes: 14 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,20 @@ def test_timedelta_range() -> None:
),
pd.TimedeltaIndex,
)
check(
assert_type(
pd.timedelta_range("1 day", "10 days", freq=pd.Timedelta("2 days")),
pd.TimedeltaIndex,
),
pd.TimedeltaIndex,
)
check(
assert_type(
pd.timedelta_range("1 day", "10 days", freq=dt.timedelta(days=2)),
pd.TimedeltaIndex,
),
pd.TimedeltaIndex,
)


def test_dateoffset_freqstr() -> None:
Expand Down